Skip to content

Commit 697a9a2

Browse files
committed
Add typecasting to prevent numpy 1.26.4 issue
1 parent caae886 commit 697a9a2

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

cf_xarray/helpers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,17 @@ def _get_core_dim_orders(core_dim_coords: dict[str, np.ndarray]) -> dict[str, st
222222

223223
for dim, coords in core_dim_coords.items():
224224
diffs = np.diff(coords)
225-
nonzero_diffs = diffs[diffs != 0]
225+
226+
# NOTE: This is added to prevent an issue with numpy 1.26.4 and Python
227+
# 3.11: numpy.core._exceptions._UFuncInputCastingError:
228+
# Cannot cast ufunc 'not_equal' input 0 from dtype('<m8[ns]') to dtype('<m8')
229+
# with casting rule 'same_kind'
230+
if np.issubdtype(coords.dtype, np.datetime64):
231+
zero = np.timedelta64(0, "ns")
232+
else:
233+
zero = 0
234+
235+
nonzero_diffs = diffs[diffs != zero]
226236

227237
if nonzero_diffs.size == 0:
228238
# All values are equal, treat as ascending

0 commit comments

Comments
 (0)