Docs for resample
say it can take a DateOffset
as the rule.
But trying this out sometimes results in a NotImplementedError
.
resample('2M')
works.resample(pd.offsets.MonthEnd(2))
also works.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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With