Skip to content

Commit

Permalink
BUG: Avoid AmbiguousTimeError on groupby
Browse files Browse the repository at this point in the history
closes pandas-dev#14682

Author: Julian Santander <julian.santander@nokia.com>
Author: Julian Santander <jsantander2@gmail.com>

Closes pandas-dev#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
  • Loading branch information
jsantander authored and jreback committed Nov 22, 2016
1 parent f862b52 commit 9f2e453
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Bug Fixes
- Compat with python 3.6 for some indexing exception types (:issue:`14684`, :issue:`14689`)
- Compat with python 3.6 for deprecation warnings in the test suite (:issue:`14681`)
- Compat with python 3.6 for Timestamp pickles (:issue:`14689`)
- Bug in resampling a ``DatetimeIndex`` in local TZ, covering a DST change, which would raise ``AmbiguousTimeError`` (:issue:`14682`)



Expand Down
2 changes: 1 addition & 1 deletion pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def _generate(cls, start, end, periods, name, offset,
tz = tz.localize(date.replace(tzinfo=None)).tzinfo

if tz is not None and inferred_tz is not None:
if not inferred_tz == tz:
if not tslib.get_timezone(inferred_tz) == tslib.get_timezone(tz):
raise AssertionError("Inferred time zone not equal to passed "
"time zone")

Expand Down
16 changes: 11 additions & 5 deletions pandas/tseries/resample.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -1283,9 +1283,18 @@ def _adjust_dates_anchored(first, last, offset, closed='right', base=0):
#
# See https://github.com/pandas-dev/pandas/issues/8683

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

first = first.tz_localize(None)
last = last.tz_localize(None)

start_day_nanos = first.normalize().value

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

# return (Timestamp(fresult, tz=first.tz),
# Timestamp(lresult, tz=last.tz))

return (Timestamp(fresult).tz_localize(first_tzinfo),
Timestamp(lresult).tz_localize(first_tzinfo))
return (Timestamp(fresult).tz_localize(first_tzinfo, ambiguous=first_dst),
Timestamp(lresult).tz_localize(last_tzinfo, ambiguous=last_dst))


def asfreq(obj, freq, method=None, how=None, normalize=False):
Expand Down
28 changes: 27 additions & 1 deletion pandas/tseries/tests/test_resample.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -1912,7 +1912,33 @@ def test_resample_size(self):
right = Series(val, index=ix)
assert_series_equal(left, right)

def test_resmaple_dst_anchor(self):
def test_resample_across_dst(self):
# The test resamples a DatetimeIndex with values before and after a
# DST change
# Issue: 14682

# The DatetimeIndex we will start with
# (note that DST happens at 03:00+02:00 -> 02:00+01:00)
# 2016-10-30 02:23:00+02:00, 2016-10-30 02:23:00+01:00
df1 = DataFrame([1477786980, 1477790580], columns=['ts'])
dti1 = DatetimeIndex(pd.to_datetime(df1.ts, unit='s')
.dt.tz_localize('UTC')
.dt.tz_convert('Europe/Madrid'))

# The expected DatetimeIndex after resampling.
# 2016-10-30 02:00:00+02:00, 2016-10-30 02:00:00+01:00
df2 = DataFrame([1477785600, 1477789200], columns=['ts'])
dti2 = DatetimeIndex(pd.to_datetime(df2.ts, unit='s')
.dt.tz_localize('UTC')
.dt.tz_convert('Europe/Madrid'))
df = DataFrame([5, 5], index=dti1)

result = df.resample(rule='H').sum()
expected = DataFrame([5, 5], index=dti2)

assert_frame_equal(result, expected)

def test_resample_dst_anchor(self):
# 5172
dti = DatetimeIndex([datetime(2012, 11, 4, 23)], tz='US/Eastern')
df = DataFrame([5], index=dti)
Expand Down

0 comments on commit 9f2e453

Please sign in to comment.