pandas DatetimeIndex将日期转换为1970年 [英] Pandas DatetimeIndex converting dates to 1970

查看:736
本文介绍了 pandas DatetimeIndex将日期转换为1970年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我遇到了一个类似的问题(在这里回答),转换为日期到大熊猫 DatetimeIndex 及其后的 groupby 使用这些日期导致日期出现为 1970-01-01 00:00:00 + 00:00 的错误。



我现在在不同的环境中面临这个问题,而以前的解决方案并没有帮助我。



我有一个这样的框架

 将大熊猫导入为pd 
from dateutil import tz

data = {'事件':范围(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)



事件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

,我想将索引从刚刚更改为 MultiIndex [date,ID] ,但是在这样做时,1970 bug出现

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

事件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]))

#事件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]))

#事件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

(我当地的时间与你的不同)


I have recently faced a similar problem (answered here) whereby conversion of a date to a pandas DatetimeIndex and subsequent groupby using those dates led to an error where the date appeared as 1970-01-01 00:00:00+00:00.

I'm facing this problem in a different context now, and the previous solution isn't helping me.

I have a frame like this

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

and I want to change the index from just the date, to a MultiIndex of [date, ID], but in doing so that "1970 bug" appears

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

Versions

  • Python 2.7.11
  • Pandas 0.18.0

解决方案

The accepted answer of your other question works for me (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

(My local time is different from yours.)

这篇关于 pandas DatetimeIndex将日期转换为1970年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆