Get a list of xarray index values

Short version: How do I get a list (or iterator) of the indexes (names) of an xarray

I am a beginner with Bayesian modeling and pymc. I’ve had amazing success with the math, but the basic programming using xarray has been baffling me - and sending me into a loop in the documentation.

If I have an xarray that has coordinates how can I get access to the list of strings that name the index of the coordinates.

For example, suppose I have an array (here stored in the variable d):

print(d)
<xarray.DataArray 'logisticerr~(3cats)::beta_responseTimeSeconds' (chartType: 4)>
array([ 0.32511041, -0.04013673,  0.29726454,  0.6054794 ])
Coordinates:
  * chartType  (chartType) <U8 'bar' 'pie' 'stackbar' 'table'

what I’d like to get is a list (or something I can iterate over to get strings) like [ 'bar', 'pie', 'stackbar', 'table']

The only thing I can come up with is: d.coords[list(d.coords.keys())[0]].to_numpy()

What is the right way to do this?

Sorry to ask such a basic question…

Thanks!

Hi,

That is how I would have done it as well. If you want something somewhat more compact you can construct a dictionary that maps dims to array of coordinates

dim_to_coords = {key:d.coords[key].to_numpy() for key in d.dims}

Note that this is not much different from the dictionary d.coords, it is essentially a version of with its values changed to arrays. So this would also work if that seems more pleasant:

{key:val.to_numpy() for key,val in d.coords.items()}

That being said, if you really just want to get coordinates by dimension index then this is slightly more compact compared to what you are using:

list(d.coords.values())[0].to_numpy()

Any reason why you are not satisfied with your approach?
Also, if your xarray is two dimensional:

d.to_pandas().index
d.to_pandas().columns

will respectively give you coordinates along in each dimension as well. This won’t work in higher dimensions.

3 Likes

Thank you! It seems weird that I have to convert to a numpy array to pull out the strings, but it is reassuring to know that I haven’t missed something easier.

Well depends on what you want to do but d.coords[“key”] itself is also an iterable so you can iterate over with or convert to list via (list()). But then elements won’t be strings they will be zero dimensional xarrays, which you would still need to convert to an array if you want to access the contents directly.

Thanks!

I need to get better at using xarray - a zero dimensional xarray is not something I have figure out how to use easily (I am sure there’s an easy way to extract the value from it, but I haven’t found it yet)

If you mean it’s just a single scalar value, you can use .item()

1 Like

I will add some tangentially related advise related to the list(d.coords.keys())[0] piece here. In xarray, what you use for slicing are dimension names (and to a lesser extent coordinate values). All xarray objects require names for all its dimensions. On the other hand however, the order of these dimensions is secondary (and there are functions that can modify that).

Therefore, list(d.coords.keys())[0] in my opinion is the wrong way to go about things, and it is bound to make your life difficult and probably hate xarray too. That is because you are relying on dimension order first and foremost, and using that to get the dimension name. You should rely on its name “chartType” first and foremost and use d.dims.index("chartType") to get which position is that dimension in (for multidimensional objects where you use multiple operations on them, for 1d objects also use 0 straight away).

This might serve as background/reasoning as to why as @iavicenna pointed out getting the coordinate values out of the first one doesn’t get better than list(d.coords.values())[0].to_numpy(). It is something that should generally be avoided as xarray was designed with the opposite workflow in mind.

3 Likes

Thanks! That is a good point…
I am not using xarray correctly, and rather than adapting my thinking to embrace it, was looking for a “quick fix” so I can solve my immediate problem. Long term, I will need to embrace xarray. In the short term, I just want to build some Bayesian models for my problem…