Skip to content

Commit dc533c8

Browse files
author
Greg Behm
committed
[docs] amend Dataset.assign_coords example (pydata#6336)
* remove np.random uses * fix one copy-paste artifact 'description: Temperature data'
1 parent 8434785 commit dc533c8

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

xarray/core/common.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -494,46 +494,47 @@ def assign_coords(self, coords=None, **coords_kwargs):
494494
495495
Convert `Dataset` longitude coordinates from 0-359 to -180-179:
496496
497-
>>> np.random.seed(0)
497+
>>> temperature = np.linspace(20, 32, num=16).reshape(2, 2, 4)
498+
>>> precipitation = 2 * np.identity(4).reshape(2, 2, 4)
498499
>>> ds = xr.Dataset(
499500
... data_vars=dict(
500-
... temperature=(["x", "y", "time"], 15 + 8 * np.random.randn(2, 2, 3)),
501-
... precipitation=(["x", "y", "time"], 10 * np.random.rand(2, 2, 3)),
501+
... temperature=(["x", "y", "time"], temperature),
502+
... precipitation=(["x", "y", "time"], precipitation),
502503
... ),
503504
... coords=dict(
504505
... lon=(["x", "y"], [[260.17, 260.68], [260.21, 260.77]]),
505506
... lat=(["x", "y"], [[42.25, 42.21], [42.63, 42.59]]),
506-
... time=pd.date_range("2014-09-06", periods=3),
507-
... reference_time=pd.Timestamp("2014-09-05")
507+
... time = pd.date_range("2014-09-06", periods=4),
508+
... reference_time = pd.Timestamp("2014-09-05")
508509
... ),
509510
... attrs=dict(description="Weather-related data"),
510511
... )
511512
>>> ds
512513
<xarray.Dataset>
513-
Dimensions: (x: 2, y: 2, time: 3)
514+
Dimensions: (x: 2, y: 2, time: 4)
514515
Coordinates:
515516
lon (x, y) float64 260.2 260.7 260.2 260.8
516517
lat (x, y) float64 42.25 42.21 42.63 42.59
517-
* time (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
518+
* time (time) datetime64[ns] 2014-09-06 2014-09-07 ... 2014-09-09
518519
reference_time datetime64[ns] 2014-09-05
519520
Dimensions without coordinates: x, y
520521
Data variables:
521-
temperature (x, y, time) float64 29.11 18.2 22.83 ... 18.28 16.15 26.63
522-
precipitation (x, y, time) float64 5.68 9.256 0.7104 ... 7.992 4.615 7.805
522+
temperature (x, y, time) float64 20.0 20.8 21.6 22.4 ... 30.4 31.2 32.0
523+
precipitation (x, y, time) float64 2.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 2.0
523524
Attributes:
524-
description: Temperature data
525+
description: Weather-related data
525526
>>> ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180))
526527
<xarray.Dataset>
527-
Dimensions: (x: 2, y: 2, time: 3)
528+
Dimensions: (x: 2, y: 2, time: 4)
528529
Coordinates:
529530
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
530531
lat (x, y) float64 42.25 42.21 42.63 42.59
531-
* time (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
532+
* time (time) datetime64[ns] 2014-09-06 2014-09-07 ... 2014-09-09
532533
reference_time datetime64[ns] 2014-09-05
533534
Dimensions without coordinates: x, y
534535
Data variables:
535-
temperature (x, y, time) float64 29.11 18.2 22.83 ... 18.28 16.15 26.63
536-
precipitation (x, y, time) float64 5.68 9.256 0.7104 ... 7.992 4.615 7.805
536+
temperature (x, y, time) float64 20.0 20.8 21.6 22.4 ... 30.4 31.2 32.0
537+
precipitation (x, y, time) float64 2.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 2.0
537538
Attributes:
538539
description: Weather-related data
539540

0 commit comments

Comments
 (0)