Skip to content

BUG: resample by BusinessHour raises ValueError #16447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Plotting
Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^

- Bug in ``.resample(..)`` with a ``BusinessHour`` raises ``ValueError`` (:issue:`12351`)


Sparse
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from pandas.tseries.frequencies import to_offset, is_subperiod, is_superperiod
from pandas.core.indexes.datetimes import DatetimeIndex, date_range
from pandas.core.indexes.timedeltas import TimedeltaIndex
from pandas.tseries.offsets import DateOffset, Tick, Day, _delta_to_nanoseconds
from pandas.tseries.offsets import (DateOffset, Tick, Day,
_delta_to_nanoseconds, BusinessHourMixin)
from pandas.core.indexes.period import PeriodIndex, period_range
import pandas.core.common as com
import pandas.core.algorithms as algos
Expand Down Expand Up @@ -1271,8 +1272,11 @@ def _get_range_edges(first, last, offset, closed='left', base=0):
if (is_day and day_nanos % offset.nanos == 0) or not is_day:
return _adjust_dates_anchored(first, last, offset,
closed=closed, base=base)

if not isinstance(offset, Tick): # and first.time() != last.time():
# GH12351
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not what I meant. This is not a general fix at all. This is the same as before. The question is what kind of offset needs to do this kind of rollback / normalization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got your point. so do we need to generalize the edges detection here?

elif issubclass(type(offset), BusinessHourMixin):
first = offset.rollback(first)
last = offset.rollforward(last + offset)
else:
# hack!
first = first.normalize()
last = last.normalize()
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2564,6 +2564,37 @@ def test_upsample_daily_business_daily(self):
expected = ts.asfreq('H', how='s').reindex(exp_rng)
assert_series_equal(result, expected)

def test_resample_business_hourly(self):
# GH12351
rng = pd.date_range(start='2017-05-18 00:00:00',
end='2017-05-19 23:00:00',
freq='H')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are asfreq tests, NOT resample tests.

expected_rng = pd.date_range(start='2017-05-18 00:00:00',
end='2017-05-19 23:00:00',
freq='BH')
ts = Series(1, index=rng)
result = ts.asfreq('BH')
expected_ts = Series(1, index=expected_rng)

assert_series_equal(result, expected_ts)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test against a CustomBusinessHour (I've never used one, but can help figure it out if you need).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just added it. Please take a look if it's okay.

def test_resample_custom_business_hourly(self):
# GH12351
rng = pd.date_range(start='2017-05-18 00:00:00',
end='2017-05-19 23:00:00',
freq='H')

hours = offsets.CustomBusinessHour(start='10:00')
expected_rng = pd.date_range(start='2017-05-18 00:00:00',
end='2017-05-19 23:00:00',
freq=hours)

ts = Series(1, index=rng)
result = ts.asfreq(hours)
expected_ts = Series(1, index=expected_rng)

assert_series_equal(result, expected_ts)

def test_resample_irregular_sparse(self):
dr = date_range(start='1/1/2012', freq='5min', periods=1000)
s = Series(np.array(100), index=dr)
Expand Down