我最近遇到了一个类似的问题(在这里回答),将日期转换为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 2.7.11
- 熊猫0.18.0
您的另一个问题的公认答案对我有效(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