pandas to_Datetime 转换与时区感知索引

day*_*yum 5 python datetime pandas timestamp-with-timezone

我有一个带有时区感知索引的数据框

>>> dfn.index
Out[1]: 
DatetimeIndex(['2004-01-02 01:00:00+11:00', '2004-01-02 02:00:00+11:00',
               '2004-01-02 03:00:00+11:00', '2004-01-02 04:00:00+11:00',
               '2004-01-02 21:00:00+11:00', '2004-01-02 22:00:00+11:00'],
              dtype='datetime64[ns]', freq='H', tz='Australia/Sydney')
Run Code Online (Sandbox Code Playgroud)

我把它保存在csv中,然后按如下方式阅读:

>>> dfn.to_csv('temp.csv')
>>> df= pd.read_csv('temp.csv', index_col=0 ,header=None )
>>> df.head()
Out[1]: 
                                1
0                                
NaN                        0.0000
2004-01-02 01:00:00+11:00  0.7519
2004-01-02 02:00:00+11:00  0.7520
2004-01-02 03:00:00+11:00  0.7515
2004-01-02 04:00:00+11:00  0.7502
Run Code Online (Sandbox Code Playgroud)

索引作为字符串读取

>>> df.index[1]
Out[3]: '2004-01-02 01:00:00+11:00'
Run Code Online (Sandbox Code Playgroud)

在转换 to_datetime 时,它​​会更改时间,因为它将 +11 增加到小时

>>> df.index = pd.to_datetime(df.index)
>>> df.index[1]
Out[6]: Timestamp('2004-01-01 14:00:00')
Run Code Online (Sandbox Code Playgroud)

我现在可以从索引中减去 11 小时来修复它,但是有没有更好的方法来处理这个问题?

我尝试在此处的答案中使用解决方案,但这会大大减慢代码速度。

jez*_*ael 6

我认为这是您需要以相同方式写入和读取文件头的问题。对于解析日期需要参数parse_dates

#write to file header
dfn.to_csv('temp.csv')
#no read header
df= pd.read_csv('temp.csv', index_col=0 ,header=None)
Run Code Online (Sandbox Code Playgroud)

解决方案1:

#no write header
dfn.to_csv('temp.csv', header=None)
#no read header
df= pd.read_csv('temp.csv', index_col=0 ,header=None, parse_dates=[0])
Run Code Online (Sandbox Code Playgroud)

解决方案2:

#write header
dfn.to_csv('temp.csv')
#read header
df= pd.read_csv('temp.csv', index_col=0, parse_dates=[0])
Run Code Online (Sandbox Code Playgroud)

不幸的是parse_date将日期转换为UTC,因此有必要稍后添加时区:

df.index = df.index.tz_localize('UTC').tz_convert('Australia/Sydney')
print (df.index)
DatetimeIndex(['2004-01-02 01:00:00+11:00', '2004-01-02 02:00:00+11:00',
               '2004-01-02 03:00:00+11:00', '2004-01-02 04:00:00+11:00',
               '2004-01-02 05:00:00+11:00', '2004-01-02 06:00:00+11:00',
               '2004-01-02 07:00:00+11:00', '2004-01-02 08:00:00+11:00',
               '2004-01-02 09:00:00+11:00', '2004-01-02 10:00:00+11:00'],
              dtype='datetime64[ns, Australia/Sydney]', name=0, freq=None)
Run Code Online (Sandbox Code Playgroud)

测试样品:

idx = pd.date_range('2004-01-02 01:00:00', periods=10, freq='H', tz='Australia/Sydney')
dfn = pd.DataFrame({'col':range(len(idx))}, index=idx)
print (dfn)
                           col
2004-01-02 01:00:00+11:00    0
2004-01-02 02:00:00+11:00    1
2004-01-02 03:00:00+11:00    2
2004-01-02 04:00:00+11:00    3
2004-01-02 05:00:00+11:00    4
2004-01-02 06:00:00+11:00    5
2004-01-02 07:00:00+11:00    6
2004-01-02 08:00:00+11:00    7
2004-01-02 09:00:00+11:00    8
2004-01-02 10:00:00+11:00    9
Run Code Online (Sandbox Code Playgroud)