Skip to content

Commit 81a4531

Browse files
author
Younggun Kim
committed
BUG: resample by BusinessHour raises ValueError
closes pandas-dev#12351
1 parent 49ec31b commit 81a4531

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Plotting
102102
Groupby/Resample/Rolling
103103
^^^^^^^^^^^^^^^^^^^^^^^^
104104

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

106107

107108
Sparse

pandas/core/resample.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from pandas.tseries.frequencies import to_offset, is_subperiod, is_superperiod
1414
from pandas.core.indexes.datetimes import DatetimeIndex, date_range
1515
from pandas.core.indexes.timedeltas import TimedeltaIndex
16-
from pandas.tseries.offsets import DateOffset, Tick, Day, _delta_to_nanoseconds
16+
from pandas.tseries.offsets import (DateOffset, Tick, Day, _delta_to_nanoseconds,
17+
BusinessHour, CustomBusinessHour)
1718
from pandas.core.indexes.period import PeriodIndex, period_range
1819
import pandas.core.common as com
1920
import pandas.core.algorithms as algos
@@ -1271,8 +1272,11 @@ def _get_range_edges(first, last, offset, closed='left', base=0):
12711272
if (is_day and day_nanos % offset.nanos == 0) or not is_day:
12721273
return _adjust_dates_anchored(first, last, offset,
12731274
closed=closed, base=base)
1274-
1275-
if not isinstance(offset, Tick): # and first.time() != last.time():
1275+
# GH12351
1276+
elif isinstance(offset, BusinessHour) or isinstance(offset, CustomBusinessHour):
1277+
first = offset.rollback(first)
1278+
last = offset.rollforward(last + offset)
1279+
else:
12761280
# hack!
12771281
first = first.normalize()
12781282
last = last.normalize()

pandas/tests/test_resample.py

+16
Original file line numberDiff line numberDiff line change
@@ -2564,6 +2564,22 @@ def test_upsample_daily_business_daily(self):
25642564
expected = ts.asfreq('H', how='s').reindex(exp_rng)
25652565
assert_series_equal(result, expected)
25662566

2567+
2568+
# GH12351
2569+
def test_resample_business_hourly(self):
2570+
rng = pd.date_range(start='2017-05-18 00:00:00',
2571+
end='2017-05-19 23:00:00',
2572+
freq='H')
2573+
expected_rng = pd.date_range(start='2017-05-18 00:00:00',
2574+
end='2017-05-19 23:00:00',
2575+
freq='BH')
2576+
ts = Series(1, index=rng)
2577+
result = ts.asfreq('BH')
2578+
expected_ts = Series(1, index=expected_rng)
2579+
2580+
assert_series_equal(result, expected_ts)
2581+
2582+
25672583
def test_resample_irregular_sparse(self):
25682584
dr = date_range(start='1/1/2012', freq='5min', periods=1000)
25692585
s = Series(np.array(100), index=dr)

0 commit comments

Comments
 (0)