keewis / xarray-custom-indexes

try out the new custom indexes feature of xarray
MIT License
0 stars 0 forks source link

Example notebook doesn't work for me with xarray 2023.06.0 #1

Open rabernat opened 1 year ago

rabernat commented 1 year ago

On cell 10

new_arr.sel(indexers)

I get

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[10], line 1
----> 1 new_arr.sel(indexers)

File [~/gh/xarray/xarray/core/dataarray.py:1550](https://file+.vscode-resource.vscode-cdn.net/Users/rabernat/gh/earth-mover/xarray-vector-index/notebooks/~/gh/xarray/xarray/core/dataarray.py:1550), in DataArray.sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   1440 def sel(
   1441     self: T_DataArray,
   1442     indexers: Mapping[Any, Any] | None = None,
   (...)
   1446     **indexers_kwargs: Any,
   1447 ) -> T_DataArray:
   1448     """Return a new DataArray whose data is given by selecting index
   1449     labels along the specified dimension(s).
   1450 
   (...)
   1548     Dimensions without coordinates: points
   1549     """
-> 1550     ds = self._to_temp_dataset().sel(
   1551         indexers=indexers,
   1552         drop=drop,
   1553         method=method,
   1554         tolerance=tolerance,
   1555         **indexers_kwargs,
   1556     )
   1557     return self._from_temp_dataset(ds)

File [~/gh/xarray/xarray/core/dataset.py:2667](https://file+.vscode-resource.vscode-cdn.net/Users/rabernat/gh/earth-mover/xarray-vector-index/notebooks/~/gh/xarray/xarray/core/dataset.py:2667), in Dataset.sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   2664                 query_results.drop_coords.append(k)
   2665     query_results.variables = no_scalar_variables
-> 2667 result = self.isel(indexers=query_results.dim_indexers, drop=drop)
   2668 return result._overwrite_indexes(*query_results.as_tuple()[1:])

File [~/gh/xarray/xarray/core/dataset.py:2507](https://file+.vscode-resource.vscode-cdn.net/Users/rabernat/gh/earth-mover/xarray-vector-index/notebooks/~/gh/xarray/xarray/core/dataset.py:2507), in Dataset.isel(self, indexers, drop, missing_dims, **indexers_kwargs)
   2505 indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "isel")
   2506 if any(is_fancy_indexer(idx) for idx in indexers.values()):
-> 2507     return self._isel_fancy(indexers, drop=drop, missing_dims=missing_dims)
   2509 # Much faster algorithm for when all indexers are ints, slices, one-dimensional
   2510 # lists, or zero or one-dimensional np.ndarray's
   2511 indexers = drop_dims_from_indexers(indexers, self.dims, missing_dims)

File [~/gh/xarray/xarray/core/dataset.py:2553](https://file+.vscode-resource.vscode-cdn.net/Users/rabernat/gh/earth-mover/xarray-vector-index/notebooks/~/gh/xarray/xarray/core/dataset.py:2553), in Dataset._isel_fancy(self, indexers, drop, missing_dims)
   2550 valid_indexers = dict(self._validate_indexers(indexers, missing_dims))
   2552 variables: dict[Hashable, Variable] = {}
-> 2553 indexes, index_variables = isel_indexes(self.xindexes, valid_indexers)
   2555 for name, var in self.variables.items():
   2556     if name in index_variables:

File [~/gh/xarray/xarray/core/indexes.py:1478](https://file+.vscode-resource.vscode-cdn.net/Users/rabernat/gh/earth-mover/xarray-vector-index/notebooks/~/gh/xarray/xarray/core/indexes.py:1478), in isel_indexes(indexes, indexers)
   1474 def isel_indexes(
   1475     indexes: Indexes[Index],
   1476     indexers: Mapping[Any, Any],
   1477 ) -> tuple[dict[Hashable, Index], dict[Hashable, Variable]]:
-> 1478     return _apply_indexes(indexes, indexers, "isel")

File [~/gh/xarray/xarray/core/indexes.py:1462](https://file+.vscode-resource.vscode-cdn.net/Users/rabernat/gh/earth-mover/xarray-vector-index/notebooks/~/gh/xarray/xarray/core/indexes.py:1462), in _apply_indexes(indexes, args, func)
   1460 index_args = {k: v for k, v in args.items() if k in index_dims}
   1461 if index_args:
-> 1462     new_index = getattr(index, func)(index_args)
   1463     if new_index is not None:
   1464         new_indexes.update({k: new_index for k in index_vars})

AttributeError: 'dict' object has no attribute 'isel
keewis commented 1 year ago

I'm not sure why that is, and I can't reproduce: in my environment, the creation of the pandas indexes for x and y fail (this can be fixed by passing options={} to the from_variables call), but then the sel in cell 9, the isel in cell 10 and even the copy in cell 15 succeed.

For reference, this is how I set up the environment:

mamba create -n test python=3.11 jupyterlab xarray numpy scipy

(plus a pip install 'xarray==2023.06.0', since the package on conda-forge does not seem to be available yet)