Skip to content

Commit fd91d3f

Browse files
BUG/ENH: Timestamp.strptime
1 parent e3b0950 commit fd91d3f

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

doc/source/whatsnew/v0.24.2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Bug Fixes
6161

6262
**Timezones**
6363

64-
-
64+
- Bug in :meth:`Timestamp.strptime` - support %z. (:issue:`21257`)
6565
-
6666
-
6767

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ class Timestamp(_Timestamp):
684684
object freq=None, tz=None, unit=None,
685685
year=None, month=None, day=None,
686686
hour=None, minute=None, second=None, microsecond=None,
687-
nanosecond=None, tzinfo=None):
687+
tzinfo=None, nanosecond=None):
688688
# The parameter list folds together legacy parameter names (the first
689689
# four) and positional and keyword parameter names from pydatetime.
690690
#
@@ -740,8 +740,8 @@ class Timestamp(_Timestamp):
740740
# microsecond[, nanosecond[, tzinfo]]]]]])
741741
ts_input = datetime(ts_input, freq, tz, unit or 0,
742742
year or 0, month or 0, day or 0)
743-
nanosecond = hour
744-
tz = minute
743+
nanosecond = minute
744+
tz = hour
745745
freq = None
746746

747747
if getattr(ts_input, 'tzinfo', None) is not None and tz is not None:

pandas/tests/scalar/timestamp/test_timestamp.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,20 @@ def test_constructor_with_stringoffset(self):
335335
assert repr(result) == expected
336336
assert result == eval(repr(result))
337337

338+
# GH25016
339+
# Test support for Timestamp.strptime
340+
fmt = '%Y%m%d-%H%M%S-%f%z'
341+
ts = '20190129-235348-000001+0000'
342+
result = Timestamp.strptime(ts, fmt)
343+
expected = Timestamp(2019, 1, 29, 23, 53, 48, 1, pytz.UTC)
344+
assert result == expected
345+
346+
fmt = '%Y%m%d-%H%M%S-%f'
347+
ts = '20190129-235348-000001'
348+
result = Timestamp.strptime(ts, fmt)
349+
expected = Timestamp(2019, 1, 29, 23, 53, 48, 1)
350+
assert result == expected
351+
338352
def test_constructor_invalid(self):
339353
with pytest.raises(TypeError, match='Cannot convert input'):
340354
Timestamp(slice(2))
@@ -433,8 +447,8 @@ def test_constructor_fromordinal(self):
433447
microsecond=6, nanosecond=1),
434448
Timestamp(year=2000, month=1, day=2, hour=3, minute=4, second=5,
435449
microsecond=6, nanosecond=1, tz='UTC'),
436-
Timestamp(2000, 1, 2, 3, 4, 5, 6, 1, None),
437-
Timestamp(2000, 1, 2, 3, 4, 5, 6, 1, pytz.UTC)])
450+
Timestamp(2000, 1, 2, 3, 4, 5, 6, None, 1),
451+
Timestamp(2000, 1, 2, 3, 4, 5, 6, pytz.UTC, 1)])
438452
def test_constructor_nanosecond(self, result):
439453
# GH 18898
440454
expected = Timestamp(datetime(2000, 1, 2, 3, 4, 5, 6), tz=result.tz)

0 commit comments

Comments
 (0)