Skip to content

Commit

Permalink
re-add timedelta support for polyval (#6599)
Browse files Browse the repository at this point in the history
  • Loading branch information
headtr1ck authored May 12, 2022
1 parent 6bb2b85 commit fc282d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion xarray/core/computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1933,14 +1933,18 @@ def _ensure_numeric(data: T_Xarray) -> T_Xarray:
from .dataset import Dataset

def to_floatable(x: DataArray) -> DataArray:
if x.dtype.kind in "mM":
if x.dtype.kind == "M":
# datetimes
return x.copy(
data=datetime_to_numeric(
x.data,
offset=np.datetime64("1970-01-01"),
datetime_unit="ns",
),
)
elif x.dtype.kind == "m":
# timedeltas
return x.astype(float)
return x

if isinstance(data, Dataset):
Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_computation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2010,6 +2010,14 @@ def test_where_attrs() -> None:
),
id="datetime",
),
pytest.param(
xr.DataArray(
np.array([1000, 2000, 3000], dtype="timedelta64[ns]"), dims="x"
),
xr.DataArray([0, 1], dims="degree", coords={"degree": [0, 1]}),
xr.DataArray([1000.0, 2000.0, 3000.0], dims="x"),
id="timedelta",
),
],
)
def test_polyval(
Expand Down

0 comments on commit fc282d5

Please sign in to comment.