How to update `xarray.DataArray` using `.sel()` indexer?
<p>I found the easiest way to visualize creating a <code>N-dimensional</code> <code>DataArray</code> was to make a <code>np.ndarray</code> 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 <code>xr.DataArray</code>.</p>

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

<pre><code># 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)]})
# &lt;xarray.DataArray (axis_A: 3, axis_B: 3, axis_C: 5)&gt;
# 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) &lt;U3 'B_0' 'B_1' 'B_2'
#   * axis_A   (axis_A) &lt;U3 'A_0' 'A_1' 'A_2'
#   * axis_C   (axis_C) &lt;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()
# # &lt;xarray.DataArray ()&gt;
# # array(0.0)

DA.sel(axis_A="A_1", axis_B="B_1", axis_C="C_1") = 1
# #  File "&lt;ipython-input-17-8feb7332633f&gt;", line 4
# #     DA.sel(axis_A="A_1", axis_B="B_1", axis_C="C_1") = 1
# #                                                         ^
# # SyntaxError: can't assign to function call
</code></pre>

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

<p>So instead, you need to put the arguments to <code>.sel</code> as a dictionary in <code>.loc</code> instead:</p>

<pre><code>DA.loc[dict(axis_A="A_1", axis_B="B_1", axis_C="C_1")] = 1
</code></pre>

python database numpy dataframe python-xarray