diff --git a/doc/source/whatsnew/v0.18.2.txt b/doc/source/whatsnew/v0.18.2.txt index 7493150370e9f..1a701850d205b 100644 --- a/doc/source/whatsnew/v0.18.2.txt +++ b/doc/source/whatsnew/v0.18.2.txt @@ -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`) diff --git a/pandas/tseries/resample.py b/pandas/tseries/resample.py index 8d6955ab43711..451c589cddcd9 100644 --- a/pandas/tseries/resample.py +++ b/pandas/tseries/resample.py @@ -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 @@ -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): + # GH12351 - normalize BH freq leads ValueError + first = Timestamp(offset.rollback(first)) + last = Timestamp(offset.rollforward(last + offset)) + 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() diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py index 2236d20975eee..c089938e05e94 100644 --- a/pandas/tseries/tests/test_resample.py +++ b/pandas/tseries/tests/test_resample.py @@ -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', + 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) + def test_resample_irregular_sparse(self): dr = date_range(start='1/1/2012', freq='5min', periods=1000) s = Series(np.array(100), index=dr)