【问题标题】:pandas.to_datetime ERRORpandas.to_datetime 错误
【发布时间】:2013-05-30 10:24:22
【问题描述】:

我有一个 pandas 数据框,我想将时间列转换为日期时间格式。

时间

2013 年 5 月 30 日 06:00:41 -0600

import pandas as pd
df.index = pd.to_datetime(df.pop('Time'))

但它总是给出以下错误。代码有什么问题? :(

AttributeError                            Traceback (most recent call last)
<ipython-input-124-9219cf10d027> in <module>()
----> 1 df.index = pd.to_datetime(df.pop('Time'))

AttributeError: 'module' object has no attribute 'to_datetime'

【问题讨论】:

  • 所以,我在pd.DatetimeIndex 类中找到了to_datetime 方法。我以前没用过pandas,但是你不能从df.pop('Time') 得到pd.DatetimeIndex 对象吗?或者,也许,转换它?
  • 你用的是哪个版本的熊猫?

标签: python pandas ipython-notebook


【解决方案1】:

使用set_index将时间列设置为索引,然后将Index转换为DateTimeIndex

df = df.set_index('Time')

df.index = df.index.to_datetime()

【讨论】:

    【解决方案2】:

    to_datetime 函数是 introduced in 0.8.0,所以你必须升级你的 pandas 才能使用它。
    最好是latest stable version

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      相关资源
      最近更新 更多