How to manipulate Arviz data arrays?


I think this may be a simple question.

How do I pull both the max of the observed data for my target variable and the coordinate that it’s associated with at the same time?

I have a time series sells forecasting model. Below is a screen shot of the observed data:

I do the following to pull the max value:


This however, does not keep the coordinates associated with it. See below.

I’d like to pull this value along with the item number associated in both predicted_eaches_dim_0 and item coordinates in the original screen shot. Is this possible?

.max and other similar aggregation functions like mean take a dim argument. What if you do


I think you want idxmax which returns the label where the max is. Then you can use .sel to get the value

Thank you. I’ve tried both




Both gave me the following:

KeyError                                  Traceback (most recent call last)
/tmp/ipykernel_38045/ in <module>
----> 1 data["observed_data"]["predicted_eaches"].idxmax()

/opt/conda/lib/python3.7/site-packages/xarray/core/ in idxmax(self, dim, skipna, fill_value, keep_attrs)
   4267             skipna=skipna,
   4268             fill_value=fill_value,
-> 4269             keep_attrs=keep_attrs,
   4270         )

/opt/conda/lib/python3.7/site-packages/xarray/core/ in _calc_idxminmax(array, func, dim, skipna, fill_value, keep_attrs)
   1670         raise KeyError(f'Dimension "{dim}" not in dimension')
   1671     if dim not in array.coords:
-> 1672         raise KeyError(f'Dimension "{dim}" does not have coordinates')
   1674     # These are dtypes with NaN values argmin and argmax can handle

KeyError: 'Dimension "predicted_eaches_dim_0" does not have coordinates'

This is what the array looks like:

Thank you.

This seems to get rid of the coordinates.

See below:

Xarray is complaining one of the dimensions being reduced has no label, so it is impossible for idxmax to return the label at the maximum. You can use argmax to get the position where the max is instead of the label, but given you ask for the label there might be some incoherence between dims and coords, maybe the same issue as in the other question?