Skip to content

Commit 8efd1a0

Browse files
ahcubjreback
authored andcommitted
!I fix for BUG: resample with tz-aware: Values falls after last bin #15549 (#18337)
1 parent 509e03c commit 8efd1a0

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

doc/source/whatsnew/v0.21.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Bug Fixes
6262
- Bug in ``pd.Series.rolling.skew()`` and ``rolling.kurt()`` with all equal values has floating issue (:issue:`18044`)
6363
- Bug in ``pd.DataFrameGroupBy.count()`` when counting over a datetimelike column (:issue:`13393`)
6464
- Bug in ``pd.concat`` when empty and non-empty DataFrames or Series are concatenated (:issue:`18178` :issue:`18187`)
65+
- Bug in ``DataFrame.resample(...)`` when there is a time change (DST) and resampling frequecy is 12h or higher (:issue:`15549`)
6566
- Bug in :class:`IntervalIndex` constructor when a list of intervals is passed with non-default ``closed`` (:issue:`18334`)
6667
- Bug in :meth:`IntervalIndex.copy` when copying and ``IntervalIndex`` with non-default ``closed`` (:issue:`18339`)
6768

pandas/core/resample.py

+10
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,16 @@ def _get_time_bins(self, ax):
11411141
tz=tz,
11421142
name=ax.name)
11431143

1144+
# GH 15549
1145+
# In edge case of tz-aware resapmling binner last index can be
1146+
# less than the last variable in data object, this happens because of
1147+
# DST time change
1148+
if len(binner) > 1 and binner[-1] < last:
1149+
extra_date_range = pd.date_range(binner[-1], last + self.freq,
1150+
freq=self.freq, tz=tz,
1151+
name=ax.name)
1152+
binner = labels = binner.append(extra_date_range[1:])
1153+
11441154
# a little hack
11451155
trimmed = False
11461156
if (len(binner) > 2 and binner[-2] == last and

pandas/tests/test_resample.py

+28
Original file line numberDiff line numberDiff line change
@@ -2719,6 +2719,34 @@ def test_resample_weekly_bug_1726(self):
27192719
# it works!
27202720
df.resample('W-MON', closed='left', label='left').first()
27212721

2722+
def test_resample_with_dst_time_change(self):
2723+
# GH 15549
2724+
index = pd.DatetimeIndex([1457537600000000000, 1458059600000000000],
2725+
tz='UTC').tz_convert('America/Chicago')
2726+
df = pd.DataFrame([1, 2], index=index)
2727+
result = df.resample('12h', closed='right',
2728+
label='right').last().ffill()
2729+
2730+
expected_index_values = ['2016-03-09 12:00:00-06:00',
2731+
'2016-03-10 00:00:00-06:00',
2732+
'2016-03-10 12:00:00-06:00',
2733+
'2016-03-11 00:00:00-06:00',
2734+
'2016-03-11 12:00:00-06:00',
2735+
'2016-03-12 00:00:00-06:00',
2736+
'2016-03-12 12:00:00-06:00',
2737+
'2016-03-13 00:00:00-06:00',
2738+
'2016-03-13 13:00:00-05:00',
2739+
'2016-03-14 01:00:00-05:00',
2740+
'2016-03-14 13:00:00-05:00',
2741+
'2016-03-15 01:00:00-05:00',
2742+
'2016-03-15 13:00:00-05:00']
2743+
index = pd.DatetimeIndex(expected_index_values,
2744+
tz='UTC').tz_convert('America/Chicago')
2745+
expected = pd.DataFrame([1.0, 1.0, 1.0, 1.0, 1.0,
2746+
1.0, 1.0, 1.0, 1.0, 1.0,
2747+
1.0, 1.0, 2.0], index=index)
2748+
assert_frame_equal(result, expected)
2749+
27222750
def test_resample_bms_2752(self):
27232751
# GH2753
27242752
foo = Series(index=pd.bdate_range('20000101', '20000201'))

0 commit comments

Comments
 (0)