@@ -53,10 +53,12 @@ DataArray:
53
53
arr[0 , 0 ]
54
54
arr[:, [2 , 1 ]]
55
55
56
+ Attributes are persisted in all indexing operations.
57
+
56
58
.. warning ::
57
59
58
60
Positional indexing deviates from the NumPy when indexing with multiple
59
- arrays like ``arr[[0, 1], [0, 1]] ``, as described in :ref: `indexing details `.
61
+ arrays like ``arr[[0, 1], [0, 1]] ``, as described in :ref: `orthogonal `.
60
62
See :ref: `pointwise indexing ` and :py:meth: `~xray.Dataset.isel_points ` for more on this functionality.
61
63
62
64
xray also supports label-based indexing, just like pandas. Because
@@ -81,6 +83,7 @@ Setting values with label based indexing is also supported:
81
83
arr.loc[' 2000-01-01' , [' IL' , ' IN' ]] = - 10
82
84
arr
83
85
86
+
84
87
Indexing with labeled dimensions
85
88
--------------------------------
86
89
@@ -191,39 +194,126 @@ index labels along a dimension dropped:
191
194
192
195
``drop `` is both a ``Dataset `` and ``DataArray `` method.
193
196
194
- .. _ indexing details :
197
+ .. _ nearest neighbor lookups :
195
198
196
- Indexing details
197
- ----------------
199
+ Nearest neighbor lookups
200
+ ------------------------
198
201
199
- Like pandas, whether array indexing returns a view or a copy of the underlying
200
- data depends entirely on numpy:
202
+ The label based selection methods :py:meth: `~xray.Dataset.sel `,
203
+ :py:meth: `~xray.Dataset.reindex ` and :py:meth: `~xray.Dataset.reindex_like ` all
204
+ support a ``method `` keyword argument. The method parameter allows for
205
+ enabling nearest neighbor (inexact) lookups by use of the methods ``'pad' ``,
206
+ ``'backfill' `` or ``'nearest' ``:
201
207
202
- * Indexing with a single label or a slice returns a view.
203
- * Indexing with a vector of array labels returns a copy.
208
+ .. ipython :: python
209
+
210
+ data = xray.DataArray([1 , 2 , 3 ], dims = ' x' )
211
+ data.sel(x = [1.1 , 1.9 ], method = ' nearest' )
212
+ data.sel(x = 0.1 , method = ' backfill' )
213
+ data.reindex(x = [0.5 , 1 , 1.5 , 2 , 2.5 ], method = ' pad' )
204
214
205
- Attributes are persisted in array indexing:
215
+ Using ``method='nearest' `` or a scalar argument with ``.sel() `` requires pandas
216
+ version 0.16 or newer.
217
+
218
+ The method parameter is not yet supported if any of the arguments
219
+ to ``.sel() `` is a ``slice `` object:
220
+
221
+ .. ipython ::
222
+ :verbatim:
223
+
224
+ In [1]: data.sel(x=slice(1, 3), method='nearest')
225
+ NotImplementedError
226
+
227
+ However, you don't need to use ``method `` to do inexact slicing. Slicing
228
+ already returns all values inside the range (inclusive), as long as the index
229
+ labels are monotonic increasing:
230
+
231
+ .. ipython :: python
232
+
233
+ data.sel(x = slice (0.9 , 3.1 ))
234
+
235
+ Indexing axes with monotonic decreasing labels also works, as long as the
236
+ ``slice `` or ``.loc `` arguments are also decreasing:
206
237
207
238
.. ipython :: python
208
239
209
- arr2 = arr.copy()
210
- arr2.attrs[' units' ] = ' meters'
211
- arr2[0 , 0 ].attrs
240
+ reversed_data = data[::- 1 ]
241
+ reversed_data.loc[3.1 :0.9 ]
242
+
243
+ .. _masking with where :
244
+
245
+ Masking with ``where ``
246
+ ----------------------
247
+
248
+ Indexing methods on xray objects generally return a subset of the original data.
249
+ However, it is sometimes useful to select an object with the same shape as the
250
+ original data, but with some elements masked. To do this type of selection in
251
+ xray, use :py:meth: `~xray.DataArray.where `:
252
+
253
+ .. ipython :: python
254
+
255
+ arr = xray.DataArray(np.arange(16 ).reshape(4 , 4 ), dims = [' x' , ' y' ])
256
+ arr.where(arr.x + arr.y < 4 )
257
+
258
+ This is particularly useful for ragged indexing of multi-dimensional data,
259
+ e.g., to apply a 2D mask to an image. Note that ``where `` follows all the
260
+ usual xray broadcasting and alignment rules for binary operations (e.g.,
261
+ ``+ ``) between the object being indexed and the condition, as described in
262
+ :ref: `comput `:
263
+
264
+ .. ipython :: python
265
+
266
+ arr.where(arr.y < 2 )
267
+
268
+ Multi-dimensional indexing
269
+ --------------------------
270
+
271
+ Xray does not yet support efficient routines for generalized multi-dimensional
272
+ indexing or regridding. However, we are definitely interested in adding support
273
+ for this in the future (see :issue: `475 ` for the ongoing discussion).
274
+
275
+ .. _copies vs views :
276
+
277
+ Copies vs. views
278
+ ----------------
279
+
280
+ Whether array indexing returns a view or a copy of the underlying
281
+ data depends on the nature of the labels. For positional (integer)
282
+ indexing, xray follows the same rules as NumPy:
283
+
284
+ * Positional indexing with only integers and slices returns a view.
285
+ * Positional indexing with arrays or lists returns a copy.
286
+
287
+ The rules for label based indexing are more complex:
288
+
289
+ * Label-based indexing with only slices returns a view.
290
+ * Label-based indexing with arrays returns a copy.
291
+ * Label-based indexing with scalars returns a view or a copy, depending
292
+ upon if the corresponding positional indexer can be represented as an
293
+ integer or a slice object.
294
+
295
+ .. _orthogonal :
296
+
297
+ Orthogonal (outer) vs. vectorized indexing
298
+ ------------------------------------------
212
299
213
300
Indexing with xray objects has one important difference from indexing numpy
214
301
arrays: you can only use one-dimensional arrays to index xray objects, and
215
302
each indexer is applied "orthogonally" along independent axes, instead of
216
- using numpy's advanced broadcasting. This means you can do indexing like this,
217
- which would require slightly more awkward syntax with numpy arrays:
303
+ using numpy's broadcasting rules to vectorize indexers. This means you can do
304
+ indexing like this, which would require slightly more awkward syntax with
305
+ numpy arrays:
218
306
219
307
.. ipython :: python
220
308
221
309
arr[arr[' time.day' ] > 1 , arr[' space' ] != ' IL' ]
222
310
223
- This is a much simpler model than numpy's `advanced indexing `__,
224
- and is basically the only model that works for labeled arrays. If you would
225
- like to do array indexing, you can always index ``.values `` directly
226
- instead:
311
+ This is a much simpler model than numpy's `advanced indexing `__. If you would
312
+ like to do advanced-style array indexing in xray, you have several options:
313
+
314
+ * :ref: `pointwise indexing `
315
+ * :ref: `masking with where `
316
+ * Index the underlying NumPy directly array using ``.values ``:
227
317
228
318
__ http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
229
319
@@ -242,6 +332,10 @@ original values are subset to the index labels still found in the new labels,
242
332
and values corresponding to new labels not found in the original object are
243
333
in-filled with `NaN `.
244
334
335
+ Xray operations that combine multiple xray objects generally automatically
336
+ align their arguments. However, manual alignment can be useful for greater
337
+ control.
338
+
245
339
To reindex a particular dimension, use :py:meth: `~xray.DataArray.reindex `:
246
340
247
341
.. ipython :: python
@@ -289,103 +383,3 @@ Both ``reindex_like`` and ``align`` work interchangeably between
289
383
other = xray.DataArray([' a' , ' b' , ' c' ], dims = ' other' )
290
384
# this is a no-op, because there are no shared dimension names
291
385
ds.reindex_like(other)
292
-
293
- .. _nearest neighbor lookups :
294
-
295
- Nearest neighbor lookups
296
- ------------------------
297
-
298
- The label based selection methods :py:meth: `~xray.Dataset.sel `,
299
- :py:meth: `~xray.Dataset.reindex ` and :py:meth: `~xray.Dataset.reindex_like ` all
300
- support a ``method `` keyword argument. The method parameter allows for
301
- enabling nearest neighbor (inexact) lookups by use of the methods ``'pad' ``,
302
- ``'backfill' `` or ``'nearest' ``:
303
-
304
- .. use verbatim because I can't seem to install pandas 0.16.1 on RTD :(
305
-
306
- .. .. ipython::
307
- :verbatim:
308
- In [35]: data = xray.DataArray([1, 2, 3], dims='x')
309
- In [36]: data.sel(x=[1.1, 1.9], method='nearest')
310
- Out[36]:
311
- <xray.DataArray (x: 2)>
312
- array([2, 3])
313
- Coordinates:
314
- * x (x) int64 1 2
315
- In [37]: data.sel(x=0.1, method='backfill')
316
- Out[37]:
317
- <xray.DataArray ()>
318
- array(2)
319
- Coordinates:
320
- x int64 1
321
- In [38]: data.reindex(x=[0.5, 1, 1.5, 2, 2.5], method='pad')
322
- Out[38]:
323
- <xray.DataArray (x: 5)>
324
- array([1, 2, 2, 3, 3])
325
- Coordinates:
326
- * x (x) float64 0.5 1.0 1.5 2.0 2.5
327
-
328
- .. ipython :: python
329
-
330
- data = xray.DataArray([1 , 2 , 3 ], dims = ' x' )
331
- data.sel(x = [1.1 , 1.9 ], method = ' nearest' )
332
- data.sel(x = 0.1 , method = ' backfill' )
333
- data.reindex(x = [0.5 , 1 , 1.5 , 2 , 2.5 ], method = ' pad' )
334
-
335
- Using ``method='nearest' `` or a scalar argument with ``.sel() `` requires pandas
336
- version 0.16 or newer.
337
-
338
- The method parameter is not yet supported if any of the arguments
339
- to ``.sel() `` is a ``slice `` object:
340
-
341
- .. ipython ::
342
- :verbatim:
343
-
344
- In [1]: data.sel(x=slice(1, 3), method='nearest')
345
- NotImplementedError
346
-
347
- However, you don't need to use ``method `` to do inexact slicing. Slicing
348
- already returns all values inside the range (inclusive), as long as the index
349
- labels are monotonic increasing:
350
-
351
- .. ipython :: python
352
-
353
- data.sel(x = slice (0.9 , 3.1 ))
354
-
355
- Indexing axes with monotonic decreasing labels also works, as long as the
356
- ``slice `` or ``.loc `` arguments are also decreasing:
357
-
358
- .. ipython :: python
359
-
360
- reversed_data = data[::- 1 ]
361
- reversed_data.loc[3.1 :0.9 ]
362
-
363
- Masking with ``where ``
364
- ----------------------
365
-
366
- Indexing methods on xray objects generally return a subset of the original data.
367
- However, it is sometimes useful to select an object with the same shape as the
368
- original data, but with some elements masked. To do this type of selection in
369
- xray, use :py:meth: `~xray.DataArray.where `:
370
-
371
- .. ipython :: python
372
-
373
- arr = xray.DataArray(np.arange(16 ).reshape(4 , 4 ), dims = [' x' , ' y' ])
374
- arr.where(arr.x + arr.y < 4 )
375
-
376
- This is particularly useful for ragged indexing of multi-dimensional data,
377
- e.g., to apply a 2D mask to an image. Note that ``where `` follows all the
378
- usual xray broadcasting and alignment rules for binary operations (e.g.,
379
- ``+ ``) between the object being indexed and the condition, as described in
380
- :ref: `comput `:
381
-
382
- .. ipython :: python
383
-
384
- arr.where(arr.y < 2 )
385
-
386
- Multi-dimensional indexing
387
- --------------------------
388
-
389
- Xray does not yet support efficient routines for generalized multi-dimensional
390
- indexing or regridding. However, we are definitely interested in adding support
391
- for this in the future (see :issue: `475 ` for the ongoing discussion).
0 commit comments