@@ -209,20 +209,23 @@ simultaneously, returning a new dataset:
209
209
210
210
.. ipython :: python
211
211
212
- da = xr.DataArray(np.random.rand(4 , 3 ),
213
- [(' time' , pd.date_range(' 2000-01-01' , periods = 4 )),
214
- (' space' , [' IA' , ' IL' , ' IN' ])])
215
- ds = da.to_dataset(name = ' foo' )
212
+ da = xr.DataArray(
213
+ np.random.rand(4 , 3 ),
214
+ [
215
+ (" time" , pd.date_range(" 2000-01-01" , periods = 4 )),
216
+ (" space" , [" IA" , " IL" , " IN" ]),
217
+ ],
218
+ )
219
+ ds = da.to_dataset(name = " foo" )
216
220
ds.isel(space = [0 ], time = [0 ])
217
- ds.sel(time = ' 2000-01-01' )
221
+ ds.sel(time = " 2000-01-01" )
218
222
219
223
Positional indexing on a dataset is not supported because the ordering of
220
224
dimensions in a dataset is somewhat ambiguous (it can vary between different
221
225
arrays). However, you can do normal indexing with dimension names:
222
226
223
227
.. ipython :: python
224
228
225
-
226
229
ds[dict (space = [0 ], time = [0 ])]
227
230
ds.loc[dict (time = ' 2000-01-01' )]
228
231
@@ -248,7 +251,6 @@ Any variables with these dimensions are also dropped:
248
251
249
252
ds.drop_dims(' time' )
250
253
251
-
252
254
.. _masking with where :
253
255
254
256
Masking with ``where ``
@@ -326,8 +328,12 @@ MATLAB, or after using the :py:func:`numpy.ix_` helper:
326
328
327
329
.. ipython :: python
328
330
329
- da = xr.DataArray(np.arange(12 ).reshape((3 , 4 )), dims = [' x' , ' y' ],
330
- coords = {' x' : [0 , 1 , 2 ], ' y' : [' a' , ' b' , ' c' , ' d' ]})
331
+
332
+ da = xr.DataArray(
333
+ np.arange(12 ).reshape((3 , 4 )),
334
+ dims = [" x" , " y" ],
335
+ coords = {" x" : [0 , 1 , 2 ], " y" : [" a" , " b" , " c" , " d" ]},
336
+ )
331
337
da
332
338
da[[0 , 1 ], [1 , 1 ]]
333
339
@@ -410,43 +416,57 @@ can use indexing with ``.loc`` :
410
416
411
417
.. ipython :: python
412
418
413
- ds = xr.tutorial.open_dataset(' air_temperature' )
414
419
415
- # add an empty 2D dataarray
416
- ds[' empty' ]= xr.full_like(ds.air.mean(' time' ),fill_value = 0 )
420
+ ds = xr.tutorial.open_dataset(" air_temperature" )
421
+
422
+ # add an empty 2D dataarray
423
+ ds[" empty" ] = xr.full_like(ds.air.mean(" time" ), fill_value = 0 )
417
424
418
- # modify one grid point using loc()
419
- ds[' empty' ].loc[dict (lon = 260 , lat = 30 )] = 100
425
+ # modify one grid point using loc()
426
+ ds[" empty" ].loc[dict (lon = 260 , lat = 30 )] = 100
420
427
421
- # modify a 2D region using loc()
422
- lc = ds.coords[' lon' ]
423
- la = ds.coords[' lat' ]
424
- ds[' empty' ].loc[dict (lon = lc[(lc> 220 )& (lc< 260 )], lat = la[(la> 20 )& (la< 60 )])] = 100
428
+ # modify a 2D region using loc()
429
+ lc = ds.coords[" lon" ]
430
+ la = ds.coords[" lat" ]
431
+ ds[" empty" ].loc[
432
+ dict (lon = lc[(lc > 220 ) & (lc < 260 )], lat = la[(la > 20 ) & (la < 60 )])
433
+ ] = 100
425
434
426
435
or :py:meth: `~xarray.where `:
427
436
428
437
.. ipython :: python
429
438
430
- # modify one grid point using xr.where()
431
- ds[' empty' ] = xr.where((ds.coords[' lat' ]== 20 )& (ds.coords[' lon' ]== 260 ), 100 , ds[' empty' ])
439
+ # modify one grid point using xr.where()
440
+ ds[" empty" ] = xr.where(
441
+ (ds.coords[" lat" ] == 20 ) & (ds.coords[" lon" ] == 260 ), 100 , ds[" empty" ]
442
+ )
443
+
444
+ # or modify a 2D region using xr.where()
445
+ mask = (
446
+ (ds.coords[" lat" ] > 20 )
447
+ & (ds.coords[" lat" ] < 60 )
448
+ & (ds.coords[" lon" ] > 220 )
449
+ & (ds.coords[" lon" ] < 260 )
450
+ )
451
+ ds[" empty" ] = xr.where(mask, 100 , ds[" empty" ])
432
452
433
- # or modify a 2D region using xr.where()
434
- mask = (ds.coords[' lat' ]> 20 )& (ds.coords[' lat' ]< 60 )& (ds.coords[' lon' ]> 220 )& (ds.coords[' lon' ]< 260 )
435
- ds[' empty' ] = xr.where(mask, 100 , ds[' empty' ])
436
453
437
454
438
455
Vectorized indexing can also be used to assign values to xarray object.
439
456
440
457
.. ipython :: python
441
458
442
- da = xr.DataArray(np.arange(12 ).reshape((3 , 4 )), dims = [' x' , ' y' ],
443
- coords = {' x' : [0 , 1 , 2 ], ' y' : [' a' , ' b' , ' c' , ' d' ]})
459
+ da = xr.DataArray(
460
+ np.arange(12 ).reshape((3 , 4 )),
461
+ dims = [" x" , " y" ],
462
+ coords = {" x" : [0 , 1 , 2 ], " y" : [" a" , " b" , " c" , " d" ]},
463
+ )
444
464
da
445
465
da[0 ] = - 1 # assignment with broadcasting
446
466
da
447
467
448
- ind_x = xr.DataArray([0 , 1 ], dims = [' x ' ])
449
- ind_y = xr.DataArray([0 , 1 ], dims = [' y ' ])
468
+ ind_x = xr.DataArray([0 , 1 ], dims = [" x " ])
469
+ ind_y = xr.DataArray([0 , 1 ], dims = [" y " ])
450
470
da[ind_x, ind_y] = - 2 # assign -2 to (ix, iy) = (0, 0) and (1, 1)
451
471
da
452
472
@@ -508,10 +528,10 @@ flexible indexing. The following is an example of the pointwise indexing:
508
528
509
529
.. ipython :: python
510
530
511
- da = xr.DataArray(np.arange(56 ).reshape((7 , 8 )), dims = [' x ' , ' y ' ])
531
+ da = xr.DataArray(np.arange(56 ).reshape((7 , 8 )), dims = [" x " , " y " ])
512
532
da
513
- da.isel(x = xr.DataArray([0 , 1 , 6 ], dims = ' z ' ),
514
- y = xr.DataArray([ 0 , 1 , 0 ], dims = ' z ' ))
533
+ da.isel(x = xr.DataArray([0 , 1 , 6 ], dims = " z " ), y = xr.DataArray([ 0 , 1 , 0 ], dims = " z " ))
534
+
515
535
516
536
where three elements at ``(ix, iy) = ((0, 0), (1, 1), (6, 0)) `` are selected
517
537
and mapped along a new dimension ``z ``.
@@ -521,23 +541,27 @@ you can supply a :py:class:`~xarray.DataArray` with a coordinate,
521
541
522
542
.. ipython :: python
523
543
524
- da.isel(x = xr.DataArray([0 , 1 , 6 ], dims = ' z' ,
525
- coords = {' z' : [' a' , ' b' , ' c' ]}),
526
- y = xr.DataArray([0 , 1 , 0 ], dims = ' z' ))
527
-
544
+ da.isel(
545
+ x = xr.DataArray([0 , 1 , 6 ], dims = " z" , coords = {" z" : [" a" , " b" , " c" ]}),
546
+ y = xr.DataArray([0 , 1 , 0 ], dims = " z" ),
547
+ )
548
+
528
549
Analogously, label-based pointwise-indexing is also possible by the ``.sel ``
529
550
method:
530
551
531
552
.. ipython :: python
532
553
533
- da = xr.DataArray(np.random.rand(4 , 3 ),
534
- [(' time' , pd.date_range(' 2000-01-01' , periods = 4 )),
535
- (' space' , [' IA' , ' IL' , ' IN' ])])
536
- times = xr.DataArray(pd.to_datetime([' 2000-01-03' , ' 2000-01-02' , ' 2000-01-01' ]),
537
- dims = ' new_time' )
538
- da.sel(space = xr.DataArray([' IA' , ' IL' , ' IN' ], dims = [' new_time' ]),
539
- time = times)
540
-
554
+ da = xr.DataArray(
555
+ np.random.rand(4 , 3 ),
556
+ [
557
+ (" time" , pd.date_range(" 2000-01-01" , periods = 4 )),
558
+ (" space" , [" IA" , " IL" , " IN" ]),
559
+ ],
560
+ )
561
+ times = xr.DataArray(
562
+ pd.to_datetime([" 2000-01-03" , " 2000-01-02" , " 2000-01-01" ]), dims = " new_time"
563
+ )
564
+ da.sel(space = xr.DataArray([" IA" , " IL" , " IN" ], dims = [" new_time" ]), time = times)
541
565
542
566
.. _align and reindex :
543
567
@@ -635,12 +659,16 @@ through the :py:attr:`~xarray.DataArray.indexes` attribute.
635
659
636
660
.. ipython :: python
637
661
638
- da = xr.DataArray(np.random.rand(4 , 3 ),
639
- [(' time' , pd.date_range(' 2000-01-01' , periods = 4 )),
640
- (' space' , [' IA' , ' IL' , ' IN' ])])
662
+ da = xr.DataArray(
663
+ np.random.rand(4 , 3 ),
664
+ [
665
+ (" time" , pd.date_range(" 2000-01-01" , periods = 4 )),
666
+ (" space" , [" IA" , " IL" , " IN" ]),
667
+ ],
668
+ )
641
669
da
642
670
da.indexes
643
- da.indexes[' time' ]
671
+ da.indexes[" time" ]
644
672
645
673
Use :py:meth: `~xarray.DataArray.get_index ` to get an index for a dimension,
646
674
falling back to a default :py:class: `pandas.RangeIndex ` if it has no coordinate
@@ -694,32 +722,31 @@ pandas:
694
722
695
723
.. ipython :: python
696
724
697
- midx = pd.MultiIndex.from_product([list (' abc' ), [0 , 1 ]],
698
- names = (' one' , ' two' ))
699
- mda = xr.DataArray(np.random.rand(6 , 3 ),
700
- [(' x' , midx), (' y' , range (3 ))])
701
- mda
702
- mda.sel(x = (list (' ab' ), [0 ]))
725
+
726
+ midx = pd.MultiIndex.from_product([list (" abc" ), [0 , 1 ]], names = (" one" , " two" ))
727
+ mda = xr.DataArray(np.random.rand(6 , 3 ), [(" x" , midx), (" y" , range (3 ))])
728
+ mda
729
+ mda.sel(x = (list (" ab" ), [0 ]))
703
730
704
731
You can also select multiple elements by providing a list of labels or tuples or
705
732
a slice of tuples:
706
733
707
734
.. ipython :: python
708
735
709
- mda.sel(x = [(' a' , 0 ), (' b' , 1 )])
736
+ mda.sel(x = [(' a' , 0 ), (' b' , 1 )])
710
737
711
738
Additionally, xarray supports dictionaries:
712
739
713
740
.. ipython :: python
714
741
715
- mda.sel(x = {' one' : ' a' , ' two' : 0 })
742
+ mda.sel(x = {' one' : ' a' , ' two' : 0 })
716
743
717
744
For convenience, ``sel `` also accepts multi-index levels directly
718
745
as keyword arguments:
719
746
720
747
.. ipython :: python
721
748
722
- mda.sel(one = ' a' , two = 0 )
749
+ mda.sel(one = ' a' , two = 0 )
723
750
724
751
Note that using ``sel `` it is not possible to mix a dimension
725
752
indexer with level indexers for that dimension
@@ -731,7 +758,7 @@ multi-index is reduced to a single index.
731
758
732
759
.. ipython :: python
733
760
734
- mda.loc[{' one' : ' a' }, ... ]
761
+ mda.loc[{' one' : ' a' }, ... ]
735
762
736
763
Unlike pandas, xarray does not guess whether you provide index levels or
737
764
dimensions when using ``loc `` in some ambiguous cases. For example, for
0 commit comments