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

Allow an ellipsis in stack? #3814

Closed
max-sixty opened this issue Feb 28, 2020 · 1 comment
Closed

Allow an ellipsis in stack? #3814

max-sixty opened this issue Feb 28, 2020 · 1 comment

Comments

@max-sixty
Copy link
Collaborator

Could we add the ability to use an ellipsis to represent all dims in more places? For example, stack:

MCVE Code Sample

In [14]: data = np.arange(15, 301, 15).reshape(2, 10)  
    ...: da = xr.DataArray(data, dims=('y', 'x'), attrs={'test': 'test'})  
    ...:                                                                                                                                                            

In [15]: da.stack(z=['x','y'])                                                                                                                                      
Out[15]: 
<xarray.DataArray (z: 20)>
array([ 15, 165,  30, 180,  45, 195,  60, 210,  75, 225,  90, 240, 105,
       255, 120, 270, 135, 285, 150, 300])
Coordinates:
  * z        (z) MultiIndex
  - x        (z) int64 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
  - y        (z) int64 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
Attributes:
    test:     test

In [16]: da.stack(z=[...])                                                                                                                                          
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-16-a92d0ffe931a> in <module>
----> 1 da.stack(z=[...])

~/workspace/./.venv/lib64/python3.6/site-packages/xarray/core/dataarray.py in stack(self, dimensions, **dimensions_kwargs)
   1739         DataArray.unstack
   1740         """
-> 1741         ds = self._to_temp_dataset().stack(dimensions, **dimensions_kwargs)
   1742         return self._from_temp_dataset(ds)
   1743 

~/workspace/./.venv/lib64/python3.6/site-packages/xarray/core/dataset.py in stack(self, dimensions, **dimensions_kwargs)
   3291         result = self
   3292         for new_dim, dims in dimensions.items():
-> 3293             result = result._stack_once(dims, new_dim)
   3294         return result
   3295 

~/workspace/./.venv/lib64/python3.6/site-packages/xarray/core/dataset.py in _stack_once(self, dims, new_dim)
   3246 
   3247         # consider dropping levels that are unused?
-> 3248         levels = [self.get_index(dim) for dim in dims]
   3249         idx = utils.multiindex_from_product_levels(levels, names=dims)
   3250         variables[new_dim] = IndexVariable(new_dim, idx)

~/workspace/./.venv/lib64/python3.6/site-packages/xarray/core/dataset.py in <listcomp>(.0)
   3246 
   3247         # consider dropping levels that are unused?
-> 3248         levels = [self.get_index(dim) for dim in dims]
   3249         idx = utils.multiindex_from_product_levels(levels, names=dims)
   3250         variables[new_dim] = IndexVariable(new_dim, idx)

~/workspace/./.venv/lib64/python3.6/site-packages/xarray/core/common.py in get_index(self, key)
    378         """
    379         if key not in self.dims:
--> 380             raise KeyError(key)
    381 
    382         try:

KeyError: Ellipsis

Expected Output

Identical between ... and listing all dimensions

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.8 (default, Aug 7 2019, 17:28:10) python-bits: 64 OS: Linux machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.utf8 LOCALE: en_US.UTF-8 libhdf5: 1.10.4 libnetcdf: None

xarray: 0.15.0
pandas: 0.25.3
numpy: 1.18.1
scipy: 1.4.1
netCDF4: None
pydap: None
h5netcdf: None
h5py: 2.10.0
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: 1.3.2
dask: 2.11.0
distributed: None
matplotlib: 3.1.2
cartopy: None
seaborn: 0.10.0
numbagg: None
setuptools: 45.0.0
pip: 20.0.2
conda: None
pytest: 5.3.2
IPython: 7.12.0
sphinx: 2.3.1

@dcherian
Copy link
Contributor

dcherian commented Mar 3, 2020

👍

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

No branches or pull requests

2 participants