Skip to content

Commit fc282d5

Browse files
authored
re-add timedelta support for polyval (#6599)
1 parent 6bb2b85 commit fc282d5

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

xarray/core/computation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1933,14 +1933,18 @@ def _ensure_numeric(data: T_Xarray) -> T_Xarray:
19331933
from .dataset import Dataset
19341934

19351935
def to_floatable(x: DataArray) -> DataArray:
1936-
if x.dtype.kind in "mM":
1936+
if x.dtype.kind == "M":
1937+
# datetimes
19371938
return x.copy(
19381939
data=datetime_to_numeric(
19391940
x.data,
19401941
offset=np.datetime64("1970-01-01"),
19411942
datetime_unit="ns",
19421943
),
19431944
)
1945+
elif x.dtype.kind == "m":
1946+
# timedeltas
1947+
return x.astype(float)
19441948
return x
19451949

19461950
if isinstance(data, Dataset):

xarray/tests/test_computation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,14 @@ def test_where_attrs() -> None:
20102010
),
20112011
id="datetime",
20122012
),
2013+
pytest.param(
2014+
xr.DataArray(
2015+
np.array([1000, 2000, 3000], dtype="timedelta64[ns]"), dims="x"
2016+
),
2017+
xr.DataArray([0, 1], dims="degree", coords={"degree": [0, 1]}),
2018+
xr.DataArray([1000.0, 2000.0, 3000.0], dims="x"),
2019+
id="timedelta",
2020+
),
20132021
],
20142022
)
20152023
def test_polyval(

0 commit comments

Comments
 (0)