Skip to content

Commit

Permalink
ENH: Support fold argument in Timestamp.replace (pandas-dev#25046)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke authored and Pingviinituutti committed Feb 28, 2019
1 parent ec8f5c6 commit ea0410a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ including other versions of pandas.
Other Enhancements
^^^^^^^^^^^^^^^^^^

-
- :meth:`Timestamp.replace` now supports the ``fold`` argument to disambiguate DST transition times (:issue:`25017`)
-
-

Expand Down
1 change: 0 additions & 1 deletion pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,6 @@ class NaTType(_NaT):
nanosecond : int, optional
tzinfo : tz-convertible, optional
fold : int, optional, default is 0
added in 3.6, NotImplemented
Returns
-------
Expand Down
17 changes: 11 additions & 6 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import sys
import warnings

from cpython cimport (PyObject_RichCompareBool, PyObject_RichCompare,
Expand Down Expand Up @@ -43,10 +44,11 @@ from pandas._libs.tslibs.timezones import UTC
# Constants
_zero_time = datetime_time(0, 0)
_no_input = object()

PY36 = sys.version_info >= (3, 6)

# ----------------------------------------------------------------------


def maybe_integer_op_deprecated(obj):
# GH#22535 add/sub of integers and int-arrays is deprecated
if obj.freq is not None:
Expand Down Expand Up @@ -1195,7 +1197,6 @@ class Timestamp(_Timestamp):
nanosecond : int, optional
tzinfo : tz-convertible, optional
fold : int, optional, default is 0
added in 3.6, NotImplemented
Returns
-------
Expand Down Expand Up @@ -1252,12 +1253,16 @@ class Timestamp(_Timestamp):
# see GH#18319
ts_input = _tzinfo.localize(datetime(dts.year, dts.month, dts.day,
dts.hour, dts.min, dts.sec,
dts.us))
dts.us),
is_dst=not bool(fold))
_tzinfo = ts_input.tzinfo
else:
ts_input = datetime(dts.year, dts.month, dts.day,
dts.hour, dts.min, dts.sec, dts.us,
tzinfo=_tzinfo)
kwargs = {'year': dts.year, 'month': dts.month, 'day': dts.day,
'hour': dts.hour, 'minute': dts.min, 'second': dts.sec,
'microsecond': dts.us, 'tzinfo': _tzinfo}
if PY36:
kwargs['fold'] = fold
ts_input = datetime(**kwargs)

ts = convert_datetime_to_tsobject(ts_input, _tzinfo)
value = ts.value + (dts.ps // 1000)
Expand Down
15 changes: 14 additions & 1 deletion pandas/tests/scalar/timestamp/test_unary_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from pandas._libs.tslibs import conversion
from pandas._libs.tslibs.frequencies import INVALID_FREQ_ERR_MSG
from pandas.compat import PY3
from pandas.compat import PY3, PY36
import pandas.util._test_decorators as td

from pandas import NaT, Timestamp
Expand Down Expand Up @@ -329,6 +329,19 @@ def test_replace_dst_border(self):
expected = Timestamp('2013-11-3 03:00:00', tz='America/Chicago')
assert result == expected

@pytest.mark.skipif(not PY36, reason='Fold not available until PY3.6')
@pytest.mark.parametrize('fold', [0, 1])
@pytest.mark.parametrize('tz', ['dateutil/Europe/London', 'Europe/London'])
def test_replace_dst_fold(self, fold, tz):
# GH 25017
d = datetime(2019, 10, 27, 2, 30)
ts = Timestamp(d, tz=tz)
result = ts.replace(hour=1, fold=fold)
expected = Timestamp(datetime(2019, 10, 27, 1, 30)).tz_localize(
tz, ambiguous=not fold
)
assert result == expected

# --------------------------------------------------------------
# Timestamp.normalize

Expand Down

0 comments on commit ea0410a

Please sign in to comment.