Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resample with DateOffset can give NotImplementedError

Tags:

pandas

Docs for resample say it can take a DateOffset as the rule.
But trying this out sometimes results in a NotImplementedError.

  • Passing a string e.g. resample('2M') works.
  • The equivalent resample(pd.offsets.MonthEnd(2)) also works.
  • But the similar (though different) resample(pd.offsets.DateOffset(months=2)) fails.

Here's code to reproduce:

idx = pd.date_range('20190101', periods=14, freq='3W')
s   = pd.Series(range(len(idx)), index=idx)
s.resample(pd.offsets.DateOffset(months=2)).mean()

Trace:

Traceback (most recent call last):
    ....
  File "....core\generic.py", line 8449, in resample
    level=level,
  File "....core\resample.py", line 1305, in resample
    tg = TimeGrouper(**kwds)
  File "....core\resample.py", line 1378, in __init__
    rule = freq.rule_code
  File "....tseries\offsets.py", line 442, in rule_code
    return self._prefix
  File "....tseries\offsets.py", line 438, in _prefix
    raise NotImplementedError("Prefix not defined")
NotImplementedError: Prefix not defined

Am I right that resample supports only certain DateOffsets?

What kinds doesn't/does it support?

like image 332
qandour Avatar asked Oct 16 '19 14:10

qandour


1 Answers

As the comments have stated it is a bug but you can work around it:

According to the documentation, the rule= parameter of DataFrame.resample() can be of type DateOffset, Timedelta or str. Apparently, it actually expects some of the DateOffset subclasses (like MonthEnd, BusinessDay, etc.), because rule_code attribute is not defined in the parent class.

You can either use an equivalent TimeDelta or one of the subclasses of DateOffset

like image 53
rudolfovic Avatar answered Jan 02 '23 21:01

rudolfovic