【问题标题】:Pandas DatetimeIndex converting dates to 1970Pandas DatetimeIndex 将日期转换为 1970
【发布时间】:2016-11-23 20:50:21
【问题描述】:

我最近遇到了一个类似的问题 (answered here),即使用这些日期将日期转换为 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

【问题讨论】:

  • 请将您的数据框和代码发布为文本,而不是图像。
  • 如果您发布图片,任何试图帮助您的人都必须手动输入所有内容。请花点时间自己做。
  • 为了帮助您,人们需要能够重现您的问题。您应该通过包含示例代码和数据让人们可以复制和粘贴并自己运行来简化此操作,它将演示问题。
  • 好的,我明白了,我应该创建相同形式的虚拟数据,而不是敏感数据的图像,并包含该代码。我现在就去做。谢谢解释
  • 你是如何导入 tz 的?

标签: python datetime pandas dataframe python-datetime


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

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

【讨论】:

  • 你能转换成本地吗?
  • 正如上述答案所说:除了tz.tzlocal()之外的任何东西。
  • 这确实对我有用,但在我之前的问题中没有。您可以看到,对于这种情况,接受的答案提出了几种解决方案,这个当时不起作用,但现在起作用了。感谢您的帮助
【解决方案2】:
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

【讨论】:

  • 这也适用于我,在我看来是一个更好的解决方法 --- 无需摆弄时区 --- 直到 pandas 0.19 出来。 @Philip O'Brien:如果它也适合你,我认为你应该接受这个答案而不是我的。
猜你喜欢
  • 2019-03-22
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 2017-04-28
  • 2018-10-02
  • 2018-03-12
  • 1970-01-01
  • 2020-04-28
相关资源
最近更新 更多