【问题标题】:Resample to DatetimeIndex raises an error重新采样到 DatetimeIndex 会引发错误
【发布时间】:2012-10-15 09:20:48
【问题描述】:

我创建了一个 DatetimeIndex,我想使用该索引重新采样数据。当我这样做时,我得到一个例外:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.1-py2.7-linux-i686.egg/pandas/core/generic.py", line 188, in resample
limit=limit, base=base)

  File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.1-py2.7-linux-i686.egg/pandas/tseries/resample.py", line 41, in __init__
    self.freq = to_offset(freq)

  File "/usr/local/lib/python2.7/dist-packages/pandas-0.8.1-py2.7-linux-i686.egg/pandas/tseries/frequencies.py", line 392, in to_offset
raise ValueError("Could not evaluate %s" % freqstr)
ValueError: Could not evaluate <class 'pandas.tseries.index.DatetimeIndex'>

[2012-03-02 09:00:00, ..., 2012-03-02 15:00:00]
Length: 73, Freq: 5T, Timezone: None

似乎 TimeGrouper.resample 应该处理 DatetimeIndex 但在 TimeGrouper init 中调用 to_offset 却没有。但我可能会在这里遗漏一些东西。

知道如何按索引重新采样吗? 或者也许是一种解决方法 - 我想要做的只是按某个频率(例如 1 分钟)进行采样,但开始/结束时间不一定在原始时间序列中有数据点(我试图有一堆从上午 8 点到下午 4 点采样的时间序列,但有些只有上午 9:30 的值,有些是上午 10 点等)

【问题讨论】:

    标签: python time-series pandas


    【解决方案1】:

    AFAIK 你不能传入 DatetimeIndex 来重新采样。作为一种解决方法,只需按频率别名('1Min')重新采样,然后重新索引到您生成的索引?

    我创建了一个github issue,可能会考虑添加额外的参数来重新采样。欢迎在此处留下更多反馈。

    【讨论】:

    • 谢谢。重采样到 1Min 将从我的第一次观察开始,对吧?那么如果第一次观察是在 10:15:23,整个索引会移动 23 秒?有什么解决方法吗?
    【解决方案2】:

    前几天我发了一个类似的问题:Date ranges in Pandas

    Wes 回复说他计划最终像这样扩展resample

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 2013-03-25
      相关资源
      最近更新 更多