Skip to content

Commit 2d3be12

Browse files
committed
BUG: resample by BusinessHour raises ValueError
closes pandas-dev#12351
1 parent 103f7d3 commit 2d3be12

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ Bug Fixes
341341
- Bug in ``.resample(..)`` with a ``PeriodIndex`` not retaining its type or name with an empty ``DataFrame``appropriately when empty (:issue:`13212`)
342342
- Bug in ``groupby(..).resample(..)`` where passing some keywords would raise an exception (:issue:`13235`)
343343
- Bug in ``.tz_convert`` on a tz-aware ``DateTimeIndex`` that relied on index being sorted for correct results (:issue: `13306`)
344+
- Bug in ``.resample(..)`` with a ``BusinessHour`` raises ``ValueError`` (:issue:`12351`)
344345

345346

346347

pandas/tseries/resample.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from pandas.tseries.frequencies import to_offset, is_subperiod, is_superperiod
1313
from pandas.tseries.index import DatetimeIndex, date_range
1414
from pandas.tseries.tdi import TimedeltaIndex
15-
from pandas.tseries.offsets import DateOffset, Tick, Day, _delta_to_nanoseconds
15+
from pandas.tseries.offsets import (DateOffset, Tick, Day, BusinessHour,
16+
_delta_to_nanoseconds)
1617
from pandas.tseries.period import PeriodIndex, period_range
1718
import pandas.core.common as com
1819
import pandas.core.algorithms as algos
@@ -1213,8 +1214,13 @@ def _get_range_edges(first, last, offset, closed='left', base=0):
12131214
if (is_day and day_nanos % offset.nanos == 0) or not is_day:
12141215
return _adjust_dates_anchored(first, last, offset,
12151216
closed=closed, base=base)
1217+
elif isinstance(offset, BusinessHour):
1218+
# GH12351 - normalize BH freq leads ValueError
1219+
first = Timestamp(offset.rollback(first))
1220+
last = Timestamp(offset.rollforward(last + offset))
1221+
return first, last
12161222

1217-
if not isinstance(offset, Tick): # and first.time() != last.time():
1223+
else: # and first.time() != last.time():
12181224
# hack!
12191225
first = first.normalize()
12201226
last = last.normalize()

pandas/tseries/tests/test_resample.py

+11
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,17 @@ def test_upsample_daily_business_daily(self):
22972297
expected = ts.asfreq('H', how='s').reindex(exp_rng)
22982298
assert_series_equal(result, expected)
22992299

2300+
def test_resample_hourly_business_hourly(self):
2301+
ts = pd.Series(index=pd.date_range(start='2016-06-01 03:00:00',
2302+
end='2016-06-03 23:00:00',
2303+
freq='H'))
2304+
expected = pd.Series(index=pd.date_range(start='2016-05-31 17:00:00',
2305+
end='2016-06-06 09:00:00',
2306+
freq='BH'))
2307+
2308+
result = ts.resample('BH').mean()
2309+
assert_series_equal(result, expected)
2310+
23002311
def test_resample_irregular_sparse(self):
23012312
dr = date_range(start='1/1/2012', freq='5min', periods=1000)
23022313
s = Series(np.array(100), index=dr)

0 commit comments

Comments
 (0)