Skip to content

Commit

Permalink
use dropna to drop NaT from timedelta before checking integer dtype c…
Browse files Browse the repository at this point in the history
…ompatibility
  • Loading branch information
kmuehlbauer committed May 8, 2023
1 parent 2cce9f6 commit b23851f
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions xarray/coding/times.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,10 @@ def encode_cf_datetime(
time_deltas = dates_as_index - ref_date

# Use floor division if time_delta evenly divides all differences
# to preserve integer dtype if possible (GH 4045).
if np.all(time_deltas % time_delta == np.timedelta64(0, "ns")):
# to preserve integer dtype if possible (GH 4045)
# NaT prevents us from using datetime64 directly, but we can safely coerce
# to int64 in presence of NaT, so we just dropna before check.
if np.all(time_deltas.dropna() % time_delta == np.timedelta64(0, "ns")):
num = time_deltas // time_delta
else:
num = time_deltas / time_delta
Expand Down

0 comments on commit b23851f

Please sign in to comment.