-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Errors when assigning using .from_pandas_multiindex
#8455
Comments
I think you rather want to pass the da.assign_coords(xr.Coordinates.from_pandas_multiindex(pd.MultiIndex.from_tuples([(1,2)]), dim='foo'))
|
Ah great, that does work. Should we update the signature of
Hmmm, no luck without it... da.assign_coords(xr.Coordinates.from_pandas_multiindex(pd.MultiIndex.from_tuples([(1,2)]), dim='foo'))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[8], line 1
----> 1 da.assign_coords(xr.Coordinates.from_pandas_multiindex(pd.MultiIndex.from_tuples([(1,2)]), dim='foo'))
File ~/workspace/xarray/xarray/core/common.py:621, in DataWithCoords.assign_coords(self, coords, **coords_kwargs)
618 else:
619 results = self._calc_assign_results(coords_combined)
--> 621 data.coords.update(results)
622 return data
File ~/workspace/xarray/xarray/core/coordinates.py:566, in Coordinates.update(self, other)
560 # special case for PandasMultiIndex: updating only its dimension coordinate
561 # is still allowed but depreciated.
562 # It is the only case where we need to actually drop coordinates here (multi-index levels)
563 # TODO: remove when removing PandasMultiIndex's dimension coordinate.
564 self._drop_coords(self._names - coords_to_align._names)
--> 566 self._update_coords(coords, indexes)
File ~/workspace/xarray/xarray/core/coordinates.py:836, in DataArrayCoordinates._update_coords(self, coords, indexes)
834 dims = calculate_dimensions(coords_plus_data)
835 if not set(dims) <= set(self.dims):
--> 836 raise ValueError(
837 "cannot add coordinates with new dimensions to a DataArray"
838 )
839 self._data._coords = coords
841 # TODO(shoyer): once ._indexes is always populated by a dict, modify
842 # it to update inplace instead.
ValueError: cannot add coordinates with new dimensions to a DataArray |
As discussed in pydata#8455
Ah yes, right you need expand_dims :) |
* Fix type of `.assign_coords` As discussed in #8455 * Update xarray/core/common.py Co-authored-by: Benoit Bovy <[email protected]> * Generally improve docstring * . --------- Co-authored-by: Benoit Bovy <[email protected]>
What happened?
Very possibly this is user-error, forgive me if so.
I'm trying to transition some code from the previous assignment of MultiIndexes, to the new world. Here's an MCVE:
What did you expect to happen?
No response
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.9.18 (main, Nov 2 2023, 16:51:22)
[Clang 14.0.3 (clang-1403.0.22.14.1)]
python-bits: 64
OS: Darwin
OS-release: 22.6.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: en_US.UTF-8
LANG: None
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None
xarray: 2023.10.2.dev10+gccc8f998
pandas: 2.1.1
numpy: 1.25.2
scipy: 1.11.1
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.16.0
cftime: None
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: None
dask: 2023.4.0
distributed: 2023.7.1
matplotlib: 3.5.1
cartopy: None
seaborn: None
numbagg: 0.2.3.dev30+gd26e29e
fsspec: 2021.11.1
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: 0.9.19
setuptools: 68.2.2
pip: 23.3.1
conda: None
pytest: 7.4.0
mypy: 1.6.0
IPython: 8.15.0
sphinx: 4.3.2
The text was updated successfully, but these errors were encountered: