From dcaf678c2395c351e3a679fd247d146078e9e428 Mon Sep 17 00:00:00 2001 From: Sujeito Operator Date: Fri, 14 Aug 2026 13:17:53 +0200 Subject: [PATCH] Avoid the deprecated generic timedelta unit in timedelta conversion sum() starts from the integer 0, so the first addition in maybe_convert_python_timedelta_to_numpy is `0 + np.timedelta64(...)` -- numpy's deprecated 'generic' timedelta unit. Raised from inside parcels.*, the "error:::parcels.*" filter escalates it and the except clause reports it as `Could not convert to np.timedelta64`. functools.reduce(operator.add, dts) adds the parts to each other with no integer start value, and reproduces the existing result units exactly -- a typed start for sum() would promote timedelta64[D] to the start's unit. Turns three existing tests in tests/utils/test_time.py green on numpy 2.5.2. Reported in #2824. --- src/parcels/_core/utils/time.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/parcels/_core/utils/time.py b/src/parcels/_core/utils/time.py index 8a20bd1256..8566bab6cf 100644 --- a/src/parcels/_core/utils/time.py +++ b/src/parcels/_core/utils/time.py @@ -1,5 +1,7 @@ from __future__ import annotations +import functools +import operator from datetime import datetime, timedelta from typing import TYPE_CHECKING, Literal, TypeVar, cast @@ -182,7 +184,10 @@ def maybe_convert_python_timedelta_to_numpy(dt: timedelta | np.timedelta64) -> n dts.append(np.timedelta64(value, np_unit)) if dts: - return sum(dts) + # Not `sum`: it starts from the integer 0, and `0 + np.timedelta64(...)` uses + # numpy's deprecated 'generic' timedelta unit. Adding the parts to each other + # keeps the same result unit without an integer start value. + return functools.reduce(operator.add, dts) else: return np.timedelta64(0, "s") except Exception as e: