Skip to content

"in" operator does not work as expected on DataArray dimensions #1267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hottwaj opened this issue Feb 14, 2017 · 2 comments · Fixed by #2520
Closed

"in" operator does not work as expected on DataArray dimensions #1267

hottwaj opened this issue Feb 14, 2017 · 2 comments · Fixed by #2520
Milestone

Comments

@hottwaj
Copy link

hottwaj commented Feb 14, 2017

As an example I have a DataArray called "my_dataarray" that looks something like this:

<xarray.DataArray 'values' (Type: 3)>
array([1, 2, 3])
Coordinates:
  * Type        (Type) object 'Type 1' 'Type 2' 'Type 3'

'Type' is a dimension on my DataArray. Note that 'Type' is also a DataArray that looks like this:

OrderedDict([('Type', <xarray.IndexVariable 'Type' (Type: 3)>
array(['Type 1', 'Type 2', 'Type 3'], 
      dtype='object'))])

Let's say I run:

'Type 1' in my_dataarray.Type

The result is False, even though 'Type 1' is in the "Type" dimension.

To get the result I was expecting I need to run:

'Type 1' in my_dataarray.Type.values

Stepping through the code, the problematic line is here:

return key in self._coords

The test used for __contains__(self, key) on the Type dimension is whether the key is in the _coords of Type.

This is probably the right thing to do when the DataArray is used for storing data, but probably not what we want if the DataArray is being used as a dimension - it should instead check if 'Type 1' is in the values of Type?

@shoyer
Copy link
Member

shoyer commented Feb 14, 2017

I agree, this is strange. my_dataarray['Type'] is really just a convenient shortcut for my_dataarray.coords['Type'], but DataArray is meant to be primarily array-like so in should look at the values (we have Dataset for the dict-like container).

I would be in favor of changing this to look at values like in on NumPy arrays in the next major release (0.10.0). That's probably a little ways off though, so let's hold off on the pull request for now :).

@shoyer
Copy link
Member

shoyer commented Oct 22, 2017

#1645 adds a FutureWarning. We'll actually change this in v0.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants