类型错误:时间戳减法

Riv*_*334 6 python datetime-format python-2.7 python-datetime pandas

我有一个可以收集数据的脚本。我遇到了TypeError: Timestamp subtraction must have the same timezones or no timezones错误。我查看了有关此错误的其他帖子,但无法为我找到解决方案。

我怎样才能绕过这个错误。一旦收集到数据,我就不会对其进行操作,我也不太明白为什么我不能将其保存dataframe到 Excel 文档中。任何人都可以提供帮助吗?

import pandas as pd
import numpy as np
import os
import datetime
import pvlib
from pvlib.forecast import GFS, NAM

#directories and filepaths
barnwell_dir = r'D:\Saurabh\Production Forecasting\Machine Learning\Sites\Barnwell'
barnwell_training = r'8760_barnwell.xlsx'

#constants
writer = pd.ExcelWriter('test' + '_PythonExport.xlsx', engine='xlsxwriter')    
time_zone = 'Etc/GMT+5'
barnwell_list = [r'8760_barnwell.xlsx', 33.2376, -81.3510] 

def get_gfs_processed_data1():
    start = pd.Timestamp(datetime.date.today(), tz=time_zone) #used for testing last week
    end = start + pd.Timedelta(days=6)
    gfs = GFS(resolution='quarter')
    #get processed data for lat/long point
    forecasted_data = gfs.get_processed_data(barnwell_list[1], barnwell_list[2], start, end)
    forecasted_data.to_excel(writer, sheet_name='Sheet1')


get_gfs_processed_data1()
Run Code Online (Sandbox Code Playgroud)

jmc*_*ara 7

当我运行您的示例代码时,我在堆栈跟踪结束时从 XlsxWriter 收到以下警告:

"Excel doesn't support timezones in datetimes. "
TypeError: Excel doesn't support timezones in datetimes. 
Set the tzinfo in the datetime/time object to None or use the
'remove_timezone' Workbook() option
Run Code Online (Sandbox Code Playgroud)

我认为这是合理的不言自明。要从时间戳中去除时区,请remove_timezone按照建议传递选项:

writer = pd.ExcelWriter('test' + '_PythonExport.xlsx',
                        engine='xlsxwriter',
                        options={'remove_timezone': True})
Run Code Online (Sandbox Code Playgroud)

当我进行此更改时,示例将运行并生成一个 xlsx 文件。请注意,该remove_timezone选项需要 XlsxWriter >= 0.9.5。