Skip to content

Commit 25b9ec8

Browse files
authored
fix polyfit changing the original object (#7900)
* fix polyfit changing the original object * what's new * add test comment
1 parent f4e0523 commit 25b9ec8

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Performance
4242

4343
Bug fixes
4444
~~~~~~~~~
45+
- Fix bug where weighted ``polyfit`` were changing the original object (:issue:`5644`, :pull:`7900`).
46+
By `Mattia Almansi <https://github.com/malmans2>`_.
4547
- Don't call ``CachingFileManager.__del__`` on interpreter shutdown (:issue:`7814`, :pull:`7880`).
4648
By `Justus Magin <https://github.com/keewis>`_.
4749

xarray/core/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7972,7 +7972,7 @@ def polyfit(
79727972
scale_da = scale
79737973

79747974
if w is not None:
7975-
rhs *= w[:, np.newaxis]
7975+
rhs = rhs * w[:, np.newaxis]
79767976

79777977
with warnings.catch_warnings():
79787978
if full: # Copy np.polyfit behavior

xarray/tests/test_dataset.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6217,6 +6217,14 @@ def test_polyfit_output(self) -> None:
62176217
out = ds.polyfit("time", 2)
62186218
assert len(out.data_vars) == 0
62196219

6220+
def test_polyfit_weighted(self) -> None:
6221+
# Make sure weighted polyfit does not change the original object (issue #5644)
6222+
ds = create_test_data(seed=1)
6223+
ds_copy = ds.copy(deep=True)
6224+
6225+
ds.polyfit("dim2", 2, w=np.arange(ds.sizes["dim2"]))
6226+
xr.testing.assert_identical(ds, ds_copy)
6227+
62206228
def test_polyfit_warnings(self) -> None:
62216229
ds = create_test_data(seed=1)
62226230

0 commit comments

Comments
 (0)