Skip to content

Commit

Permalink
Allow selecting variables using a list with mixed data types (pydata#…
Browse files Browse the repository at this point in the history
…5394)

* force object dtype in asarray

* remove asarray
  • Loading branch information
malmans2 authored Jun 12, 2021
1 parent d2de2c7 commit 5c59b66
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ def __getitem__(self, key):
if hashable(key):
return self._construct_dataarray(key)
else:
return self._copy_listed(np.asarray(key))
return self._copy_listed(key)

def __setitem__(self, key: Union[Hashable, List[Hashable], Mapping], value) -> None:
"""Add an array to this dataset.
Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3305,6 +3305,11 @@ def test_getitem_hashable(self):
with pytest.raises(KeyError, match=r"('var1', 'var2')"):
data[("var1", "var2")]

def test_getitem_multiple_dtype(self):
keys = ["foo", 1]
dataset = Dataset({key: ("dim0", range(1)) for key in keys})
assert_identical(dataset, dataset[keys])

def test_virtual_variables_default_coords(self):
dataset = Dataset({"foo": ("x", range(10))})
expected = DataArray(range(10), dims="x", name="x")
Expand Down

0 comments on commit 5c59b66

Please sign in to comment.