Skip to content

Selecting over a MultiIndex coordinate drops coordinate in question #1491

Closed
@slmason85

Description

@slmason85

Selecting over a Dataset or DataArray using a coordinate that's part of a MultiIndex drops the coordinate over which the selection was made.

#Initialising (time, lat, lon) arrays
time = pd.date_range(start='2008-01-01 00:00:00', end='2008-01-01 12:00:00', freq='1H')
latitude  = np.linspace(-30, 30, 13)
longitude = np.linspace(0, 60, 13)

Creating a simple Dataset with (time, lat, lon) variables along the same track.

#Create a dataset 
ds = xray.Dataset(data_vars={'time':xray.Variable(['index'], time), 
                             'latitude':xray.Variable(['index'], latitude),
                             'longitude':xray.Variable(['index'], longitude)},
                  coords={'index':xray.IndexVariable(['index'], data=np.arange(13))})

If we create a MultiIndex then select over the time coordinate...

ds.set_index(index=('time', 'latitude', 'longitude'), inplace=True)
ds.sel(**dict(time=slice('2008-01-01 03:00:00', '2008-01-01 09:00:00')))

In the output the time coordinate has been dropped entirely, whether the selection was as a scalar or over a slice:

<xarray.Dataset>
Dimensions:    (index: 7)
Coordinates:
  * index      (index) MultiIndex
  - latitude   (index) float64 -15.0 -10.0 -5.0 0.0 5.0 10.0 15.0
  - longitude  (index) float64 15.0 20.0 25.0 30.0 35.0 40.0 45.0
Data variables:
    *empty*

Expected behaviour would be to retain the time coordinate within the MultiIndex, possibly with an option to drop scalar coordinates.

Seems like this behaviour might be related to other coordinate-dropping behaviour: #1470 and #1483 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions