Skip to content

Handling a CustomBusinessDay in time-based .rolling() #13969

Closed
@chrisaycock

Description

Starting with 10 business days following Christmas Eve:

from pandas.tseries.offsets import CustomBusinessDay
from pandas.tseries.holiday import USFederalHolidayCalendar

days = CustomBusinessDay(calendar=USFederalHolidayCalendar())

df = pd.DataFrame({'value': np.arange(n)},
                  index=pd.date_range('2015-12-24', periods=n, freq=days))

I can compute the three-day sum of the values with just:

In [21]: df.rolling('3d').sum()
Out[21]:
            value
2015-12-24    0.0
2015-12-28    1.0
2015-12-29    3.0
2015-12-30    6.0
2015-12-31    9.0
2016-01-04    5.0
2016-01-05   11.0
2016-01-06   18.0
2016-01-07   21.0
2016-01-08   24.0

But this is purely in terms of Gregorian calendar days, not the business calendar days that I had created the DataFrame with.

I can easily compute

df.index - 3*days

though I get a performance warning:

Non-vectorized DateOffset being applied to Series or DatetimeIndex

But I can't just pass this offset directly:

In [23]: df.rolling(3*days).sum()
...
ValueError: <3 * CustomBusinessDays> is a non-fixed frequency

I would like to be able to handle a CustomBusinessDay in .rolling(). (Because the DataFrame may come from any source, it would be easier to just pass the DateOffset object to .rolling() instead of using anything specific to the DataFrame's index.)

I know that the freq parameter was deprecated in 0.18, though 0.19 kinda brings this back in window. Is there an intrinsic reason window can't handle a CustomBusinessDay?

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions