Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update `xarray.DataArray` using `.sel()` indexer?

I found the easiest way to visualize creating a N-dimensional DataArray was to make a np.ndarray and then fill in the values by the coordinates I've created. When I tried to actually do it, I couldn't figure out how to update the xr.DataArray.

How can I update the xr.DataArray I've initialized using the labels I've created? My actual data is a much more complicated dataset but this sums up what I'm trying to do. I can use .loc but sometimes my ndarrays get huge and complicated where I don't really know the order of the dims.

# Construct DataArray
DA = xr.DataArray(np.ndarray((3,3,5)), dims=["axis_A","axis_B","axis_C"], coords={"axis_A":["A_%d"%_ for _ in range(3)],
                                                                                  "axis_B":["B_%d"%_ for _ in range(3)],
                                                                                 "axis_C":["C_%d"%_ for _ in range(5)]})
# <xarray.DataArray (axis_A: 3, axis_B: 3, axis_C: 5)>
# array([[[ 0.,  0.,  0.,  0.,  0.],
#         [ 0.,  0.,  0.,  0.,  0.],
#         [ 0.,  0.,  0.,  0.,  0.]],

#        [[ 0.,  0.,  0.,  0.,  0.],
#         [ 0.,  0.,  0.,  0.,  0.],
#         [ 0.,  0.,  0.,  0.,  0.]],

#        [[ 0.,  0.,  0.,  0.,  0.],
#         [ 0.,  0.,  0.,  0.,  0.],
#         [ 0.,  0.,  0.,  0.,  0.]]])
# Coordinates:
#   * axis_B   (axis_B) <U3 'B_0' 'B_1' 'B_2'
#   * axis_A   (axis_A) <U3 'A_0' 'A_1' 'A_2'
#   * axis_C   (axis_C) <U3 'C_0' 'C_1' 'C_2' 'C_3' 'C_4'

# # Update? 
DA.sel(axis_A="A_1", axis_B="B_1", axis_C="C_1").values = 1
DA.max()
# # <xarray.DataArray ()>
# # array(0.0)

DA.sel(axis_A="A_1", axis_B="B_1", axis_C="C_1") = 1
# #  File "<ipython-input-17-8feb7332633f>", line 4
# #     DA.sel(axis_A="A_1", axis_B="B_1", axis_C="C_1") = 1
# #                                                         ^
# # SyntaxError: can't assign to function call
like image 579
O.rka Avatar asked Oct 13 '16 19:10

O.rka


People also ask

How do I access Xarray data?

xarray offers extremely flexible indexing routines that combine the best features of NumPy and pandas for data selection. The most basic way to access elements of a DataArray object is to use Python's [] syntax, such as array[i, j] , where i and j are both integers.

What is Python Xarray?

xarray (formerly xray) is an open source project and Python package that makes working with labelled multi-dimensional arrays simple, efficient, and fun!

How do I drop a dimension in Xarray?

In future versions of xarray (v0. 9 and later), you will be able to drop coordinates when indexing by writing drop=True , e.g., ds['bar']. sel(x=1, drop=True) .


1 Answers

This is really awkward, due to the unfortunate limitation of Python syntax that keyword arguments are not supported in inside square bracket.

So instead, you need to put the arguments to .sel as a dictionary in .loc instead:

DA.loc[dict(axis_A="A_1", axis_B="B_1", axis_C="C_1")] = 1
like image 192
shoyer Avatar answered Oct 03 '22 00:10

shoyer