Skip to content

Commit 4fbca23

Browse files
committed
fix bug in polyval broadcasting
1 parent 553de10 commit 4fbca23

File tree

2 files changed

+5
-12
lines changed

2 files changed

+5
-12
lines changed

xarray/core/computation.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,14 +1892,7 @@ def polyval(
18921892

18931893
# using Horner's method
18941894
# https://en.wikipedia.org/wiki/Horner%27s_method
1895-
res = (
1896-
coeffs.isel({degree_dim: int(deg_idx_sorted[-1])}, drop=True)
1897-
.broadcast_like(coord)
1898-
.copy(deep=True)
1899-
)
1900-
if isinstance(coord, Dataset) and not isinstance(res, Dataset):
1901-
res = Dataset({var: res for var in coord})
1902-
1895+
res = coeffs.isel({degree_dim: int(deg_idx_sorted[-1])}, drop=True) + 0 * coord
19031896
deg_idx = len(deg_coord) - 2
19041897
for deg in range(max_deg - 1, -1, -1):
19051898
res *= coord

xarray/tests/test_computation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,7 @@ def test_polyval_compat(use_dask, use_datetime) -> None:
19631963

19641964
da_pv = xr.polyval(da.x, coeffs)
19651965

1966-
xr.testing.assert_allclose(da, da_pv.T)
1966+
xr.testing.assert_allclose(da, da_pv)
19671967

19681968

19691969
@pytest.mark.parametrize(
@@ -1978,7 +1978,7 @@ def test_polyval_compat(use_dask, use_datetime) -> None:
19781978
pytest.param(
19791979
xr.DataArray([1, 2, 3], dims="x"),
19801980
xr.DataArray([[0, 1], [0, 1]], dims=("y", "degree")),
1981-
xr.DataArray([[1, 1], [2, 2], [3, 3]], dims=("x", "y")),
1981+
xr.DataArray([[1, 2, 3], [1, 2, 3]], dims=("y", "x")),
19821982
id="broadcast-x",
19831983
),
19841984
pytest.param(
@@ -2012,9 +2012,9 @@ def test_polyval_compat(use_dask, use_datetime) -> None:
20122012
id="dataset-array",
20132013
),
20142014
pytest.param(
2015-
xr.Dataset({"a": ("x", [1, 2, 3]), "b": ("x", [2, 3, 4])}),
2015+
xr.Dataset({"a": ("x", [1, 2, 3]), "b": ("y", [2, 3, 4])}),
20162016
xr.Dataset({"a": ("degree", [0, 1]), "b": ("degree", [1, 1])}),
2017-
xr.Dataset({"a": ("x", [1, 2, 3]), "b": ("x", [3, 4, 5])}),
2017+
xr.Dataset({"a": ("x", [1, 2, 3]), "b": ("y", [3, 4, 5])}),
20182018
id="dataset-dataset",
20192019
),
20202020
],

0 commit comments

Comments
 (0)