@@ -454,7 +454,7 @@ def assign_coords(self, coords=None, **coords_kwargs):
454
454
455
455
Examples
456
456
--------
457
- Convert longitude coordinates from 0-359 to -180-179:
457
+ Convert `DataArray` longitude coordinates from 0-359 to -180-179:
458
458
459
459
>>> da = xr.DataArray(
460
460
... np.random.rand(4),
@@ -494,6 +494,54 @@ def assign_coords(self, coords=None, **coords_kwargs):
494
494
495
495
>>> _ = da.assign_coords({"lon_2": ("lon", lon_2)})
496
496
497
+ Note the same method applies to `Dataset` objects.
498
+
499
+ Convert `Dataset` longitude coordinates from 0-359 to -180-179:
500
+
501
+ >>> temperature = np.linspace(20, 32, num=16).reshape(2, 2, 4)
502
+ >>> precipitation = 2 * np.identity(4).reshape(2, 2, 4)
503
+ >>> ds = xr.Dataset(
504
+ ... data_vars=dict(
505
+ ... temperature=(["x", "y", "time"], temperature),
506
+ ... precipitation=(["x", "y", "time"], precipitation),
507
+ ... ),
508
+ ... coords=dict(
509
+ ... lon=(["x", "y"], [[260.17, 260.68], [260.21, 260.77]]),
510
+ ... lat=(["x", "y"], [[42.25, 42.21], [42.63, 42.59]]),
511
+ ... time=pd.date_range("2014-09-06", periods=4),
512
+ ... reference_time=pd.Timestamp("2014-09-05"),
513
+ ... ),
514
+ ... attrs=dict(description="Weather-related data"),
515
+ ... )
516
+ >>> ds
517
+ <xarray.Dataset>
518
+ Dimensions: (x: 2, y: 2, time: 4)
519
+ Coordinates:
520
+ lon (x, y) float64 260.2 260.7 260.2 260.8
521
+ lat (x, y) float64 42.25 42.21 42.63 42.59
522
+ * time (time) datetime64[ns] 2014-09-06 2014-09-07 ... 2014-09-09
523
+ reference_time datetime64[ns] 2014-09-05
524
+ Dimensions without coordinates: x, y
525
+ Data variables:
526
+ temperature (x, y, time) float64 20.0 20.8 21.6 22.4 ... 30.4 31.2 32.0
527
+ precipitation (x, y, time) float64 2.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 2.0
528
+ Attributes:
529
+ description: Weather-related data
530
+ >>> ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180))
531
+ <xarray.Dataset>
532
+ Dimensions: (x: 2, y: 2, time: 4)
533
+ Coordinates:
534
+ lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
535
+ lat (x, y) float64 42.25 42.21 42.63 42.59
536
+ * time (time) datetime64[ns] 2014-09-06 2014-09-07 ... 2014-09-09
537
+ reference_time datetime64[ns] 2014-09-05
538
+ Dimensions without coordinates: x, y
539
+ Data variables:
540
+ temperature (x, y, time) float64 20.0 20.8 21.6 22.4 ... 30.4 31.2 32.0
541
+ precipitation (x, y, time) float64 2.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 2.0
542
+ Attributes:
543
+ description: Weather-related data
544
+
497
545
Notes
498
546
-----
499
547
Since ``coords_kwargs`` is a dictionary, the order of your arguments
0 commit comments