Skip to content

Commit d89a219

Browse files
authored
Fix dim argument for isel_points/sel_points when it's a pandas.Index (#918)
1 parent 70611a9 commit d89a219

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ Bug fixes
104104
- Fixed incorrect test for dask version (:issue:`891`). By
105105
`Stephan Hoyer <https://github.com/shoyer>`_.
106106

107+
- Fixed `dim` argument for `isel_points`/`sel_points` when a `pandas.Index` is
108+
passed. By `Stephan Hoyer <https://github.com/shoyer>`_.
109+
107110
- :py:func:`~xarray.plot.contour` now plots the correct number of contours
108111
(:issue:`866`). By `Fabien Maussion <https://github.com/fmaussion>`_.
109112

xarray/core/dataset.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,8 @@ def isel_points(self, dim='points', **indexers):
10341034
Dataset.sel_points
10351035
DataArray.isel_points
10361036
"""
1037+
from .dataarray import DataArray
1038+
10371039
indexer_dims = set(indexers)
10381040

10391041
def relevant_keys(mapping):
@@ -1072,10 +1074,9 @@ def relevant_keys(mapping):
10721074
# dim already exists
10731075
raise ValueError('Existing dimensions are not valid choices '
10741076
'for the dim argument in sel_points')
1075-
else:
1076-
# try to cast dim to DataArray with name = points
1077-
from .dataarray import DataArray
1078-
dim = DataArray(dim, dims='points', name='points')
1077+
1078+
if not utils.is_scalar(dim) and not isinstance(dim, DataArray):
1079+
dim = as_variable(dim, key='points')
10791080

10801081
# TODO: This would be sped up with vectorized indexing. This will
10811082
# require dask to support pointwise indexing as well.

xarray/test/test_dataset.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,11 +787,18 @@ def test_isel_points(self):
787787
self.assertDataArrayIdentical(actual['station'].drop(['dim1', 'dim2']),
788788
stations['station'])
789789

790-
# make sure we get the default points coordinate when a list is passed
790+
# make sure we get the default 'points' coordinate when a list is passed
791791
actual = data.isel_points(dim1=stations['dim1s'],
792792
dim2=stations['dim2s'],
793793
dim=['A', 'B', 'C'])
794794
assert 'points' in actual.coords
795+
assert actual.coords['points'].values.tolist() == ['A', 'B', 'C']
796+
797+
# test index
798+
actual = data.isel_points(dim1=stations['dim1s'].values,
799+
dim2=stations['dim2s'].values,
800+
dim=pd.Index(['A', 'B', 'C'], name='letters'))
801+
assert 'letters' in actual.coords
795802

796803
# can pass a numpy array
797804
data.isel_points(dim1=stations['dim1s'],

0 commit comments

Comments
 (0)