【问题标题】:datetime index to datetime series not working in Pandas?日期时间序列的日期时间索引在熊猫中不起作用?
【发布时间】:2020-05-27 22:10:14
【问题描述】:

我正在尝试将日期时间索引转换为日期时间序列,但出现错误:

ticks = pd.date_range(start = '2019-12-30', end = '2020-02-11', periods = 6)
ticks.to_datetime()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-79-f41607e594d5> in <module>
----> 1 ticks.to_datetime(dayfirst=False)

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

为什么以及我应该怎么做?

【问题讨论】:

    标签: python-3.x pandas datetime indexing


    【解决方案1】:

    这里ticks 已经是DatetimeIndex,所以不需要转换:

    ticks = pd.date_range(start = '2019-12-30', end = '2020-02-11', periods = 6)
    

    如果要Series:

    s = ticks.to_series()
    

    或者:

    s = pd.Series(ticks)
    

    如果想转换为日期时间,可以pandas.to_datetime

    pd.to_datetime(ticks)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2021-12-29
      • 2023-01-12
      • 2020-09-02
      • 2013-07-20
      • 2021-06-21
      相关资源
      最近更新 更多