MultiIndex Coordinates (also: selection by coordinate)

414 views
Skip to first unread message

Paul Hobson

unread,
May 24, 2018, 7:26:23 PM5/24/18
to xarray
Hey folks,

I'm working through the basics of xarray at the moment. In the docs, there's an example like this:

import numpy
import pandas
import xarray

xx
= xarray.DataArray(numpy.random.rand(4, 3), [
   
('time', pandas.date_range('2000-01-01', periods=4)),
   
('space', ['IA', 'IL', 'IN'])
]).to_dataset(name='foo')

And then the `xx` object is:

<xarray.Dataset>
Dimensions:  (space: 3, time: 4)
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03 2000-01-04
  * space    (space) <U2 'IA' 'IL' 'IN'
Data variables:
    foo      (time, space) float64 0.3914 0.6567 0.8812 0.5413 0.7003 0.5879 ...

And I can select values based on the coordinates with:

xx.sel(time='2000-01-01', space=['IA', 'IN'])

And understandably get:

<xarray.Dataset>
Dimensions:  (space: 2)
Coordinates:
    time     datetime64[ns] 2000-01-01
  * space    (space) <U2 'IA' 'IN'
Data variables:
    foo      (space) float64 0.3914 0.8812


Easy enough. So now I'm reading grib files (with a pynio engine) from NOAA and trying to select from it (note the emphasized labels):

ds = xarray.open_dataset('data/p06m_2018052418f060.grb', engine='pynio')
<xarray.Dataset>
Dimensions:               (g3_x_0: 138, g3_y_1: 180)
Coordinates:
    g3_lat_0              (g3_x_0, g3_y_1) float32 ...
    g3_lon_1              (g3_x_0, g3_y_1) float32 ...
Dimensions without coordinates: g3_x_0, g3_y_1
Data variables:
    A_PCP_GDS3_SFC_acc6h  (g3_x_0, g3_y_1) float32 ...
    g3_rot_2              (g3_x_0, g3_y_1) float32 ...


And so trying to select the data in a similar fashion fails with a ValueError that I don't understand:

ds.sel(method='nearest', g3_lat_0=19.017)
~/miniconda3/envs/grb/lib/python3.6/site-packages/xarray/core/indexing.py in get_dim_indexers(data_obj, indexers)
    203     if invalid:
    204         raise ValueError("dimensions or multi-index levels %r do not exist"
--> 205                          % invalid)
    206 
    207     level_indexers = defaultdict(dict)

ValueError: dimensions or multi-index levels ['g3_lat_0'] do not exist


How can I access values in a dataset where the coordinate labels appear to be tuples?

Cheers,
-Paul

Stephan Hoyer

unread,
May 25, 2018, 1:57:03 AM5/25/18
to xar...@googlegroups.com
Hi Paul,

In your example, "g3_lat_0" is actually a two-dimensional coordinate. Unfortunately, we don't yet support indexing on 2D coordinates, but this is something we do hope to support in the future (it will require something like a KDTree instead of the pandas.Index we use for 1D coordinates).

This post has a few more details on this question:
--
You received this message because you are subscribed to the Google Groups "xarray" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xarray+un...@googlegroups.com.
To post to this group, send email to xar...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xarray/d10b1d41-a5e9-45b3-a3c2-b7c01b4697ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Hobson

unread,
May 25, 2018, 2:28:00 PM5/25/18
to xar...@googlegroups.com
Stephen,

Thanks for the response and the links. It's very much appreciated.

I think I'll be able to piece together a working solution from this info. I'll post it back here when that happens.

-Paul

To unsubscribe from this group and stop receiving emails from it, send an email to xarray+unsubscribe@googlegroups.com.

To post to this group, send email to xar...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xarray/d10b1d41-a5e9-45b3-a3c2-b7c01b4697ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "xarray" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xarray+unsubscribe@googlegroups.com.

To post to this group, send email to xar...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages