Skip to content

Commit 0804d1b

Browse files
committed
Fix casting issue with numpy 1.26.4
1 parent 72d6087 commit 0804d1b

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

cf_xarray/helpers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,13 @@ def _is_bounds_strictly_monotonic(arr: np.ndarray) -> bool:
311311
>>> _is_bounds_strictly_monotonic(bounds)
312312
True
313313
"""
314-
diffs = np.diff(arr, axis=-2)
314+
# NOTE: Python 3.10 uses numpy 1.26.4. If the input is a datetime64 array,
315+
# numpy 1.26.4 may raise: numpy.core._exceptions._UFuncInputCastingError:
316+
# Cannot cast ufunc 'greater' input 0 from dtype('<m8[ns]') to dtype('<m8')
317+
# with casting rule 'same_kind' To avoid this, always cast to float64 before
318+
# np.diff.
319+
arr_numeric = arr.astype("float64")
320+
diffs = np.diff(arr_numeric, axis=-2)
315321
strictly_increasing = np.all(diffs > 0, axis=-2)
316322
strictly_decreasing = np.all(diffs < 0, axis=-2)
317323

0 commit comments

Comments
 (0)