Skip to content
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

Attempt datetime coding using cftime when pandas fails #6049

Merged
merged 4 commits into from
Dec 24, 2021
Merged
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
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Bug fixes
By `Sebastian Weigand <https://github.com/s-weigand>`_.
- Fix a regression in the removal of duplicate backend entrypoints (:issue:`5944`, :pull:`5959`)
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
- Fix an issue that datasets from being saved when time variables with units that ``cftime`` can parse but pandas can not were present (:pull:`6049`).
By `Tim Heap <https://github.com/mx-moth>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
7 changes: 4 additions & 3 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,9 @@ def _cleanup_netcdf_time_units(units):
delta, ref_date = _unpack_netcdf_time_units(units)
try:
units = "{} since {}".format(delta, format_timestamp(ref_date))
except OutOfBoundsDatetime:
# don't worry about reifying the units if they're out of bounds
except (OutOfBoundsDatetime, ValueError):
# don't worry about reifying the units if they're out of bounds or
# formatted badly
pass
return units

Expand Down Expand Up @@ -482,7 +483,7 @@ def encode_cf_datetime(dates, units=None, calendar=None):
num = time_deltas / time_delta
num = num.values.reshape(dates.shape)

except (OutOfBoundsDatetime, OverflowError):
except (OutOfBoundsDatetime, OverflowError, ValueError):
num = _encode_datetime_with_cftime(dates, units, calendar)

num = cast_to_int_if_safe(num)
Expand Down
17 changes: 17 additions & 0 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,23 @@ def test_encode_cf_datetime_pandas_min() -> None:
assert calendar == expected_calendar


@requires_cftime
def test_encode_cf_datetime_invalid_pandas_valid_cftime() -> None:
num, units, calendar = encode_cf_datetime(
pd.date_range("2000", periods=3),
# Pandas fails to parse this unit, but cftime is quite happy with it
"days since 1970-01-01 00:00:00 00",
"standard",
)

expected_num = [10957, 10958, 10959]
expected_units = "days since 1970-01-01 00:00:00 00"
expected_calendar = "standard"
assert_array_equal(num, expected_num)
assert units == expected_units
assert calendar == expected_calendar


@requires_cftime
def test_time_units_with_timezone_roundtrip(calendar) -> None:
# Regression test for GH 2649
Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ def test_pretty_print(self) -> None:
def test_maybe_truncate(self) -> None:
assert formatting.maybe_truncate("ß", 10) == "ß"

def test_format_timestamp_invalid_pandas_format(self) -> None:
expected = "2021-12-06 17:00:00 00"
with pytest.raises(ValueError):
formatting.format_timestamp(expected)

def test_format_timestamp_out_of_bounds(self) -> None:
from datetime import datetime

Expand Down