问题描述
我的数据框在EST中有日期时间索引。但是,此信息不会附加到datetime索引中。我该如何追加?
我的代码:
df.index =
DatetimeIndex(['2019-10-01 07:49:28','2019-10-01 07:50:21','2020-07-25 18:48:44','2020-07-25 18:49:43','2020-07-25 18:50:43','2020-07-25 18:51:44'],dtype='datetime64[ns]',name='Unnamed: 0',length=6,freq=None)
df.index = df.index.replace(tzinfo='EST')
当前输出:
df.index.replace(tzinfo='EST')
AttributeError: 'DatetimeIndex' object has no attribute 'replace'
预期输出:
df.index =
DatetimeIndex(['2019-10-01 07:49:28-05:00','2019-10-01 07:50:21-05:00','2020-07-25 18:48:44-05:00','2020-07-25 18:49:43-05:00','2020-07-25 18:50:43-05:00','2020-07-25 18:51:44-05:00'],freq=None)
解决方法
使用.tz_localize
添加时区
itx = pd.DatetimeIndex(['2019-10-01 07:49:28','2019-10-01 07:50:21','2020-07-25 18:48:44','2020-07-25 18:49:43','2020-07-25 18:50:43','2020-07-25 18:51:44'],dtype='datetime64[ns]',name='Unnamed: 0')
itx = itx.tz_localize(tz='EST')
Out[285]:
DatetimeIndex(['2019-10-01 07:49:28-05:00','2019-10-01 07:50:21-05:00','2020-07-25 18:48:44-05:00','2020-07-25 18:49:43-05:00','2020-07-25 18:50:43-05:00','2020-07-25 18:51:44-05:00'],dtype='datetime64[ns,EST]',name='Unnamed: 0',freq=None)