Skip to content

Commit e213399

Browse files
BUG: Support %z in Timestamp
1 parent 5c40cf2 commit e213399

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/source/whatsnew/v0.24.1.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Enhancements
3636

3737
Bug Fixes
3838
~~~~~~~~~
39-
39+
- Bug in :meth: `Timestamp` supporting %z (:issue:`21257`).
4040
**Conversion**
4141

4242
-

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ from numpy cimport int64_t, int32_t, int8_t
1010
cnp.import_array()
1111

1212
from datetime import time as datetime_time, timedelta
13+
from datetime import timezone
1314
from cpython.datetime cimport (datetime,
1415
PyDateTime_Check, PyDelta_Check, PyTZInfo_Check,
1516
PyDateTime_IMPORT)
@@ -736,8 +737,13 @@ class Timestamp(_Timestamp):
736737
# microsecond[, nanosecond[, tzinfo]]]]]])
737738
ts_input = datetime(ts_input, freq, tz, unit or 0,
738739
year or 0, month or 0, day or 0)
739-
nanosecond = hour
740-
tz = minute
740+
if isinstance(hour, timezone):
741+
print("hello")
742+
nanosecond = minute
743+
tz = hour
744+
else:
745+
nanosecond = hour
746+
tz = minute
741747
freq = None
742748

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

pandas/tests/scalar/timestamp/test_timestamp.py

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

338+
# GH25016
339+
# Test support for zone offset with microsecond
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-01-29 23:53:48.000001+0000', tz='UTC')"
344+
assert repr(result) == expected
345+
338346
def test_constructor_invalid(self):
339347
with pytest.raises(TypeError, match='Cannot convert input'):
340348
Timestamp(slice(2))

0 commit comments

Comments
 (0)