Skip to content

Conversation

malmans2
Copy link
Contributor

Lists passed to __getitem__ are converted to arrays. np.asarray infers the dtype, and for example converts a list with str and int to an array of str. Forcing dtype="O" fixes the issue.

@malmans2 malmans2 changed the title force object dtype in asarray Allow selecting variables using a list with mixed data types May 28, 2021
@@ -1485,7 +1485,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(np.asarray(key, dtype="O"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is np.asarray needed here?
Can we just check that key is a list, and raise an error otherwise.

Right now, if key is for example a set (i.e., ds[set(["foo"])]), an error is raised anyways because np.asarray creates a 0-d array and iteration is not possible. The error is not raised directly by xarray though and I think it's not very clear, especially because the original set is iterable.

Also, is the goal to only allow lists? Does it make sense to allow any iterable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for name in names:

Seems to only be used to loop through in ._copy_listed. I can't really think of a reason why it has to be forced to ndarray? names is also typed as an Iterable[Hashable], so I suppose it makes sense to allow any iterable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does anything break if we remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All tests look OK removing np.asarray

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Without wanting to ignore Chesterton's fence — let's do it unless we hear objections in the next day or so.

@max-sixty max-sixty added the plan to merge Final call for comments label May 31, 2021
@max-sixty max-sixty merged commit 5c59b66 into pydata:master Jun 12, 2021
@max-sixty
Copy link
Collaborator

@malmans2 feel free to add a whatsnew in another PR!

@max-sixty
Copy link
Collaborator

...and thank you for this contribution!

@malmans2 malmans2 deleted the fix_getitem branch June 21, 2021 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KeyError when trying to select a list of DataArrays with different name type
3 participants