Skip to content

BUG: DatetimeTz shift raises AmbiguousTimeError near DST #13926

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,7 @@ Bug Fixes
- Clean some compile time warnings in datetime parsing (:issue:`13607`)
- Bug in ``factorize`` raises ``AmbiguousTimeError`` if data contains datetime near DST boundary (:issue:`13750`)
- Bug in ``.set_index`` raises ``AmbiguousTimeError`` if new index contains DST boundary and multi levels (:issue:`12920`)
- Bug in ``.shift`` raises ``AmbiguousTimeError`` if data contains datetime near DST boundary (:issue:`13926`)
- Bug in ``pd.read_hdf()`` returns incorrect result when a ``DataFrame`` with a ``categorical`` column and a query which doesn't match any values (:issue:`13792`)
- Bug in ``pd.to_datetime()`` raise ``AttributeError`` with NaN and the other string is not valid when errors='ignore' (:issue:`12424`)

Expand Down
5 changes: 2 additions & 3 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2438,15 +2438,14 @@ def shift(self, periods, axis=0, mgr=None):
else:
indexer[:periods] = np.arange(-periods, N)

# move to UTC & take
new_values = self.values.tz_localize(None).asi8.take(indexer)
new_values = self.values.asi8.take(indexer)

if periods > 0:
new_values[:periods] = tslib.iNaT
else:
new_values[periods:] = tslib.iNaT

new_values = DatetimeIndex(new_values, tz=self.values.tz)
new_values = self.values._shallow_copy(new_values)
return [self.make_block_same_class(new_values,
placement=self.mgr_locs)]

Expand Down
29 changes: 28 additions & 1 deletion pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy as np

from pandas import Index, Series, date_range
from pandas import Index, Series, date_range, NaT
from pandas.tseries.index import DatetimeIndex
from pandas.tseries.tdi import TimedeltaIndex

Expand Down Expand Up @@ -93,6 +93,33 @@ def test_shift(self):
tz='CET'), name='foo')
self.assertRaises(ValueError, lambda: s - s2)

def test_shift_dst(self):
# GH 13926
dates = date_range('2016-11-06', freq='H', periods=10, tz='US/Eastern')
s = Series(dates)

res = s.shift(0)
tm.assert_series_equal(res, s)
self.assertEqual(res.dtype, 'datetime64[ns, US/Eastern]')

res = s.shift(1)
exp_vals = [NaT] + dates.asobject.values.tolist()[:9]
exp = Series(exp_vals)
tm.assert_series_equal(res, exp)
self.assertEqual(res.dtype, 'datetime64[ns, US/Eastern]')

res = s.shift(-2)
exp_vals = dates.asobject.values.tolist()[2:] + [NaT, NaT]
exp = Series(exp_vals)
tm.assert_series_equal(res, exp)
self.assertEqual(res.dtype, 'datetime64[ns, US/Eastern]')

for ex in [10, -10, 20, -20]:
res = s.shift(ex)
exp = Series([NaT] * 10, dtype='datetime64[ns, US/Eastern]')
tm.assert_series_equal(res, exp)
self.assertEqual(res.dtype, 'datetime64[ns, US/Eastern]')

def test_tshift(self):
# PeriodIndex
ps = tm.makePeriodSeries()
Expand Down