Skip to content

BUG: resample by BusinessHour raises ValueError #13364

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 1 commit 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.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ Bug Fixes
- Bug in ``.resample(..)`` with a ``PeriodIndex`` not retaining its type or name with an empty ``DataFrame``appropriately when empty (:issue:`13212`)
- Bug in ``groupby(..).resample(..)`` where passing some keywords would raise an exception (:issue:`13235`)
- Bug in ``.tz_convert`` on a tz-aware ``DateTimeIndex`` that relied on index being sorted for correct results (:issue: `13306`)
- Bug in ``.resample(..)`` with a ``BusinessHour`` raises ``ValueError`` (:issue:`12351`)



Expand Down
10 changes: 8 additions & 2 deletions pandas/tseries/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from pandas.tseries.frequencies import to_offset, is_subperiod, is_superperiod
from pandas.tseries.index import DatetimeIndex, date_range
from pandas.tseries.tdi import TimedeltaIndex
from pandas.tseries.offsets import DateOffset, Tick, Day, _delta_to_nanoseconds
from pandas.tseries.offsets import (DateOffset, Tick, Day, BusinessHour,
_delta_to_nanoseconds)
from pandas.tseries.period import PeriodIndex, period_range
import pandas.core.common as com
import pandas.core.algorithms as algos
Expand Down Expand Up @@ -1213,8 +1214,13 @@ 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)
elif isinstance(offset, BusinessHour):
Copy link
Member

Choose a reason for hiding this comment

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

can u check CustomBusinessHour can be supported by the same logic?

# GH12351 - normalize BH freq leads ValueError
first = Timestamp(offset.rollback(first))
last = Timestamp(offset.rollforward(last + offset))
Copy link
Member

Choose a reason for hiding this comment

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

isn't rollback/rollforward returns Timestamp?

return first, last

if not isinstance(offset, Tick): # and first.time() != last.time():
else: # and first.time() != last.time():
# hack!
first = first.normalize()
last = last.normalize()
Expand Down
11 changes: 11 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,17 @@ def test_upsample_daily_business_daily(self):
expected = ts.asfreq('H', how='s').reindex(exp_rng)
assert_series_equal(result, expected)

def test_resample_hourly_business_hourly(self):
ts = pd.Series(index=pd.date_range(start='2016-06-01 03:00:00',
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number here as a comment

end='2016-06-03 23:00:00',
freq='H'))
expected = pd.Series(index=pd.date_range(start='2016-05-31 17:00:00',
end='2016-06-06 09:00:00',
freq='BH'))

result = ts.resample('BH').mean()
assert_series_equal(result, expected)
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure this is right. shouldn't the result have BH freq?


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