Skip to content

BUG: Support timespec argument in Timestamp.isoformat() (#26131) #38550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is tagged as enhancement so this probably should be moved there

Also I would expect this to target v1.3 not v1.2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah pls move this to 1.3 under other enhancements


Timedelta
^^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is 'auto' the typical name for this?

base = super(_Timestamp, self).isoformat(sep=sep, timespec=timespec)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add a doc-string here

if self.nanosecond == 0:
return base

Expand All @@ -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}"
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/scalar/timestamp/test_formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pandas import Timestamp


def test_isoformat():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you locate with with other formatting tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you suggest a more specific location that would be better? I looked, but couldn't find an obvious place where other formatting tests are located.

ts = Timestamp(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this handle a nanosecond argument?

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"