Skip to content

fix roundtripping zero-size timedelta arrays #10313

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 7 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
More speaking variable, add whats-new.rst
  • Loading branch information
kmuehlbauer committed May 14, 2025
commit 3d1c91c76587439f40bd54c85f9ca1cf004428f7
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ Bug fixes
~~~~~~~~~
- Fix :py:class:`~xarray.groupers.BinGrouper` when ``labels`` is not specified (:issue:`10284`).
By `Deepak Cherian <https://github.com/dcherian>`_.

- Allow accessing arbitrary attributes on Pandas ExtensionArrays.
By `Deepak Cherian <https://github.com/dcherian>`_.
- Fix coding empty (zero-size) timedelta64 arrays, ``units`` taking precedence when encoding, fallback to default values when decoding.
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.


Documentation
Expand Down
12 changes: 7 additions & 5 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,12 @@ def decode_cf_timedelta(
num_timedeltas = to_numpy(num_timedeltas)
unit = _netcdf_to_numpy_timeunit(units)

# special case empty arrays
is_empty_array = num_timedeltas.size == 0

with warnings.catch_warnings():
warnings.filterwarnings("ignore", "All-NaN slice encountered", RuntimeWarning)
if num_timedeltas.size > 0:
if not is_empty_array:
_check_timedelta_range(np.nanmin(num_timedeltas), unit, time_unit)
_check_timedelta_range(np.nanmax(num_timedeltas), unit, time_unit)

Expand All @@ -670,14 +673,13 @@ def decode_cf_timedelta(
)
pd_timedeltas = pd.to_timedelta(ravel(timedeltas))

if timedeltas.size > 0 and np.isnat(timedeltas).all():
if not is_empty_array and np.isnat(timedeltas).all():
empirical_unit = time_unit
else:
empirical_unit = pd_timedeltas.unit

if (
np.timedelta64(1, time_unit) > np.timedelta64(1, empirical_unit)
or timedeltas.size == 0
if is_empty_array or np.timedelta64(1, time_unit) > np.timedelta64(
1, empirical_unit
):
time_unit = empirical_unit

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy