Skip to content

Commit

Permalink
Preserve formatting of reference time units under pandas 2.0.0 (#7441)
Browse files Browse the repository at this point in the history
* [test-upstream] Preserve formatting of reference time units

Changes in pandas 2.0.0 interfere with the way we expect some times
to be formatted.

* Use strftime to force expected date format

Co-authored-by: Justus Magin <keewis@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [test-upstream] Preserve formatting of reference time units

Changes in pandas 2.0.0 interfere with the way we expect some times
to be formatted.

* Use strftime to force expected date format

Co-authored-by: Justus Magin <keewis@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [test-upstream] Remove old comment and move what's new entry to latest version

* [test-upstream] remove old comment and move what's new entry

* Update xarray/core/formatting.py

* Use isoformat() now that pandas zero-pads years

Co-authored-by: Justus Magin <keewis@users.noreply.github.com>

* [test-upstream] update what's new

* replace the `isoformat` separator with `" "` [test-upstream]

* use the `sep` parameter to `isoformat` instead

Co-authored-by: Spencer Clark <spencerkclark@gmail.com>

* Update doc/whats-new.rst

---------

Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
Co-authored-by: Justus Magin <keewis@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Justus Magin <keewis@posteo.de>
  • Loading branch information
5 people authored Apr 1, 2023
1 parent 1c81162 commit 84607c3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ Bug fixes
By `Thomas Coleman <https://github.com/ColemanTom>`_.
- Proper plotting when passing :py:class:`~matplotlib.colors.BoundaryNorm` type argument in :py:meth:`DataArray.plot`. (:issue:`4061`, :issue:`7014`,:pull:`7553`)
By `Jelmer Veenstra <https://github.com/veenstrajelmer>`_.
- Ensure the formatting of time encoding reference dates outside the range of
nanosecond-precision datetimes remains the same under pandas version 2.0.0
(:issue:`7420`, :pull:`7441`).
By `Justus Magin <https://github.com/keewis>`_ and
`Spencer Clark <https://github.com/spencerkclark>`_.


Documentation
~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def calc_max_rows_last(max_rows: int) -> int:

def format_timestamp(t):
"""Cast given object to a Timestamp and return a nicely formatted string"""
# Timestamp is only valid for 1678 to 2262
try:
datetime_str = str(pd.Timestamp(t))
timestamp = pd.Timestamp(t)
datetime_str = timestamp.isoformat(sep=" ")
except OutOfBoundsDatetime:
datetime_str = str(t)

Expand Down

0 comments on commit 84607c3

Please sign in to comment.