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
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.
xarray (formerly xray) is an open source project and Python package that makes working with labelled multi-dimensional arrays simple, efficient, and fun!
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) .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With