Why how is [] different from loc[] ? #7528
-
|
There is no documentation for loc: https://docs.xarray.dev/en/stable/generated/xarray.DataArray.loc.html So I tried this: This prints: (2,2) and (3,2)! Why does |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Hi @johann-petrak - sorry that there's nothing in the docstring for The table here states the difference between Your example DataArray has the integers 1, 2, 3 in the labels for the In [5]: a1
Out[5]:
<xarray.DataArray (rows: 10, cols: 4)>
array([[ 0.63574108, -0.66717472, 0.00915737, 2.87035267],
[ 0.14699325, 0.0393495 , -0.3325451 , 1.72347164],
[-1.08075426, -0.25283328, -0.22643869, -0.29025223],
[ 0.64201311, 2.01351354, 0.64635188, -1.00025037],
[-0.58669366, -0.06947171, 0.11525736, 0.33282736],
[ 1.11675031, -1.26638065, -1.05421688, -0.95508593],
[-1.94780685, 0.26467594, -1.73543175, -0.70611111],
[-1.89087655, 0.38499429, 0.27008176, -0.56125357],
[-1.20288908, 1.39705547, -0.54248866, -0.51891135],
[-0.22672511, -2.07950408, 1.20850539, -1.81501284]])
Coordinates:
* rows (rows) int64 0 1 2 3 4 5 6 7 8 9
* cols (cols) <U4 'col1' 'col2' 'col3' 'col4'So in your example This difference is easier to see if we alter your example to use different labels for the rows: a1 = xr.DataArray(
np.random.randn(10,4), dims=["rows", "cols"],
coords=[
np.arange(10) + 10, # note the change here
["col1", "col2", "col3", "col4"],
]
)<xarray.DataArray (rows: 10, cols: 4)>
array([[-1.70024691, 1.54145511, -0.18230258, -1.22038387],
[ 0.99081958, 0.22927578, -0.01439966, 0.31420335],
[ 0.61803064, -0.88470156, -0.68377578, 0.6172416 ],
[ 0.51741572, 0.21337269, 0.29452151, 1.72603969],
[ 0.28126145, 0.58515375, -0.11026639, 0.15560316],
[-0.76637363, 0.67396002, -1.47838801, -0.1330614 ],
[ 0.74503108, -0.30663846, 1.17255296, 0.46380143],
[ 0.61045344, -2.10286191, -0.88960886, 1.87087362],
[-0.18107632, -1.08329405, 0.44506773, 1.55144911],
[ 0.15745851, -1.24268807, -0.55151284, 0.87646351]])
Coordinates:
* rows (rows) int64 10 11 12 13 14 15 16 17 18 19
* cols (cols) <U4 'col1' 'col2' 'col3' 'col4'No elements were returned along the The elements with labels matching If you want to mix positional indexing and label-based indexing you will need to supply each in a different method call, though you can chain these methods together on one line, like this: In [20]: a1[1:3].loc[:, ["col1", "col2"]]
Out[20]:
<xarray.DataArray (rows: 2, cols: 2)>
array([[ 0.99081958, 0.22927578],
[ 0.61803064, -0.88470156]])
Coordinates:
* rows (rows) int64 11 12
* cols (cols) <U4 'col1' 'col2'This is probably what you expected to happen. I personally prefer to make the dimensions I'm indexing along explicit by using In [24]: a1.isel(rows=slice(1,3)).sel(cols=["col1", "col2"])
Out[24]:
<xarray.DataArray (rows: 2, cols: 2)>
array([[ 0.99081958, 0.22927578],
[ 0.61803064, -0.88470156]])
Coordinates:
* rows (rows) int64 11 12
* cols (cols) <U4 'col1' 'col2'I recommend first reading the page on indexing for all indexing-related queries. |
Beta Was this translation helpful? Give feedback.
Hi @johann-petrak - sorry that there's nothing in the docstring for
DataArray.loc, it should at the very least link to our general page on Indexing, which covers.locas well as other indexing methods.The table here states the difference between
[]and.loc[]- the former indexes by position and the latter by label.Your example DataArray has the integers 1, 2, 3 in the labels for the
rowscoordinate: