【问题标题】:pandas to_Datetime conversion with timezone aware index带有时区感知索引的 pandas to_Datetime 转换
【发布时间】:2018-04-22 01:23:44
【问题描述】:

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

>>> 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')

我把它保存在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

索引被读取为字符串

>>> df.index[1]
Out[3]: '2004-01-02 01:00:00+11:00'

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

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

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

我尝试使用答案 here 中的解决方案,但这会大大降低代码速度。

【问题讨论】:

  • df= pd.read_csv('temp.csv', index_col=0 ,header=None, parse_dates=[0] ) 呢?
  • 它还添加了+11
  • 我测试了它,你是对的,日期默认转换为 UTC。所以如果不想使用dateutil.parser.parse 需要稍后转换。

标签: python pandas datetime timestamp-with-timezone


【解决方案1】:

我认为这是您需要以相同方式写入和读取文件头的问题。 而解析日期需要参数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)

解决方案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])

解决方案2:

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

不幸的是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)

测试样本:

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

【讨论】:

    猜你喜欢
    • 2019-03-29
    • 2021-11-24
    • 1970-01-01
    • 2015-08-03
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    相关资源
    最近更新 更多