Skip to content

Allow decoding of size-0 datetimes #6882

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

Merged
merged 5 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -34,6 +34,8 @@ Deprecations
Bug fixes
~~~~~~~~~

- Allow decoding of 0 sized datetimes(:issue:`1329`, :pull:`6882`)
By `Deepak Cherian <https://github.com/dcherian>`_.
- Make sure DataArray.name is always a string when used as label for plotting.
(:issue:`6826`, :pull:`6832`)
By `Jimmy Westling <https://github.com/illviljan>`_.
Expand Down
15 changes: 10 additions & 5 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,12 @@ def _decode_cf_datetime_dtype(data, units, calendar, use_cftime):
def _decode_datetime_with_cftime(num_dates, units, calendar):
if cftime is None:
raise ModuleNotFoundError("No module named 'cftime'")
return np.asarray(
cftime.num2date(num_dates, units, calendar, only_use_cftime_datetimes=True)
)
if num_dates.size > 0:
return np.asarray(
cftime.num2date(num_dates, units, calendar, only_use_cftime_datetimes=True)
)
else:
raise ValueError("Can't decode 0-sized times with cftime.")


def _decode_datetime_with_pandas(flat_num_dates, units, calendar):
Expand All @@ -220,8 +223,10 @@ def _decode_datetime_with_pandas(flat_num_dates, units, calendar):

with warnings.catch_warnings():
warnings.filterwarnings("ignore", "invalid value encountered", RuntimeWarning)
pd.to_timedelta(flat_num_dates.min(), delta) + ref_date
pd.to_timedelta(flat_num_dates.max(), delta) + ref_date
if flat_num_dates.size > 0:
# avoid size 0 datetimes GH1329
pd.to_timedelta(flat_num_dates.min(), delta) + ref_date
pd.to_timedelta(flat_num_dates.max(), delta) + ref_date

# To avoid integer overflow when converting to nanosecond units for integer
# dtypes smaller than np.int64 cast all integer and unsigned integer dtype
Expand Down
22 changes: 22 additions & 0 deletions xarray/tests/test_coding_times.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,3 +1150,25 @@ def test_decode_cf_datetime_uint64_with_cftime_overflow_error():
num_dates = np.uint64(1_000_000 * 86_400 * 360 * 500_000)
with pytest.raises(OverflowError):
decode_cf_datetime(num_dates, units, calendar)


@pytest.mark.parametrize(
"use_cftime", [pytest.mark.skipif(not has_cftime, True), False]
)
def test_decode_0size_datetime(use_cftime):
expected = np.zeros(shape=0, dtype="M8[ns]")
actual = decode_cf_datetime(
np.zeros(shape=0, dtype=np.int64),
units="days since 1970-01-01 00:00:00",
calendar="proleptic_gregorian",
use_cftime=False,
)
np.testing.assert_equal(expected, actual)

with pytest.raises(ValueError):
decode_cf_datetime(
np.zeros(shape=0, dtype=np.int64),
units="days since 1970-01-01 00:00:00",
calendar="proleptic_gregorian",
use_cftime=True,
)