Skip to content

Commit

Permalink
Fix ns precision isoformat (#53073)
Browse files Browse the repository at this point in the history
* fix #53020

* add tests #53020
  • Loading branch information
i1oveMyse1f committed May 9, 2023
1 parent a37451f commit 94e868a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ cdef class _Timestamp(ABCTimestamp):
base1, base2 = base, ""

if timespec == "nanoseconds" or (timespec == "auto" and self.nanosecond):
if self.microsecond:
if self.microsecond or timespec == "nanoseconds":
base1 += f"{self.nanosecond:03d}"
else:
base1 += f".{self.nanosecond:09d}"
Expand Down
44 changes: 44 additions & 0 deletions pandas/tests/tslibs/test_parse_iso8601.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,47 @@ def test_parsers_iso8601_leading_space():
date_str, expected = ("2013-1-1 5:30:00", datetime(2013, 1, 1, 5, 30))
actual = tslib._test_parse_iso8601(" " * 200 + date_str)
assert actual == expected


@pytest.mark.parametrize(
"date_str, timespec, exp",
[
("2023-01-01 00:00:00", "auto", "2023-01-01T00:00:00"),
("2023-01-01 00:00:00", "seconds", "2023-01-01T00:00:00"),
("2023-01-01 00:00:00", "milliseconds", "2023-01-01T00:00:00.000"),
("2023-01-01 00:00:00", "microseconds", "2023-01-01T00:00:00.000000"),
("2023-01-01 00:00:00", "nanoseconds", "2023-01-01T00:00:00.000000000"),
("2023-01-01 00:00:00.001", "auto", "2023-01-01T00:00:00.001000"),
("2023-01-01 00:00:00.001", "seconds", "2023-01-01T00:00:00"),
("2023-01-01 00:00:00.001", "milliseconds", "2023-01-01T00:00:00.001"),
("2023-01-01 00:00:00.001", "microseconds", "2023-01-01T00:00:00.001000"),
("2023-01-01 00:00:00.001", "nanoseconds", "2023-01-01T00:00:00.001000000"),
("2023-01-01 00:00:00.000001", "auto", "2023-01-01T00:00:00.000001"),
("2023-01-01 00:00:00.000001", "seconds", "2023-01-01T00:00:00"),
("2023-01-01 00:00:00.000001", "milliseconds", "2023-01-01T00:00:00.000"),
("2023-01-01 00:00:00.000001", "microseconds", "2023-01-01T00:00:00.000001"),
("2023-01-01 00:00:00.000001", "nanoseconds", "2023-01-01T00:00:00.000001000"),
("2023-01-01 00:00:00.000000001", "auto", "2023-01-01T00:00:00.000000001"),
("2023-01-01 00:00:00.000000001", "seconds", "2023-01-01T00:00:00"),
("2023-01-01 00:00:00.000000001", "milliseconds", "2023-01-01T00:00:00.000"),
("2023-01-01 00:00:00.000000001", "microseconds", "2023-01-01T00:00:00.000000"),
(
"2023-01-01 00:00:00.000000001",
"nanoseconds",
"2023-01-01T00:00:00.000000001",
),
("2023-01-01 00:00:00.000001001", "auto", "2023-01-01T00:00:00.000001001"),
("2023-01-01 00:00:00.000001001", "seconds", "2023-01-01T00:00:00"),
("2023-01-01 00:00:00.000001001", "milliseconds", "2023-01-01T00:00:00.000"),
("2023-01-01 00:00:00.000001001", "microseconds", "2023-01-01T00:00:00.000001"),
(
"2023-01-01 00:00:00.000001001",
"nanoseconds",
"2023-01-01T00:00:00.000001001",
),
],
)
def test_iso8601_formatter(date_str: str, timespec: str, exp: str):
# GH#53020
ts = Timestamp(date_str)
assert ts.isoformat(timespec=timespec) == exp

0 comments on commit 94e868a

Please sign in to comment.