Skip to content

Commit b2c7519

Browse files
saurav-chakravortymroeschke
authored andcommitted
BUG/ENH: Timestamp.strptime (#25124)
* BUG: constructor Timestamp.strptime() does not support %z. * Add doc string to NaT and Timestamp * updated the error message * Updated whatsnew entry.
1 parent f9cb581 commit b2c7519

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Other Enhancements
2828
Backwards incompatible API changes
2929
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3030

31+
- :meth:`Timestamp.strptime` will now rise a NotImplementedError (:issue:`21257`)
32+
3133
.. _whatsnew_0250.api.other:
3234

3335
Other API Changes

pandas/_libs/tslibs/nattype.pyx

+8-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ class NaTType(_NaT):
374374
utctimetuple = _make_error_func('utctimetuple', datetime)
375375
timetz = _make_error_func('timetz', datetime)
376376
timetuple = _make_error_func('timetuple', datetime)
377-
strptime = _make_error_func('strptime', datetime)
378377
strftime = _make_error_func('strftime', datetime)
379378
isocalendar = _make_error_func('isocalendar', datetime)
380379
dst = _make_error_func('dst', datetime)
@@ -388,6 +387,14 @@ class NaTType(_NaT):
388387
# The remaining methods have docstrings copy/pasted from the analogous
389388
# Timestamp methods.
390389

390+
strptime = _make_error_func('strptime', # noqa:E128
391+
"""
392+
Timestamp.strptime(string, format)
393+
394+
Function is not implemented. Use pd.to_datetime().
395+
"""
396+
)
397+
391398
utcfromtimestamp = _make_error_func('utcfromtimestamp', # noqa:E128
392399
"""
393400
Timestamp.utcfromtimestamp(ts)

pandas/_libs/tslibs/timestamps.pyx

+11
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,17 @@ class Timestamp(_Timestamp):
697697
"""
698698
return cls(datetime.fromtimestamp(ts))
699699

700+
# Issue 25016.
701+
@classmethod
702+
def strptime(cls, date_string, format):
703+
"""
704+
Timestamp.strptime(string, format)
705+
706+
Function is not implemented. Use pd.to_datetime().
707+
"""
708+
raise NotImplementedError("Timestamp.strptime() is not implmented."
709+
"Use to_datetime() to parse date strings.")
710+
700711
@classmethod
701712
def combine(cls, date, time):
702713
"""

pandas/tests/scalar/timestamp/test_timestamp.py

+8
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ def test_constructor_invalid_tz(self):
355355
# interpreted as a `freq`
356356
Timestamp('2012-01-01', 'US/Pacific')
357357

358+
def test_constructor_strptime(self):
359+
# GH25016
360+
# Test support for Timestamp.strptime
361+
fmt = '%Y%m%d-%H%M%S-%f%z'
362+
ts = '20190129-235348-000001+0000'
363+
with pytest.raises(NotImplementedError):
364+
Timestamp.strptime(ts, fmt)
365+
358366
def test_constructor_tz_or_tzinfo(self):
359367
# GH#17943, GH#17690, GH#5168
360368
stamps = [Timestamp(year=2017, month=10, day=22, tz='UTC'),

0 commit comments

Comments
 (0)