Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Correctly weekly resample over DST #22941

Merged
merged 7 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ Timezones
- Bug in :meth:`DatetimeIndex.unique` that did not re-localize tz-aware dates correctly (:issue:`21737`)
- Bug when indexing a :class:`Series` with a DST transition (:issue:`21846`)
- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` where an ``AmbiguousTimeError`` or ``NonExistentTimeError`` would raise if a timezone aware timeseries ended on a DST transition (:issue:`19375`, :issue:`10117`)
- Bug in :meth:`DataFrame.resample` and :meth:`Series.resample` when resampling by a weekly offset (``'W'``) across a DST transition (:issue:`9119`, :issue:`21459`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move to the resample section


Offsets
^^^^^^^
Expand Down
16 changes: 10 additions & 6 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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, Nano)
from pandas.core.indexes.period import PeriodIndex
from pandas.errors import AbstractMethodError
import pandas.core.algorithms as algos
Expand Down Expand Up @@ -1395,18 +1396,21 @@ def _get_time_bins(self, ax):
def _adjust_bin_edges(self, binner, ax_values):
# Some hacks for > daily data, see #1471, #1458, #1483

bin_edges = binner.asi8

if self.freq != 'D' and is_superperiod(self.freq, 'D'):
day_nanos = delta_to_nanoseconds(timedelta(1))
if self.closed == 'right':
bin_edges = bin_edges + day_nanos - 1
# GH 21459, GH 9119: Adjust the bins relative to the wall time
bin_edges = binner.tz_localize(None)
bin_edges = bin_edges + timedelta(1) - Nano(1)
bin_edges = bin_edges.tz_localize(binner.tz).asi8
else:
bin_edges = binner.asi8

# intraday values on last day
if bin_edges[-2] > ax_values.max():
bin_edges = bin_edges[:-1]
binner = binner[:-1]

else:
bin_edges = binner.asi8
return binner, bin_edges

def _get_time_delta_bins(self, ax):
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -2114,6 +2114,28 @@ def test_downsample_across_dst(self):
freq='H'))
tm.assert_series_equal(result, expected)

def test_downsample_across_dst_weekly(self):
# GH 9119, GH 21459
df = DataFrame(index=DatetimeIndex([
'2017-03-25', '2017-03-26', '2017-03-27',
'2017-03-28', '2017-03-29'
], tz='Europe/Amsterdam'),
data=[11, 12, 13, 14, 15])
result = df.resample('1W').sum()
expected = DataFrame([23, 42], index=pd.DatetimeIndex([
'2017-03-26', '2017-04-02'
], tz='Europe/Amsterdam'))
tm.assert_frame_equal(result, expected)

idx = pd.date_range("2013-04-01", "2013-05-01", tz='Europe/London',
freq='H')
s = Series(index=idx)
result = s.resample('W').mean()
expected = Series(index=pd.date_range(
'2013-04-07', freq='W', periods=5, tz='Europe/London'
))
tm.assert_series_equal(result, expected)

def test_resample_with_nat(self):
# GH 13020
index = DatetimeIndex([pd.NaT,
Expand Down