home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 524940277

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
524940277 MDU6SXNzdWU1MjQ5NDAyNzc= 3546 loc API gives KeyError: "not all values found in index" 8934026 open 0     4 2019-11-19T11:11:13Z 2019-11-19T15:22:12Z   NONE      

I am having issues with the loc API of xarray. I am trying to select data that satisfy a certain condition. Below should be a reproducible example:

import xarray as xr
ds = xr.tutorial.load_dataset('air_temperature')
da = ds.air
mask = 2.3e2*xr.ones_like(da[0,0])
da[0,0].loc[da[0,0]<mask]

but this gives the following error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-238-75b6b6f544d4> in <module>
      1 da = ds.air
      2 mask = 2.3e2*xr.ones_like(da[0,0])
----> 3 da[0,0].loc[da[0,0]<mask]

~/miniconda3/envs/ensemble/lib/python3.7/site-packages/xarray/core/dataarray.py in __getitem__(self, key)
    194             labels = indexing.expanded_indexer(key, self.data_array.ndim)
    195             key = dict(zip(self.data_array.dims, labels))
--> 196         return self.data_array.sel(**key)
    197 
    198     def __setitem__(self, key, value) -> None:

~/miniconda3/envs/ensemble/lib/python3.7/site-packages/xarray/core/dataarray.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   1045             method=method,
   1046             tolerance=tolerance,
-> 1047             **indexers_kwargs
   1048         )
   1049         return self._from_temp_dataset(ds)

~/miniconda3/envs/ensemble/lib/python3.7/site-packages/xarray/core/dataset.py in sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   1998         indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
   1999         pos_indexers, new_indexes = remap_label_indexers(
-> 2000             self, indexers=indexers, method=method, tolerance=tolerance
   2001         )
   2002         result = self.isel(indexers=pos_indexers, drop=drop)

~/miniconda3/envs/ensemble/lib/python3.7/site-packages/xarray/core/coordinates.py in remap_label_indexers(obj, indexers, method, tolerance, **indexers_kwargs)
    390 
    391     pos_indexers, new_indexes = indexing.remap_label_indexers(
--> 392         obj, v_indexers, method=method, tolerance=tolerance
    393     )
    394     # attach indexer's coordinate to pos_indexers

~/miniconda3/envs/ensemble/lib/python3.7/site-packages/xarray/core/indexing.py in remap_label_indexers(data_obj, indexers, method, tolerance)
    259             coords_dtype = data_obj.coords[dim].dtype
    260             label = maybe_cast_to_coords_dtype(label, coords_dtype)
--> 261             idxr, new_idx = convert_label_indexer(index, label, dim, method, tolerance)
    262             pos_indexers[dim] = idxr
    263             if new_idx is not None:

~/miniconda3/envs/ensemble/lib/python3.7/site-packages/xarray/core/indexing.py in convert_label_indexer(index, label, index_name, method, tolerance)
    191             indexer = get_indexer_nd(index, label, method, tolerance)
    192             if np.any(indexer < 0):
--> 193                 raise KeyError("not all values found in index %r" % index_name)
    194     return indexer, new_index
    195

KeyError: "not all values found in index 'lat'"

Since the mask is identical to the xarray.DataArray da in terms of dimensions and coordinates, I don't think this error makes sense... If I change the coordinates in the following manner:

da = xr.DataArray(ds.air.data, dims=ds.air.dims, coords={'time':range(len(ds.time)),'lat':range(len(ds.lat)),'lon':range(len(ds.lon))}) mask = da[0,0] < 2.3e2*xr.ones_like(da[0,0]) da[0,0].loc[mask]

it works:

<xarray.DataArray (lon: 5)> array([228.79999, 227.29999, 227. , 227.5 , 228.79999], dtype=float32) Coordinates: time int64 0 lat int64 0 * lon (lon) int64 44 45 46 47 48

Am I missing something or is this possibly a bug? Thank you in advance for your help.

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/3546/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
    13221727 issue

Links from other tables

  • 0 rows from issues_id in issues_labels
  • 4 rows from issue in issue_comments
Powered by Datasette · Queries took 158.115ms · About: xarray-datasette