From fae65a481e758b78f0f30ecc283d0632ab553897 Mon Sep 17 00:00:00 2001 From: Saurav Chakravorty Date: Tue, 12 Feb 2019 22:58:52 +0530 Subject: [PATCH 1/4] BUG: constructor Timestamp.strptime() does not support %z. --- doc/source/whatsnew/v0.24.2.rst | 2 +- pandas/_libs/tslibs/timestamps.pyx | 6 ++++++ pandas/tests/scalar/timestamp/test_timestamp.py | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.2.rst b/doc/source/whatsnew/v0.24.2.rst index f17c4974cd450..84e449f6a8f87 100644 --- a/doc/source/whatsnew/v0.24.2.rst +++ b/doc/source/whatsnew/v0.24.2.rst @@ -66,7 +66,7 @@ Bug Fixes **Timezones** -- +- Bug in :meth:`Timestamp.strptime` - support %z. (:issue:`21257`) - - diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 8a95d2494dfa4..9937e14b03a37 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -677,6 +677,12 @@ class Timestamp(_Timestamp): """ return cls(datetime.fromtimestamp(ts)) + # Issue 25016. As strptime with %z is not supported in Python 2. + @classmethod + def strptime(cls, date_string, format): + raise NotImplementedError("Timestamp.strptime() is not implmented." + "Use to_datetime() to parse date strings.") + @classmethod def combine(cls, date, time): """ diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index c27ef3d0662c8..655528abba6dd 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -355,6 +355,14 @@ def test_constructor_invalid_tz(self): # interpreted as a `freq` Timestamp('2012-01-01', 'US/Pacific') + def test_constructor_strptime(self): + # GH25016 + # Test support for Timestamp.strptime + fmt = '%Y%m%d-%H%M%S-%f%z' + ts = '20190129-235348-000001+0000' + with pytest.raises(NotImplementedError): + Timestamp.strptime(ts, fmt) + def test_constructor_tz_or_tzinfo(self): # GH#17943, GH#17690, GH#5168 stamps = [Timestamp(year=2017, month=10, day=22, tz='UTC'), From edb629e0d845b00d45dac2d3dbaa211c69ab950e Mon Sep 17 00:00:00 2001 From: Saurav Chakravorty Date: Wed, 13 Feb 2019 22:49:24 +0530 Subject: [PATCH 2/4] Add doc string to NaT and Timestamp --- pandas/_libs/tslibs/nattype.pyx | 10 +++++++++- pandas/_libs/tslibs/timestamps.pyx | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index b64c3479f23fe..0e9c2d292b87d 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -354,7 +354,6 @@ class NaTType(_NaT): utctimetuple = _make_error_func('utctimetuple', datetime) timetz = _make_error_func('timetz', datetime) timetuple = _make_error_func('timetuple', datetime) - strptime = _make_error_func('strptime', datetime) strftime = _make_error_func('strftime', datetime) isocalendar = _make_error_func('isocalendar', datetime) dst = _make_error_func('dst', datetime) @@ -368,6 +367,15 @@ class NaTType(_NaT): # The remaining methods have docstrings copy/pasted from the analogous # Timestamp methods. + strptime = _make_error_func('strptime', + """ + Timestamp.strptime(string, format) + + Function is not implemented as the behavior of datetime.strptime() + differs in Python 2 and Python 3. + """ + ) + utcfromtimestamp = _make_error_func('utcfromtimestamp', # noqa:E128 """ Timestamp.utcfromtimestamp(ts) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 9937e14b03a37..80d40ec82fb42 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -680,6 +680,12 @@ class Timestamp(_Timestamp): # Issue 25016. As strptime with %z is not supported in Python 2. @classmethod def strptime(cls, date_string, format): + """ + Timestamp.strptime(string, format) + + Function is not implemented as the behavior of datetime.strptime() + differs in Python 2 and Python 3. + """ raise NotImplementedError("Timestamp.strptime() is not implmented." "Use to_datetime() to parse date strings.") From 80413192cfc15c20914e7a5a6b45117be3026c48 Mon Sep 17 00:00:00 2001 From: Saurav Chakravorty Date: Thu, 14 Feb 2019 18:09:21 +0530 Subject: [PATCH 3/4] updated the error message --- pandas/_libs/tslibs/nattype.pyx | 5 ++--- pandas/_libs/tslibs/timestamps.pyx | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pandas/_libs/tslibs/nattype.pyx b/pandas/_libs/tslibs/nattype.pyx index 0e9c2d292b87d..9a317ddaa5a2e 100644 --- a/pandas/_libs/tslibs/nattype.pyx +++ b/pandas/_libs/tslibs/nattype.pyx @@ -367,12 +367,11 @@ class NaTType(_NaT): # The remaining methods have docstrings copy/pasted from the analogous # Timestamp methods. - strptime = _make_error_func('strptime', + strptime = _make_error_func('strptime', # noqa:E128 """ Timestamp.strptime(string, format) - Function is not implemented as the behavior of datetime.strptime() - differs in Python 2 and Python 3. + Function is not implemented. Use pd.to_datetime(). """ ) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 80d40ec82fb42..104a4539e3977 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -677,14 +677,13 @@ class Timestamp(_Timestamp): """ return cls(datetime.fromtimestamp(ts)) - # Issue 25016. As strptime with %z is not supported in Python 2. + # Issue 25016. @classmethod def strptime(cls, date_string, format): """ Timestamp.strptime(string, format) - Function is not implemented as the behavior of datetime.strptime() - differs in Python 2 and Python 3. + Function is not implemented. Use pd.to_datetime(). """ raise NotImplementedError("Timestamp.strptime() is not implmented." "Use to_datetime() to parse date strings.") From 2bb6934a1d2bb86be393217099a01c8834a95c22 Mon Sep 17 00:00:00 2001 From: Saurav Chakravorty Date: Sun, 17 Feb 2019 16:26:08 +0530 Subject: [PATCH 4/4] Updated whatsnew entry. --- doc/source/whatsnew/v0.24.2.rst | 2 +- doc/source/whatsnew/v0.25.0.rst | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.2.rst b/doc/source/whatsnew/v0.24.2.rst index 84e449f6a8f87..f17c4974cd450 100644 --- a/doc/source/whatsnew/v0.24.2.rst +++ b/doc/source/whatsnew/v0.24.2.rst @@ -66,7 +66,7 @@ Bug Fixes **Timezones** -- Bug in :meth:`Timestamp.strptime` - support %z. (:issue:`21257`) +- - - diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 95362521f3b9f..a998f8d7305aa 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -28,6 +28,8 @@ Other Enhancements Backwards incompatible API changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- :meth:`Timestamp.strptime` will now rise a NotImplementedError (:issue:`21257`) + .. _whatsnew_0250.api.other: Other API Changes