Skip to content

handle DST appropriately in Timestamp.replace #18618

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

Merged
merged 16 commits into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
cfb760c
handle DST appropriately in Timestamp.replace
jbrockmendel Dec 4, 2017
4ca60fd
better comment, move test to test_timezones
jbrockmendel Dec 4, 2017
f738989
informative comment in test
jbrockmendel Dec 5, 2017
98a7296
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Dec 6, 2017
6f36a97
handle DST appropriately in Timestamp.replace
jbrockmendel Dec 4, 2017
b916b17
better comment, move test to test_timezones
jbrockmendel Dec 4, 2017
b00d763
informative comment in test
jbrockmendel Dec 5, 2017
000f6cd
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Dec 6, 2017
ec9b38a
Merge branch 'replace_bug' of https://github.com/jbrockmendel/pandas …
jbrockmendel Dec 6, 2017
f5886e2
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Dec 7, 2017
4cfd949
fix duplicate whatsnews caused by merge mixups
jbrockmendel Dec 7, 2017
a3b14f9
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Dec 14, 2017
504cf97
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Dec 29, 2017
4459c9f
fixup merge messup
jbrockmendel Dec 29, 2017
1eb5f2f
Merge branch 'master' of https://github.com/pandas-dev/pandas into re…
jbrockmendel Dec 30, 2017
dced361
refactor test to apply to both pytz and dateutil
jbrockmendel Jan 4, 2018
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.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,4 @@ Other
^^^^^

- Improved error message when attempting to use a Python keyword as an identifier in a ``numexpr`` backed query (:issue:`18221`)
- :func:`Timestamp.replace` will now handle Daylight Savings transitions gracefully (:issue:`18319`)
16 changes: 13 additions & 3 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ from np_datetime cimport (reverse_ops, cmp_scalar, check_dts_bounds,
is_leapyear)
from timedeltas import Timedelta
from timedeltas cimport delta_to_nanoseconds
from timezones cimport get_timezone, is_utc, maybe_get_tz
from timezones cimport get_timezone, is_utc, maybe_get_tz, treat_tz_as_pytz

# ----------------------------------------------------------------------
# Constants
Expand Down Expand Up @@ -922,8 +922,18 @@ class Timestamp(_Timestamp):
_tzinfo = tzinfo

# reconstruct & check bounds
ts_input = datetime(dts.year, dts.month, dts.day, dts.hour, dts.min,
dts.sec, dts.us, tzinfo=_tzinfo)
if _tzinfo is not None and treat_tz_as_pytz(_tzinfo):
# replacing across a DST boundary may induce a new tzinfo object
# see GH#18319
ts_input = _tzinfo.localize(datetime(dts.year, dts.month, dts.day,
dts.hour, dts.min, dts.sec,
dts.us))
_tzinfo = ts_input.tzinfo
else:
ts_input = datetime(dts.year, dts.month, dts.day,
dts.hour, dts.min, dts.sec, dts.us,
tzinfo=_tzinfo)

ts = convert_datetime_to_tsobject(ts_input, _tzinfo)
value = ts.value + (dts.ps // 1000)
if value != NPY_NAT:
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/tseries/test_timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def tzstr(self, tz):
def localize(self, tz, x):
return tz.localize(x)

def normalize(self, ts):
tzinfo = ts.tzinfo
return tzinfo.normalize(ts)

def cmptz(self, tz1, tz2):
# Compare two timezones. Overridden in subclass to parameterize
# tests.
Expand Down Expand Up @@ -935,6 +939,27 @@ def test_datetimeindex_tz_nat(self):
assert isna(idx[1])
assert idx[0].tzinfo is not None

def test_replace_across_dst(self):
# GH#18319 check that 1) timezone is correctly normalized and
# 2) that hour is not incorrectly changed by this normalization
tz = self.tz('US/Eastern')

ts_naive = Timestamp('2017-12-03 16:03:30')
ts_aware = self.localize(tz, ts_naive)

# Preliminary sanity-check
assert ts_aware == self.normalize(ts_aware)

# Replace across DST boundary
ts2 = ts_aware.replace(month=6)

# Check that `replace` preserves hour literal
assert (ts2.hour, ts2.minute) == (ts_aware.hour, ts_aware.minute)

# Check that post-replace object is appropriately normalized
ts2b = self.normalize(ts2)
assert ts2 == ts2b


class TestTimeZoneSupportDateutil(TestTimeZoneSupportPytz):

Expand All @@ -959,6 +984,10 @@ def cmptz(self, tz1, tz2):
def localize(self, tz, x):
return x.replace(tzinfo=tz)

def normalize(self, ts):
# no-op for dateutil
return ts

@td.skip_if_windows
def test_utc_with_system_utc(self):
from pandas._libs.tslibs.timezones import maybe_get_tz
Expand Down