Skip to content

weighted polyfit with NaNs appears broken #9048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 5 tasks
bbuzz31 opened this issue May 28, 2024 · 2 comments
Open
2 of 5 tasks

weighted polyfit with NaNs appears broken #9048

bbuzz31 opened this issue May 28, 2024 · 2 comments
Labels

Comments

@bbuzz31
Copy link

bbuzz31 commented May 28, 2024

What happened?

polyfit can't seem to handle weights still (despite #5933). nans are involved.

What did you expect to happen?

No response

Minimal Complete Verifiable Example

np.random.seed(10)
y = np.random.rand(10)
x = np.arange(len(y))
y[3] = np.nan
y[5] = np.nan
w = y/10

msk = ~np.isnan(y)
x = x[msk]
y = y[msk]
w = w[msk]

da_test = xr.DataArray(y, dims='time', coords={'time': x})
da_test.polyfit('time', 1, w=w)

# np.polyfit(x, y, 1)#, w=w) # <-- should match this

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

No response

Anything else we need to know?

not inserting the nans and/or converting the weights to a data array didn't matter

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:38:13) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 5.14.0-284.30.1.el9_2.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.3
libnetcdf: 4.9.2

xarray: 2024.3.0
pandas: 2.2.2
numpy: 1.26.4
scipy: 1.13.0
netCDF4: 1.6.5
pydap: None
h5netcdf: 1.3.0
h5py: 3.11.0
Nio: None
zarr: None
cftime: 1.6.3
nc_time_axis: None
iris: None
bottleneck: None
dask: 2024.5.0
distributed: 2024.5.0
matplotlib: 3.8.4
cartopy: 0.23.0
seaborn: 0.13.2
numbagg: None
fsspec: 2024.3.1
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 69.5.1
pip: 24.0
conda: None
pytest: None
mypy: None
IPython: 8.22.2
sphinx: None

@bbuzz31 bbuzz31 added bug needs triage Issue that has not been reviewed by xarray team member labels May 28, 2024
Copy link

welcome bot commented May 28, 2024

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

@bbuzz31 bbuzz31 changed the title weighted polyfit weighted polyfit May 28, 2024
@dcherian dcherian removed the needs triage Issue that has not been reviewed by xarray team member label Dec 2, 2024
@dcherian dcherian changed the title weighted polyfit weighted polyfit with NaNs appears broken Jan 22, 2025
@gcaria
Copy link
Contributor

gcaria commented May 16, 2025

This seems to be alright now

   ...: da_test.polyfit('time', 1, w=w)
Out[4]:
<xarray.Dataset> Size: 32B
Dimensions:               (degree: 2)
Coordinates:
  * degree                (degree) int64 16B 1 0
Data variables:
    polyfit_coefficients  (degree) float64 16B -0.009755 0.7121

In [5]: np.polyfit(x, y, 1, w=w)
Out[5]: array([-0.00975477,  0.71212604])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants