Skip to content

Commit

Permalink
BUG: resample by BusinessHour raises ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
Younggun Kim committed May 23, 2017
1 parent 49ec31b commit 81a4531
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Plotting
Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^

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


Sparse
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
BusinessHour, CustomBusinessHour)
from pandas.core.indexes.period import PeriodIndex, period_range
import pandas.core.common as com
import pandas.core.algorithms as algos
Expand Down Expand Up @@ -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 isinstance(offset, BusinessHour) or isinstance(offset, CustomBusinessHour):
first = offset.rollback(first)
last = offset.rollforward(last + offset)
else:
# hack!
first = first.normalize()
last = last.normalize()
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 81a4531

Please sign in to comment.