Skip to content

Commit 0a3e582

Browse files
committed
BUG: fix BH resampling (GH12351)
close pandas-dev#12351 xref pandas-dev#13364
1 parent 19fc8da commit 0a3e582

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

pandas/core/resample.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
SeriesGroupBy, groupby, PanelGroupBy)
1111

1212
from pandas.tseries.frequencies import to_offset, is_subperiod, is_superperiod
13-
from pandas.core.indexes.datetimes import DatetimeIndex, date_range
14-
from pandas.core.indexes.timedeltas import TimedeltaIndex
15-
from pandas.tseries.offsets import DateOffset, Tick, Day, _delta_to_nanoseconds
16-
from pandas.core.indexes.period import PeriodIndex, period_range
13+
from pandas.tseries.index import DatetimeIndex, date_range
14+
from pandas.tseries.tdi import TimedeltaIndex
15+
from pandas.tseries.offsets import DateOffset, Tick, Day, BusinessHour, _delta_to_nanoseconds
16+
from pandas.tseries.period import PeriodIndex, period_range
1717
import pandas.core.common as com
1818
import pandas.core.algorithms as algos
1919

@@ -1016,7 +1016,7 @@ def __init__(self, freq='Min', closed=None, label=None, how='mean',
10161016
convention=None, base=0, **kwargs):
10171017
freq = to_offset(freq)
10181018

1019-
end_types = set(['M', 'A', 'Q', 'BM', 'BA', 'BQ', 'W'])
1019+
end_types = set(['M', 'A', 'Q', 'BH', 'B', 'BM', 'BA', 'BQ', 'W'])
10201020
rule = freq.rule_code
10211021
if (rule in end_types or
10221022
('-' in rule and rule[:rule.find('-')] in end_types)):
@@ -1298,7 +1298,8 @@ def _get_range_edges(first, last, offset, closed='left', base=0):
12981298
return _adjust_dates_anchored(first, last, offset,
12991299
closed=closed, base=base)
13001300

1301-
if not isinstance(offset, Tick): # and first.time() != last.time():
1301+
# GH12351
1302+
if not isinstance(offset, Tick) and not isinstance(offset, BusinessHour):
13021303
# hack!
13031304
first = first.normalize()
13041305
last = last.normalize()

pandas/tests/test_resample.py

+20
Original file line numberDiff line numberDiff line change
@@ -2572,6 +2572,26 @@ def test_upsample_daily_business_daily(self):
25722572
expected = ts.asfreq('H', how='s').reindex(exp_rng)
25732573
assert_series_equal(result, expected)
25742574

2575+
'''
2576+
def test_resample_business_hourly(self):
2577+
rng = pd.date_range(start='2016-02-11 00:00:00',
2578+
end='2016-02-12 23:00:00',
2579+
freq='H')
2580+
expected_rng = pd.date_range(start='2016-02-11 00:00:00',
2581+
end='2016-02-12 23:00:00',
2582+
freq='H')
2583+
df = pd.DataFrame(index=rng)
2584+
2585+
df.resample('BH').count()
2586+
'''
2587+
2588+
def test_resample_empty(self):
2589+
ts = _simple_pts('1/1/2000', '2/1/2000')[:0]
2590+
2591+
result = ts.resample('A').asfreq()
2592+
self.assertEqual(len(result), 0)
2593+
2594+
25752595
def test_resample_irregular_sparse(self):
25762596
dr = date_range(start='1/1/2012', freq='5min', periods=1000)
25772597
s = Series(np.array(100), index=dr)

pandas/tseries/frequencies.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ def is_subperiod(source, target):
11141114
return source in ['C', 'H', 'T', 'S', 'L', 'U', 'N']
11151115
elif target == 'D':
11161116
return source in ['D', 'H', 'T', 'S', 'L', 'U', 'N']
1117-
elif target == 'H':
1117+
elif _is_hourly(source):
11181118
return source in ['H', 'T', 'S', 'L', 'U', 'N']
11191119
elif target == 'T':
11201120
return source in ['T', 'S', 'L', 'U', 'N']
@@ -1170,7 +1170,7 @@ def is_superperiod(source, target):
11701170
return target in ['D', 'C', 'B', 'H', 'T', 'S', 'L', 'U', 'N']
11711171
elif source == 'D':
11721172
return target in ['D', 'C', 'B', 'H', 'T', 'S', 'L', 'U', 'N']
1173-
elif source == 'H':
1173+
elif _is_hourly(source):
11741174
return target in ['H', 'T', 'S', 'L', 'U', 'N']
11751175
elif source == 'T':
11761176
return target in ['T', 'S', 'L', 'U', 'N']
@@ -1213,6 +1213,11 @@ def _is_weekly(rule):
12131213
return rule == 'W' or rule.startswith('W-')
12141214

12151215

1216+
def _is_hourly(rule):
1217+
rule = rule.upper()
1218+
return rule == 'H' or rule.startswith('BH')
1219+
1220+
12161221
DAYS = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']
12171222

12181223
MONTHS = tslib._MONTHS

0 commit comments

Comments
 (0)