Pandas DatetimeIndex 将日期转换为 1970

Pandas DatetimeIndex converting dates to 1970

我最近遇到了类似的问题() whereby conversion of a date to a pandas DatetimeIndex 和随后的 groupby 使用这些日期导致错误,日期显示为 1970-01-01 00:00:00+00:00

我现在在不同的环境中遇到这个问题,之前的解决方案对我没有帮助。

我有这样的框架

import pandas as pd
from dateutil import tz

data = { 'Events' : range(1, 5 + 1 ,1), 'ID' : [1, 1, 1, 1, 1]}
idx = pd.date_range(start='2008-01-01', end='2008-01-05', freq='D', tz=tz.tzlocal())
frame = pd.DataFrame(data, index=idx)



                           Events  ID
2008-01-01 00:00:00+00:00       1   1
2008-01-02 00:00:00+00:00       2   1
2008-01-03 00:00:00+00:00       3   1
2008-01-04 00:00:00+00:00       4   1
2008-01-05 00:00:00+00:00       5   1

我想将索引从日期更改为 [date, ID]MultiIndex,但这样做会出现“1970 错误”

frame.set_index([frame.ID, frame.index])

                              Events  ID
ID                                      
1  2008-01-01 00:00:00+00:00       1   1
   1970-01-01 00:00:00+00:00       2   1
   1970-01-01 00:00:00+00:00       3   1
   1970-01-01 00:00:00+00:00       4   1
   1970-01-01 00:00:00+00:00       5   1

版本

你的其他问题的公认答案对我有用(Python 3.5.2,Pandas 0.18.1):

print(frame.set_index([frame.ID, frame.index]))

#                               Events  ID
# ID                                      
# 1  2008-01-01 00:00:00-05:00       1   1
#    1970-01-01 00:00:00-05:00       2   1
#    1970-01-01 00:00:00-05:00       3   1
#    1970-01-01 00:00:00-05:00       4   1
#    1970-01-01 00:00:00-05:00       5   1

frame.index = frame.index.tz_convert(tz='EST')
print(frame.set_index([frame.ID, frame.index]))

#                               Events  ID
# ID                                      
# 1  2008-01-01 00:00:00-05:00       1   1
#    2008-01-02 00:00:00-05:00       2   1
#    2008-01-03 00:00:00-05:00       3   1
#    2008-01-04 00:00:00-05:00       4   1
#    2008-01-05 00:00:00-05:00       5   1

(我的当地时间和你的不一样。)

frame = frame.reset_index()
frame = frame.set_index([frame.ID, frame.index])
print frame

                         index  Events  ID
ID                                        
1  0 2008-01-01 00:00:00-05:00       1   1
   1 2008-01-02 00:00:00-05:00       2   1
   2 2008-01-03 00:00:00-05:00       3   1
   3 2008-01-04 00:00:00-05:00       4   1
   4 2008-01-05 00:00:00-05:00       5   1


print frame.info()

<class 'pandas.core.frame.DataFrame'>
MultiIndex: 5 entries, (1, 0) to (1, 4)
Data columns (total 4 columns):
level_0    5 non-null int64
index      5 non-null datetime64[ns, tzlocal()]
Events     5 non-null int64
ID         5 non-null int64
dtypes: datetime64[ns, tzlocal()](1), int64(3)
memory usage: 200.0+ bytes