Skip to content

Commit 578275d

Browse files
authored
BUG: zero-pad shorter years in Timestamp.isoformat (#52220)
* add tests checking that non-4 digit years isoformat properly * zero-pad the year to 4 digits * adapt the constructor consistency tests * remove the constructor error message since that should not be raised anymore
1 parent 6cae820 commit 578275d

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

pandas/_libs/tslibs/timestamps.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ cdef class _Timestamp(ABCTimestamp):
10151015
base_ts = "microseconds" if timespec == "nanoseconds" else timespec
10161016
base = super(_Timestamp, self).isoformat(sep=sep, timespec=base_ts)
10171017
# We need to replace the fake year 1970 with our real year
1018-
base = f"{self.year}-" + base.split("-", 1)[1]
1018+
base = f"{self.year:04d}-" + base.split("-", 1)[1]
10191019

10201020
if self.nanosecond == 0 and timespec != "nanoseconds":
10211021
return base

pandas/tests/scalar/timestamp/test_constructors.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -599,21 +599,13 @@ def test_bounds_with_different_units(self):
599599
@pytest.mark.parametrize("arg", ["001-01-01", "0001-01-01"])
600600
def test_out_of_bounds_string_consistency(self, arg):
601601
# GH 15829
602-
msg = "|".join(
603-
[
604-
"Cannot cast 1-01-01 00:00:00 to unit='ns' without overflow",
605-
"Out of bounds nanosecond timestamp: 1-01-01 00:00:00",
606-
]
607-
)
602+
msg = "Cannot cast 0001-01-01 00:00:00 to unit='ns' without overflow"
608603
with pytest.raises(OutOfBoundsDatetime, match=msg):
609604
Timestamp(arg).as_unit("ns")
610605

611-
if arg == "0001-01-01":
612-
# only the 4-digit year goes through ISO path which gets second reso
613-
# instead of ns reso
614-
ts = Timestamp(arg)
615-
assert ts.unit == "s"
616-
assert ts.year == ts.month == ts.day == 1
606+
ts = Timestamp(arg)
607+
assert ts.unit == "s"
608+
assert ts.year == ts.month == ts.day == 1
617609

618610
def test_min_valid(self):
619611
# Ensure that Timestamp.min is a valid Timestamp

pandas/tests/scalar/timestamp/test_formats.py

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
second=8,
1212
microsecond=132263,
1313
)
14+
ts_no_ns_year1 = Timestamp(
15+
year=1,
16+
month=5,
17+
day=18,
18+
hour=15,
19+
minute=17,
20+
second=8,
21+
microsecond=132263,
22+
)
1423
ts_ns = Timestamp(
1524
year=2019,
1625
month=5,
@@ -50,6 +59,8 @@
5059
(ts_no_ns, "auto", "2019-05-18T15:17:08.132263"),
5160
(ts_no_ns, "seconds", "2019-05-18T15:17:08"),
5261
(ts_no_ns, "nanoseconds", "2019-05-18T15:17:08.132263000"),
62+
(ts_no_ns_year1, "seconds", "0001-05-18T15:17:08"),
63+
(ts_no_ns_year1, "nanoseconds", "0001-05-18T15:17:08.132263000"),
5364
(ts_ns, "auto", "2019-05-18T15:17:08.132263123"),
5465
(ts_ns, "hours", "2019-05-18T15"),
5566
(ts_ns, "minutes", "2019-05-18T15:17"),

0 commit comments

Comments
 (0)