Description
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