Skip to content

Commit 9f2e453

Browse files
jsantanderjreback
authored andcommitted
BUG: Avoid AmbiguousTimeError on groupby
closes #14682 Author: Julian Santander <[email protected]> Author: Julian Santander <[email protected]> Closes #14683 from j-santander/master and squashes the following commits: d90afaf [Julian Santander] Addressing additional code inspection comments 817ed97 [Julian Santander] Addressing code inspections comments 99a5367 [Julian Santander] Fix unittest error and lint warning 940fb22 [Julian Santander] Avoid AmbiguousTimeError on groupby
1 parent f862b52 commit 9f2e453

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

doc/source/whatsnew/v0.19.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Bug Fixes
4949
- Compat with python 3.6 for some indexing exception types (:issue:`14684`, :issue:`14689`)
5050
- Compat with python 3.6 for deprecation warnings in the test suite (:issue:`14681`)
5151
- Compat with python 3.6 for Timestamp pickles (:issue:`14689`)
52+
- Bug in resampling a ``DatetimeIndex`` in local TZ, covering a DST change, which would raise ``AmbiguousTimeError`` (:issue:`14682`)
5253

5354

5455

pandas/tseries/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def _generate(cls, start, end, periods, name, offset,
439439
tz = tz.localize(date.replace(tzinfo=None)).tzinfo
440440

441441
if tz is not None and inferred_tz is not None:
442-
if not inferred_tz == tz:
442+
if not tslib.get_timezone(inferred_tz) == tslib.get_timezone(tz):
443443
raise AssertionError("Inferred time zone not equal to passed "
444444
"time zone")
445445

pandas/tseries/resample.py

100644100755
+11-5
Original file line numberDiff line numberDiff line change
@@ -1283,9 +1283,18 @@ def _adjust_dates_anchored(first, last, offset, closed='right', base=0):
12831283
#
12841284
# See https://github.com/pandas-dev/pandas/issues/8683
12851285

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.
12861290
first_tzinfo = first.tzinfo
1291+
last_tzinfo = last.tzinfo
1292+
first_dst = bool(first.dst())
1293+
last_dst = bool(last.dst())
1294+
12871295
first = first.tz_localize(None)
12881296
last = last.tz_localize(None)
1297+
12891298
start_day_nanos = first.normalize().value
12901299

12911300
base_nanos = (base % offset.n) * offset.nanos // offset.n
@@ -1320,11 +1329,8 @@ def _adjust_dates_anchored(first, last, offset, closed='right', base=0):
13201329
else:
13211330
lresult = last.value + offset.nanos
13221331

1323-
# return (Timestamp(fresult, tz=first.tz),
1324-
# Timestamp(lresult, tz=last.tz))
1325-
1326-
return (Timestamp(fresult).tz_localize(first_tzinfo),
1327-
Timestamp(lresult).tz_localize(first_tzinfo))
1332+
return (Timestamp(fresult).tz_localize(first_tzinfo, ambiguous=first_dst),
1333+
Timestamp(lresult).tz_localize(last_tzinfo, ambiguous=last_dst))
13281334

13291335

13301336
def asfreq(obj, freq, method=None, how=None, normalize=False):

pandas/tseries/tests/test_resample.py

100644100755
+27-1
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,33 @@ def test_resample_size(self):
19121912
right = Series(val, index=ix)
19131913
assert_series_equal(left, right)
19141914

1915-
def test_resmaple_dst_anchor(self):
1915+
def test_resample_across_dst(self):
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
1923+
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
1930+
df2 = DataFrame([1477785600, 1477789200], columns=['ts'])
1931+
dti2 = DatetimeIndex(pd.to_datetime(df2.ts, unit='s')
1932+
.dt.tz_localize('UTC')
1933+
.dt.tz_convert('Europe/Madrid'))
1934+
df = DataFrame([5, 5], index=dti1)
1935+
1936+
result = df.resample(rule='H').sum()
1937+
expected = DataFrame([5, 5], index=dti2)
1938+
1939+
assert_frame_equal(result, expected)
1940+
1941+
def test_resample_dst_anchor(self):
19161942
# 5172
19171943
dti = DatetimeIndex([datetime(2012, 11, 4, 23)], tz='US/Eastern')
19181944
df = DataFrame([5], index=dti)

0 commit comments

Comments
 (0)