Skip to content

Commit 817ed97

Browse files
committed
Addressing code inspections comments
1 parent 99a5367 commit 817ed97

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

doc/source/whatsnew/v0.19.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ Bug Fixes
4343

4444

4545
- Explicit check in ``to_stata`` and ``StataWriter`` for out-of-range values when writing doubles (:issue:`14618`)
46+
- Fix AmbiguousTimeError exception when resampling a DatetimeIndex in local TZ, covering a DST change (:issue:`14682`)

pandas/tseries/resample.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import pandas as pd
77
from pandas.core.base import AbstractMethodError, GroupByMixin
8-
from pandas.core.config_init import pc_ambiguous_as_wide_doc
98

109
from pandas.core.groupby import (BinGrouper, Grouper, _GroupBy, GroupBy,
1110
SeriesGroupBy, groupby, PanelGroupBy)
@@ -1284,6 +1283,10 @@ def _adjust_dates_anchored(first, last, offset, closed='right', base=0):
12841283
#
12851284
# See https://github.com/pandas-dev/pandas/issues/8683
12861285

1286+
# 14682 - Since we need to drop the TZ information to perform
1287+
# the adjustment in the presence of a DST change,
1288+
# save TZ Info and the DST state of the first and last parameters
1289+
# so that we can accurately rebuild them at the end.
12871290
first_tzinfo = first.tzinfo
12881291
last_tzinfo = last.tzinfo
12891292
first_dst = bool(first.dst())

pandas/tseries/tests/test_resample.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -1913,11 +1913,24 @@ def test_resample_size(self):
19131913
assert_series_equal(left, right)
19141914

19151915
def test_resample_across_dst(self):
1916-
#14682
1916+
# The test resamples a DatetimeIndex with values before and after a
1917+
# DST change
1918+
# Issue: 14682
1919+
1920+
# The DatetimeIndex we will start with
1921+
# (note that DST happens at 03:00+02:00 -> 02:00+01:00)
1922+
# 2016-10-30 02:23:00+02:00, 2016-10-30 02:23:00+01:00
19171923
df1 = DataFrame([1477786980, 1477790580], columns=['ts'])
1924+
dti1 = DatetimeIndex(pd.to_datetime(df1.ts, unit='s')
1925+
.dt.tz_localize('UTC')
1926+
.dt.tz_convert('Europe/Madrid'))
1927+
1928+
# The expected DatetimeIndex after resampling.
1929+
# 2016-10-30 02:00:00+02:00, 2016-10-30 02:00:00+01:00
19181930
df2 = DataFrame([1477785600, 1477789200], columns=['ts'])
1919-
dti1 = DatetimeIndex(pd.to_datetime(df1.ts, unit='s').dt.tz_localize('UTC').dt.tz_convert('Europe/Madrid'))
1920-
dti2 = DatetimeIndex(pd.to_datetime(df2.ts, unit='s').dt.tz_localize('UTC').dt.tz_convert('Europe/Madrid'))
1931+
dti2 = DatetimeIndex(pd.to_datetime(df2.ts, unit='s')
1932+
.dt.tz_localize('UTC')
1933+
.dt.tz_convert('Europe/Madrid'))
19211934
df = DataFrame([5, 5], index=dti1)
19221935
assert_frame_equal(
19231936
df.resample(rule='H').sum(),

0 commit comments

Comments
 (0)