简体   繁体   中英

Resample to DatetimeIndex raises an error

I created a DatetimeIndex and I want to resample the data with that index. When I do that I get an exception:

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

It seems that TimeGrouper.resample is supposed to handle DatetimeIndex but a call to to_offset in the TimeGrouper init does not. But I could be missing something here.

Any idea how to resample by an index? Or perhaps a workaround - All I'm trying to do is sample by some frequency (say, 1Min) but with start / end time that do not necessarily have data points in the original time series (I'm trying to have a bunch of time series sampled from 8am to 4pm, but some only have values from 9:30am, some from 10am etc.)

AFAIK you cannot pass in a DatetimeIndex to resample. As a workaround, just resample by the freq alias ('1Min') and then reindex to your generated index?

I started a github issue to maybe think about adding in additional parameters to resample. Feel free to leave more feedback there.

I posted a similar question the other day: Date ranges in Pandas

Wes replied saying he plans to extend resample like this eventually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM