Skip to content

Commit 94e868a

Browse files
authored
Fix ns precision isoformat (#53073)
* fix #53020 * add tests #53020
1 parent a37451f commit 94e868a

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

pandas/_libs/tslibs/timestamps.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ cdef class _Timestamp(ABCTimestamp):
10261026
base1, base2 = base, ""
10271027

10281028
if timespec == "nanoseconds" or (timespec == "auto" and self.nanosecond):
1029-
if self.microsecond:
1029+
if self.microsecond or timespec == "nanoseconds":
10301030
base1 += f"{self.nanosecond:03d}"
10311031
else:
10321032
base1 += f".{self.nanosecond:09d}"

pandas/tests/tslibs/test_parse_iso8601.py

+44
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,47 @@ def test_parsers_iso8601_leading_space():
7373
date_str, expected = ("2013-1-1 5:30:00", datetime(2013, 1, 1, 5, 30))
7474
actual = tslib._test_parse_iso8601(" " * 200 + date_str)
7575
assert actual == expected
76+
77+
78+
@pytest.mark.parametrize(
79+
"date_str, timespec, exp",
80+
[
81+
("2023-01-01 00:00:00", "auto", "2023-01-01T00:00:00"),
82+
("2023-01-01 00:00:00", "seconds", "2023-01-01T00:00:00"),
83+
("2023-01-01 00:00:00", "milliseconds", "2023-01-01T00:00:00.000"),
84+
("2023-01-01 00:00:00", "microseconds", "2023-01-01T00:00:00.000000"),
85+
("2023-01-01 00:00:00", "nanoseconds", "2023-01-01T00:00:00.000000000"),
86+
("2023-01-01 00:00:00.001", "auto", "2023-01-01T00:00:00.001000"),
87+
("2023-01-01 00:00:00.001", "seconds", "2023-01-01T00:00:00"),
88+
("2023-01-01 00:00:00.001", "milliseconds", "2023-01-01T00:00:00.001"),
89+
("2023-01-01 00:00:00.001", "microseconds", "2023-01-01T00:00:00.001000"),
90+
("2023-01-01 00:00:00.001", "nanoseconds", "2023-01-01T00:00:00.001000000"),
91+
("2023-01-01 00:00:00.000001", "auto", "2023-01-01T00:00:00.000001"),
92+
("2023-01-01 00:00:00.000001", "seconds", "2023-01-01T00:00:00"),
93+
("2023-01-01 00:00:00.000001", "milliseconds", "2023-01-01T00:00:00.000"),
94+
("2023-01-01 00:00:00.000001", "microseconds", "2023-01-01T00:00:00.000001"),
95+
("2023-01-01 00:00:00.000001", "nanoseconds", "2023-01-01T00:00:00.000001000"),
96+
("2023-01-01 00:00:00.000000001", "auto", "2023-01-01T00:00:00.000000001"),
97+
("2023-01-01 00:00:00.000000001", "seconds", "2023-01-01T00:00:00"),
98+
("2023-01-01 00:00:00.000000001", "milliseconds", "2023-01-01T00:00:00.000"),
99+
("2023-01-01 00:00:00.000000001", "microseconds", "2023-01-01T00:00:00.000000"),
100+
(
101+
"2023-01-01 00:00:00.000000001",
102+
"nanoseconds",
103+
"2023-01-01T00:00:00.000000001",
104+
),
105+
("2023-01-01 00:00:00.000001001", "auto", "2023-01-01T00:00:00.000001001"),
106+
("2023-01-01 00:00:00.000001001", "seconds", "2023-01-01T00:00:00"),
107+
("2023-01-01 00:00:00.000001001", "milliseconds", "2023-01-01T00:00:00.000"),
108+
("2023-01-01 00:00:00.000001001", "microseconds", "2023-01-01T00:00:00.000001"),
109+
(
110+
"2023-01-01 00:00:00.000001001",
111+
"nanoseconds",
112+
"2023-01-01T00:00:00.000001001",
113+
),
114+
],
115+
)
116+
def test_iso8601_formatter(date_str: str, timespec: str, exp: str):
117+
# GH#53020
118+
ts = Timestamp(date_str)
119+
assert ts.isoformat(timespec=timespec) == exp

0 commit comments

Comments
 (0)