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