Skip to content

Commit f6d0f84

Browse files
[docs] add Dataset.assign_coords example (#6336) (#6558)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4a53e41 commit f6d0f84

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

xarray/core/common.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def assign_coords(self, coords=None, **coords_kwargs):
454454
455455
Examples
456456
--------
457-
Convert longitude coordinates from 0-359 to -180-179:
457+
Convert `DataArray` longitude coordinates from 0-359 to -180-179:
458458
459459
>>> da = xr.DataArray(
460460
... np.random.rand(4),
@@ -494,6 +494,54 @@ def assign_coords(self, coords=None, **coords_kwargs):
494494
495495
>>> _ = da.assign_coords({"lon_2": ("lon", lon_2)})
496496
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+
497545
Notes
498546
-----
499547
Since ``coords_kwargs`` is a dictionary, the order of your arguments

0 commit comments

Comments
 (0)