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

xr.polyval first arg requires name attribute #6526

Closed
headtr1ck opened this issue Apr 27, 2022 · 2 comments · Fixed by #6548
Closed

xr.polyval first arg requires name attribute #6526

headtr1ck opened this issue Apr 27, 2022 · 2 comments · Fixed by #6548

Comments

@headtr1ck
Copy link
Collaborator

What happened?

I have some polynomial coefficients and want to evaluate them at some values using xr.polyval.

As described in the docstring/docu I created a 1D coordinate DataArray and pass it to xr.polyval but it raises a KeyError (see example).

What did you expect to happen?

I expected that the polynomial would be evaluated at the given points.

Minimal Complete Verifiable Example

import xarray as xr

coeffs = xr.DataArray([1, 2, 3], dims="degree")

# With a "handmade" coordinate it fails:
coord = xr.DataArray([0, 1, 2], dims="x")

xr.polyval(coord, coeffs)
# raises:
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "xarray/core/computation.py", line 1847, in polyval
#     x = get_clean_interp_index(coord, coord.name, strict=False)
#   File "xarray/core/missing.py", line 252, in get_clean_interp_index
#     index = arr.get_index(dim)
#   File "xarray/core/common.py", line 404, in get_index
#     raise KeyError(key)
# KeyError: None

# If one adds a name to the coord that is called like the dimension:
coord2 = xr.DataArray([0, 1, 2], dims="x", name="x")

xr.polyval(coord2, coeffs)
# works

Relevant log output

No response

Anything else we need to know?

I assume that the "standard" workflow is to obtain the coord argument from an existing DataArrays coordinate, where the name would be correctly set already.
However, that is not clear from the description, and also prevents my "manual" workflow.

It could be that the problem will be solved by replacing the coord DataArray argument by an explicit Index in the future.

Environment

INSTALLED VERSIONS

commit: None
python: 3.9.10 (main, Mar 15 2022, 15:56:56)
[GCC 7.5.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-1160.49.1.el7.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.12.0
libnetcdf: 4.7.4

xarray: 2022.3.0
pandas: 1.4.2
numpy: 1.22.3
scipy: None
netCDF4: 1.5.8
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.6.0
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: 3.5.1
cartopy: 0.20.2
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
setuptools: 58.1.0
pip: 22.0.4
conda: None
pytest: None
IPython: 8.2.0
sphinx: None

@headtr1ck headtr1ck added bug needs triage Issue that has not been reviewed by xarray team member labels Apr 27, 2022
@headtr1ck
Copy link
Collaborator Author

Actually, I just realized that the second version also does not work since it uses the index of the coord argument and not its values. I guess that was meant by "The 1D coordinate along which to evaluate the polynomial".

Would you be open to a PR that allows any DataArray as coord argument and evaluates the polynomial at its values? Maybe that would break backwards compatibility though.

@dcherian dcherian added enhancement and removed needs triage Issue that has not been reviewed by xarray team member bug labels Apr 28, 2022
@dcherian
Copy link
Contributor

Would you be open to a PR that allows any DataArray as coord argument and evaluates the polynomial at its values?

I think yes. Note #4375 for the inverse problem.

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

Successfully merging a pull request may close this issue.

2 participants