Skip to content
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

Polyfit with weights doesn't work with one-dimensional array #9845

Open
5 tasks done
mgunyho opened this issue Dec 2, 2024 · 0 comments
Open
5 tasks done

Polyfit with weights doesn't work with one-dimensional array #9845

mgunyho opened this issue Dec 2, 2024 · 0 comments
Labels

Comments

@mgunyho
Copy link
Contributor

mgunyho commented Dec 2, 2024

What happened?

I'm trying to use DataArray.polyfit with a set of weights, but if the data is one-dimensional, I get the following error:

Traceback (most recent call last):
  File ".../xarray/.scratch/polyfit-1d-weights.py", line 3, in <module>
    xr.DataArray([1, 2, 4], coords=[("x", [1, 2, 3])]).polyfit("x", deg=2, w=[0.1, 0.2, 0.3])
  File ".../xarray/xarray/core/dataarray.py", line 5729, in polyfit
    return self._to_temp_dataset().polyfit(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../xarray/xarray/core/dataset.py", line 9223, in polyfit
    rhs = rhs * w[:, np.newaxis]
          ~~~~^~~~~~~~~~~~~~~~~~
  File ".../xarray/xarray/core/_typed_ops.py", line 934, in __mul__
    return self._binary_op(other, operator.mul)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../xarray/xarray/core/variable.py", line 2393, in _binary_op
    result = Variable(dims, new_data, attrs=attrs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../xarray/xarray/core/variable.py", line 424, in __init__
    super().__init__(
  File ".../xarray/xarray/namedarray/core.py", line 264, in __init__
    self._dims = self._parse_dimensions(dims)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../xarray/xarray/namedarray/core.py", line 508, in _parse_dimensions
    raise ValueError(
ValueError: dimensions ('x',) must have the same length as the number of data dimensions, ndim=2

What did you expect to happen?

Polyfit should work normally when both the input data and weights are one-dimensional

Minimal Complete Verifiable Example

import xarray as xr

xr.DataArray([1, 2, 4], coords=[("x", [1, 2, 3])]).polyfit("x", deg=2, w=[0.1, 0.2, 0.3])

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?

If I artificially add an extra dimension to the data, there is no error. For example, the following works:

(
     xr.DataArray([1, 2, 4], coords=[("x", [1, 2, 3])])
     .expand_dims(foo=1)
     .polyfit("x", deg=2, w=[0.1, 0.2, 0.3])
 )

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.7 (main, Oct 1 2024, 00:00:00) [GCC 14.2.1 20240912 (Red Hat 14.2.1-3)]
python-bits: 64
OS: Linux
OS-release: 6.11.5-200.fc40.x86_64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None

xarray: 2024.9.1.dev73+g8f6e45ba
pandas: 2.2.3
numpy: 2.1.2
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: 23.3.2
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None

@mgunyho mgunyho added bug needs triage Issue that has not been reviewed by xarray team member labels Dec 2, 2024
@dcherian dcherian removed the needs triage Issue that has not been reviewed by xarray team member label Dec 2, 2024
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

2 participants