【问题标题】:TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'TypeError:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但获得了“RangeIndex”实例
【发布时间】:2020-04-18 08:23:46
【问题描述】:

这是我的数据框的样子:

intrate.head()
Out[83]: 
         Date  UNRATE
0  1954-07-01    0.80
1  1954-08-01    1.22
2  1954-09-01    1.06
3  1954-10-01    0.85
4  1954-11-01    0.83

这些年一直到 2019 年,我正在尝试将其重新采样到各个年份。

我已将日期列转换为日期时间,如下所示。

Out[86]: 
Date      datetime64[ns]
UNRATE           float64
dtype: object

问题是当我尝试重新采样时:

intratey = intrate.resample('A').ffill()

我收到以下错误消息:

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    resample 要求 index 为日期时间格式。因此,如果您选择日期列作为索引,它应该可以工作。你可以使用set_index来做到这一点。

    【讨论】:

    • 谢谢约瑟夫!我不敢相信我错过了。
    猜你喜欢
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 2015-08-31
    • 1970-01-01
    • 2018-11-13
    相关资源
    最近更新 更多