From 923507f215dcd56fd221ec7a020a9b6a65ad8da6 Mon Sep 17 00:00:00 2001 From: Younggun Kim Date: Mon, 22 May 2017 18:12:06 -0700 Subject: [PATCH 1/2] BUG: resample by BusinessHour raises ValueError closes #12351 --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/core/resample.py | 10 +++++++--- pandas/tests/test_resample.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 3734dc15be2e9..aade494073233 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -102,6 +102,7 @@ Plotting Groupby/Resample/Rolling ^^^^^^^^^^^^^^^^^^^^^^^^ +- Bug in ``.resample(..)`` with a ``BusinessHour`` raises ``ValueError`` (:issue:`12351`) Sparse diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 631b91c3aad11..482920b4d2bdb 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -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 @@ -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 + elif issubclass(type(offset), BusinessHourMixin): + first = offset.rollback(first) + last = offset.rollforward(last + offset) + else: # hack! first = first.normalize() last = last.normalize() diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py index 37e2fd0e9b188..5d0eb9aa2c9a2 100644 --- a/pandas/tests/test_resample.py +++ b/pandas/tests/test_resample.py @@ -2564,6 +2564,22 @@ def test_upsample_daily_business_daily(self): expected = ts.asfreq('H', how='s').reindex(exp_rng) assert_series_equal(result, expected) + + # GH12351 + def test_resample_business_hourly(self): + rng = pd.date_range(start='2017-05-18 00:00:00', + end='2017-05-19 23:00:00', + freq='H') + 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) + + def test_resample_irregular_sparse(self): dr = date_range(start='1/1/2012', freq='5min', periods=1000) s = Series(np.array(100), index=dr) From 6f03530c5a4e662eb65791730e8b7edd4ea16983 Mon Sep 17 00:00:00 2001 From: Younggun Kim Date: Tue, 23 May 2017 11:20:14 -0700 Subject: [PATCH 2/2] TST: Test CustomBusinessHour resampling (#12351) follow up to GH16447 --- pandas/tests/test_resample.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/pandas/tests/test_resample.py b/pandas/tests/test_resample.py index 5d0eb9aa2c9a2..1dbb1660da8d1 100644 --- a/pandas/tests/test_resample.py +++ b/pandas/tests/test_resample.py @@ -2564,21 +2564,36 @@ def test_upsample_daily_business_daily(self): expected = ts.asfreq('H', how='s').reindex(exp_rng) assert_series_equal(result, expected) - - # GH12351 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') expected_rng = pd.date_range(start='2017-05-18 00:00:00', - end='2017-05-19 23:00:00', - freq='BH') + 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) + 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)