@@ -282,27 +282,40 @@ variables (``data_vars``), coordinates (``coords``) and attributes (``attrs``).
282
282
283
283
- ``attrs `` should be a dictionary.
284
284
285
- Let's create some fake data for the example we show above:
285
+ Let's create some fake data for the example we show above. In this
286
+ example dataset, we will represent measurements of the temperature and
287
+ pressure that were made under various conditions:
288
+
289
+ * the measurements were made on four different days;
290
+ * they were made at two separate locations, which we will represent using
291
+ their latitude and longitude; and
292
+ * they were made using instruments by three different manufacutrers, which we
293
+ will refer to as `'manufac1' `, `'manufac2' `, and `'manufac3' `.
286
294
287
295
.. ipython :: python
288
296
289
- temp = 15 + 8 * np.random.randn(2 , 2 , 3 )
290
- precip = 10 * np.random.rand(2 , 2 , 3 )
291
- lon = [[- 99.83 , - 99.32 ], [- 99.79 , - 99.23 ]]
292
- lat = [[42.25 , 42.21 ], [42.63 , 42.59 ]]
297
+ np.random.seed(0 )
298
+ temperature = 15 + 8 * np.random.randn(2 , 3 , 4 )
299
+ precipitation = 10 * np.random.rand(2 , 3 , 4 )
300
+ lon = [- 99.83 , - 99.32 ]
301
+ lat = [42.25 , 42.21 ]
302
+ instruments = [" manufac1" , " manufac2" , " manufac3" ]
303
+ time = pd.date_range(" 2014-09-06" , periods = 4 )
304
+ reference_time = pd.Timestamp(" 2014-09-05" )
293
305
294
306
# for real use cases, its good practice to supply array attributes such as
295
307
# units, but we won't bother here for the sake of brevity
296
308
ds = xr.Dataset(
297
309
{
298
- " temperature" : ([" x " , " y " , " time" ], temp ),
299
- " precipitation" : ([" x " , " y " , " time" ], precip ),
310
+ " temperature" : ([" loc " , " instrument " , " time" ], temperature ),
311
+ " precipitation" : ([" loc " , " instrument " , " time" ], precipitation ),
300
312
},
301
313
coords = {
302
- " lon" : ([" x" , " y" ], lon),
303
- " lat" : ([" x" , " y" ], lat),
304
- " time" : pd.date_range(" 2014-09-06" , periods = 3 ),
305
- " reference_time" : pd.Timestamp(" 2014-09-05" ),
314
+ " lon" : ([" loc" ], lon),
315
+ " lat" : ([" loc" ], lat),
316
+ " instrument" : instruments,
317
+ " time" : time,
318
+ " reference_time" : reference_time,
306
319
},
307
320
)
308
321
ds
@@ -387,12 +400,12 @@ example, to create this example dataset from scratch, we could have written:
387
400
.. ipython :: python
388
401
389
402
ds = xr.Dataset()
390
- ds[" temperature" ] = ((" x " , " y " , " time" ), temp )
391
- ds[" temperature_double" ] = ((" x " , " y " , " time" ), temp * 2 )
392
- ds[" precipitation" ] = ((" x " , " y " , " time" ), precip )
393
- ds.coords[" lat" ] = ((" x " , " y " ), lat)
394
- ds.coords[" lon" ] = ((" x " , " y " ), lon)
395
- ds.coords[" time" ] = pd.date_range(" 2014-09-06" , periods = 3 )
403
+ ds[" temperature" ] = ((" loc " , " instrument " , " time" ), temperature )
404
+ ds[" temperature_double" ] = ((" loc " , " instrument " , " time" ), temperature * 2 )
405
+ ds[" precipitation" ] = ((" loc " , " instrument " , " time" ), precipitation )
406
+ ds.coords[" lat" ] = ((" loc " , ), lat)
407
+ ds.coords[" lon" ] = ((" loc " , ), lon)
408
+ ds.coords[" time" ] = pd.date_range(" 2014-09-06" , periods = 4 )
396
409
ds.coords[" reference_time" ] = pd.Timestamp(" 2014-09-05" )
397
410
398
411
To change the variables in a ``Dataset ``, you can use all the standard dictionary
@@ -452,8 +465,8 @@ follow nested function calls:
452
465
453
466
# these lines are equivalent, but with pipe we can make the logic flow
454
467
# entirely from left to right
455
- plt.plot((2 * ds.temperature.sel(x = 0 )).mean(" y " ))
456
- (ds.temperature.sel(x = 0 ).pipe(lambda x : 2 * x).mean(" y " ).pipe(plt.plot))
468
+ plt.plot((2 * ds.temperature.sel(loc = 0 )).mean(" instrument " ))
469
+ (ds.temperature.sel(loc = 0 ).pipe(lambda x : 2 * x).mean(" instrument " ).pipe(plt.plot))
457
470
458
471
Both ``pipe `` and ``assign `` replicate the pandas methods of the same names
459
472
(:py:meth: `DataFrame.pipe <pandas.DataFrame.pipe> ` and
@@ -479,7 +492,7 @@ dimension and non-dimension variables:
479
492
480
493
.. ipython :: python
481
494
482
- ds.coords[" day" ] = (" time" , [6 , 7 , 8 ])
495
+ ds.coords[" day" ] = (" time" , [6 , 7 , 8 , 9 ])
483
496
ds.swap_dims({" time" : " day" })
484
497
485
498
.. _coordinates :
0 commit comments