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

FIX: expand time slice selection for datetime64 #15

Merged
merged 1 commit into from
Jan 29, 2025
Merged
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
16 changes: 10 additions & 6 deletions ilamb3/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@
raise ValueError("Single element conversions only")
return (int(da.dt.year), int(da.dt.month), int(da.dt.day))

def _stamp(t: xr.DataArray, ymd: tuple[int]):
cls = t.item().__class__
try:
stamp = cls(*ymd)
except Exception:
stamp = np.datetime64(f"{ymd[0]:4d}-{ymd[1]:02d}-{ymd[2]:02d}")
return stamp

Check warning on line 142 in ilamb3/compare.py

View check run for this annotation

Codecov / codecov/patch

ilamb3/compare.py#L137-L142

Added lines #L137 - L142 were not covered by tests

# Get the time extents in the original calendars
ta0, taf = dset.get_time_extent(dsa)
tb0, tbf = dset.get_time_extent(dsb)
Expand All @@ -148,12 +156,8 @@
tmax = min(_to_tuple(taf), _to_tuple(tbf))

# Recast back into native calendar objects and select
dsa = dsa.sel(
{"time": slice(ta0.item().__class__(*tmin), taf.item().__class__(*tmax))}
)
dsb = dsb.sel(
{"time": slice(tb0.item().__class__(*tmin), tbf.item().__class__(*tmax))}
)
dsa = dsa.sel({"time": slice(_stamp(ta0, tmin), _stamp(taf, tmax))})
dsb = dsb.sel({"time": slice(_stamp(tb0, tmin), _stamp(tbf, tmax))})

Check warning on line 160 in ilamb3/compare.py

View check run for this annotation

Codecov / codecov/patch

ilamb3/compare.py#L159-L160

Added lines #L159 - L160 were not covered by tests
return dsa, dsb


Expand Down