Skip to content

Commit 3ba4ce4

Browse files
authored
fix weighted polyfit for arrays with more than 2 dimensions (#9974)
* fix weighted polyfit * docs * cleanup * restore unicode test
1 parent 609412d commit 3ba4ce4

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Bug fixes
7171
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
7272
- Use zarr-fixture to prevent thread leakage errors (:pull:`9967`).
7373
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
74+
- Fix weighted ``polyfit`` for arrays with more than two dimensions (:issue:`9972`, :pull:`9974`).
75+
By `Mattia Almansi <https://github.com/malmans2>`_.
7476

7577
Documentation
7678
~~~~~~~~~~~~~

xarray/core/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9206,7 +9206,7 @@ def polyfit(
92069206

92079207
present_dims.update(other_dims)
92089208
if w is not None:
9209-
rhs = rhs * w[:, np.newaxis]
9209+
rhs = rhs * w.reshape(-1, *((1,) * len(other_dims)))
92109210

92119211
with warnings.catch_warnings():
92129212
if full: # Copy np.polyfit behavior

xarray/tests/test_dataset.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6685,11 +6685,15 @@ def test_polyfit_output(self) -> None:
66856685
assert len(out.data_vars) == 0
66866686

66876687
def test_polyfit_weighted(self) -> None:
6688-
# Make sure weighted polyfit does not change the original object (issue #5644)
66896688
ds = create_test_data(seed=1)
6689+
ds = ds.broadcast_like(ds) # test more than 2 dimensions (issue #9972)
66906690
ds_copy = ds.copy(deep=True)
66916691

6692-
ds.polyfit("dim2", 2, w=np.arange(ds.sizes["dim2"]))
6692+
expected = ds.polyfit("dim2", 2)
6693+
actual = ds.polyfit("dim2", 2, w=np.ones(ds.sizes["dim2"]))
6694+
xr.testing.assert_identical(expected, actual)
6695+
6696+
# Make sure weighted polyfit does not change the original object (issue #5644)
66936697
xr.testing.assert_identical(ds, ds_copy)
66946698

66956699
def test_polyfit_coord(self) -> None:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy