From 233a63a13aef283e35b3e33ef2442a7f8841a0ff Mon Sep 17 00:00:00 2001 From: Marius Date: Fri, 18 Dec 2020 09:02:56 +1100 Subject: [PATCH 1/6] Add basic tests of isoformat() --- pandas/tests/scalar/timestamp/test_formats.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 pandas/tests/scalar/timestamp/test_formats.py diff --git a/pandas/tests/scalar/timestamp/test_formats.py b/pandas/tests/scalar/timestamp/test_formats.py new file mode 100644 index 0000000000000..7b1e8688e9861 --- /dev/null +++ b/pandas/tests/scalar/timestamp/test_formats.py @@ -0,0 +1,12 @@ +import pytest + +from pandas import Timestamp + + +def test_isoformat(): + ts = Timestamp(year=2019, month=5, day=18, hour=15, minute=17, + second=8, microsecond=132263) + assert ts.isoformat() == '2019-05-18T15:17:08.132263' + assert ts.isoformat(timespec='seconds') == '2019-05-18T15:17:08' + + From f8b0c4b4d79576e6bf04686458a4b3aaecab233b Mon Sep 17 00:00:00 2001 From: Marius Date: Fri, 18 Dec 2020 09:33:44 +1100 Subject: [PATCH 2/6] Add timespec argument for Timestamp --- pandas/_libs/tslibs/timestamps.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 242eb89d1e723..1ce820d7a96bd 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -609,8 +609,8 @@ cdef class _Timestamp(ABCTimestamp): # ----------------------------------------------------------------- # Rendering Methods - def isoformat(self, sep: str = "T") -> str: - base = super(_Timestamp, self).isoformat(sep=sep) + def isoformat(self, sep: str = "T", timespec: str = "auto") -> str: + base = super(_Timestamp, self).isoformat(sep=sep, timespec=timespec) if self.nanosecond == 0: return base From 43341329b0b2fe2e760901400f1787715a15efb3 Mon Sep 17 00:00:00 2001 From: Marius Date: Fri, 18 Dec 2020 09:34:34 +1100 Subject: [PATCH 3/6] Only add nanoseconds for relevant timespecs --- pandas/_libs/tslibs/timestamps.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 1ce820d7a96bd..9e0b9e3dd674a 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -619,7 +619,7 @@ cdef class _Timestamp(ABCTimestamp): else: base1, base2 = base, "" - if self.microsecond != 0: + if self.microsecond != 0 and timespec in ("auto", "microseconds"): base1 += f"{self.nanosecond:03d}" else: base1 += f".{self.nanosecond:09d}" From 972ce2bb33d5d8c9bccae39e7d0bc5a129d6de89 Mon Sep 17 00:00:00 2001 From: Marius Date: Fri, 18 Dec 2020 09:41:02 +1100 Subject: [PATCH 4/6] Reformat with black --- pandas/tests/scalar/timestamp/test_formats.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/tests/scalar/timestamp/test_formats.py b/pandas/tests/scalar/timestamp/test_formats.py index 7b1e8688e9861..2975967301f81 100644 --- a/pandas/tests/scalar/timestamp/test_formats.py +++ b/pandas/tests/scalar/timestamp/test_formats.py @@ -4,9 +4,8 @@ def test_isoformat(): - ts = Timestamp(year=2019, month=5, day=18, hour=15, minute=17, - second=8, microsecond=132263) - assert ts.isoformat() == '2019-05-18T15:17:08.132263' - assert ts.isoformat(timespec='seconds') == '2019-05-18T15:17:08' - - + ts = Timestamp( + year=2019, month=5, day=18, hour=15, minute=17, second=8, microsecond=132263 + ) + assert ts.isoformat() == "2019-05-18T15:17:08.132263" + assert ts.isoformat(timespec="seconds") == "2019-05-18T15:17:08" From 1a508aafd3c97040897ed675ae148bde3535ffc3 Mon Sep 17 00:00:00 2001 From: Marius Date: Fri, 18 Dec 2020 09:42:50 +1100 Subject: [PATCH 5/6] Don't need explicit pytest import --- pandas/tests/scalar/timestamp/test_formats.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tests/scalar/timestamp/test_formats.py b/pandas/tests/scalar/timestamp/test_formats.py index 2975967301f81..e291df4a15e55 100644 --- a/pandas/tests/scalar/timestamp/test_formats.py +++ b/pandas/tests/scalar/timestamp/test_formats.py @@ -1,5 +1,3 @@ -import pytest - from pandas import Timestamp From 575c283ce855ba90184007c0c119bed7efc7ecb8 Mon Sep 17 00:00:00 2001 From: Marius Date: Fri, 18 Dec 2020 09:49:47 +1100 Subject: [PATCH 6/6] Add to whatsnew --- doc/source/whatsnew/v1.2.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index e2521cedb64cc..8625f94bf39c0 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -607,6 +607,7 @@ Datetimelike - Bug in :meth:`Series.isin` with ``datetime64[ns]`` dtype and :meth:`.DatetimeIndex.isin` failing to consider timezone-aware and timezone-naive datetimes as always different (:issue:`35728`) - Bug in :meth:`Series.isin` with ``PeriodDtype`` dtype and :meth:`PeriodIndex.isin` failing to consider arguments with different ``PeriodDtype`` as always different (:issue:`37528`) - Bug in :class:`Period` constructor now correctly handles nanoseconds in the ``value`` argument (:issue:`34621` and :issue:`17053`) +- Bug in :meth:`Timestamp.isoformat`, now handles the ``timespec`` argument from the base :class:``datetime`` class (:issue:`26131`) Timedelta ^^^^^^^^^