Skip to content

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

@JonAnCla

Description

@JonAnCla

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions