Skip to content

Commit

Permalink
BUG/ENH: Timestamp.strptime (pandas-dev#25124)
Browse files Browse the repository at this point in the history
* BUG: constructor Timestamp.strptime() does not support %z.

* Add doc string to NaT and Timestamp

* updated the error message

* Updated whatsnew entry.
  • Loading branch information
saurav-chakravorty authored and Pingviinituutti committed Feb 28, 2019
1 parent aa9fae1 commit 8068dcb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion pandas/_libs/tslibs/nattype.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,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)
Expand All @@ -388,6 +387,14 @@ class NaTType(_NaT):
# The remaining methods have docstrings copy/pasted from the analogous
# Timestamp methods.

strptime = _make_error_func('strptime', # noqa:E128
"""
Timestamp.strptime(string, format)
Function is not implemented. Use pd.to_datetime().
"""
)

utcfromtimestamp = _make_error_func('utcfromtimestamp', # noqa:E128
"""
Timestamp.utcfromtimestamp(ts)
Expand Down
11 changes: 11 additions & 0 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,17 @@ class Timestamp(_Timestamp):
"""
return cls(datetime.fromtimestamp(ts))

# Issue 25016.
@classmethod
def strptime(cls, date_string, format):
"""
Timestamp.strptime(string, format)
Function is not implemented. Use pd.to_datetime().
"""
raise NotImplementedError("Timestamp.strptime() is not implmented."
"Use to_datetime() to parse date strings.")

@classmethod
def combine(cls, date, time):
"""
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 8068dcb

Please sign in to comment.