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

assign_coords changed its behaviour from v2023.07.0 to v2023.08.0 #8150

Closed
4 tasks done
enekomartinmartinez opened this issue Sep 6, 2023 · 2 comments
Closed
4 tasks done
Labels
bug needs triage Issue that has not been reviewed by xarray team member

Comments

@enekomartinmartinez
Copy link

enekomartinmartinez commented Sep 6, 2023

What happened?

assign_coords changed its behaviour in v2023.08.0 now, when trying to assign an existing coord, it doesn't do anything...

I was using it to reorder some xarray.DataArrays, keeping the coordinate names, f.e.:

import xarray as xr
a = xr.DataArray([1, 2, 3], {'dim': ['A', 'B', 'C']})
new_order = [1, 0, 2]
a[new_order].assign_coords(a.coords)

returns

<xarray.DataArray (dim: 3)>
array([2, 1, 3])
Coordinates:
  * dim      (dim) <U1 'B' 'A' 'C'

I also tried to use a copy of the original coords a[new_order].assign_coords(a.coords.copy()), but it didn't work.

The behaviour is confusing and may lead to wrong results.

What did you expect to happen?

The same code in version v2023.07.0 returned the coordinates as defined and only changed the position of the values:

<xarray.DataArray (dim: 3)>
array([2, 1, 3])
Coordinates:
  * dim      (dim) <U1 'A' 'B' 'C'

It also works properly passing a dictionary instead of the coords object, a[new_order].assign_coords({'dim': ['A', 'B', 'C']}).

Minimal Complete Verifiable Example

import xarray as xr
a = xr.DataArray([1, 2, 3], {'dim': ['A', 'B', 'C']})
new_order = [1, 0, 2]
a[new_order].assign_coords(a.coords)

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.

Relevant log output

No response

Anything else we need to know?

No response

Environment

INSTALLED VERSIONS

commit: None
python: 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:27:40) [GCC 11.3.0]
python-bits: 64
OS: Linux
OS-release: 5.15.0-83-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: ('en_GB', 'UTF-8')
libhdf5: 1.10.4
libnetcdf: 4.7.3

xarray: 2023.8.0
pandas: 2.1.0
numpy: 1.23.5
scipy: 1.10.0
netCDF4: 1.6.0
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.6.2
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: None
dask: 2023.1.1
distributed: 2023.1.1
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: 2023.1.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 66.1.1
pip: 22.3.1
conda: None
pytest: 7.2.1
mypy: None
IPython: None
sphinx: None

@enekomartinmartinez enekomartinmartinez added bug needs triage Issue that has not been reviewed by xarray team member labels Sep 6, 2023
@welcome
Copy link

welcome bot commented Sep 6, 2023

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!

@benbovy
Copy link
Member

benbovy commented Sep 6, 2023

Hi @enekomartinmartinez, thanks for the report. This has been fixed in #8094 so the next release should restore the behavior in versions prior to v2023.08.0 (+ fix some other bugs). I'm going to close this issue but feel free to re-open it if the problem persists.

@benbovy benbovy closed this as completed Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs triage Issue that has not been reviewed by xarray team member
Projects
None yet
Development

No branches or pull requests

2 participants