Skip to content

Commit e68b38e

Browse files
add second exercise
1 parent abe6992 commit e68b38e

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

content/xarray.rst

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ The resulting plot would look like this:
283283

284284
We can modify the plots by passing additional arguments to the ``.plot()`` method. Since we haven't discussed the plotting library matplotlib in this course, we will not go into further detail here. You can find more information in the `Xarray documentation <https://xarray.pydata.org/en/stable/plotting.html>`_.
285285

286-
Exercises 1 (if time allows)
287-
----------------------------
286+
Exercises 1
287+
-----------
288288

289289
.. challenge:: Exercises: Xarray-1
290290

@@ -371,16 +371,61 @@ To save the dataset as a NetCDF file, use the ``.to_netcdf()`` method: ::
371371
ds.to_netcdf('weather_data.nc')
372372

373373

374-
Exercises 2 (if time allows)
375-
----------------------------
374+
Exercises 2
375+
-----------
376376

377377
.. challenge:: Exercises: Xarray-2
378378

379-
Provide two 3D numpy arrays and let participants turn them into an Xarray Dataset with the correct dimensions and coordinates.
379+
Let's change from clmate science to finance for this example. We assume we want to put the stock prices and trading volumes of three companies over ten days in one dataset. Create an Xarray Dataset that uses time and company as dimensions and contains two DataArrays: ``stock_price`` and ``trading_volume``. You can choose the values for the stock prices and trading volumes yourself. As a last thing, add the currency of the stock prices as an attribute to the Dataset.
380380

381381
.. solution:: Solutions: Xarray-2
382382

383-
Solution to Exercise 2 coming soon.
383+
We can use a script similar to this one: ::
384+
385+
import xarray as xr
386+
import numpy as np
387+
388+
time = [
389+
"2023-01-01",
390+
"2023-01-02",
391+
"2023-01-03",
392+
"2023-01-04",
393+
"2023-01-05",
394+
"2023-01-06",
395+
"2023-01-07",
396+
"2023-01-08",
397+
"2023-01-09",
398+
"2023-01-10",
399+
]
400+
companies = ["AAPL", "GOOGL", "MSFT"]
401+
stock_prices = np.random.normal(loc=[100, 1500, 200], scale=[10, 50, 20], size=(10, 3))
402+
trading_volumes = np.random.randint(1000, 10000, size=(10, 3))
403+
ds = xr.Dataset(
404+
{
405+
"stock_price": (["time", "company"], stock_prices),
406+
"trading_volume": (["time", "company"], trading_volumes),
407+
},
408+
coords={"time": time, "company": companies},
409+
attrs={"currency": "USD"},
410+
)
411+
print(ds)
412+
413+
The output should then resemble this: ::
414+
415+
$ python exercise.py
416+
<xarray.Dataset> Size: 940B
417+
Dimensions: (time: 10, company: 3)
418+
Coordinates:
419+
* time (time) <U10 400B '2023-01-01' '2023-01-02' ... '2023-01-10'
420+
* company (company) <U5 60B 'AAPL' 'GOOGL' 'MSFT'
421+
Data variables:
422+
stock_price (time, company) float64 240B 101.1 1.572e+03 ... 217.8
423+
trading_volume (time, company) int64 240B 1214 7911 4578 ... 4338 6861 6958
424+
Attributes:
425+
currency: USD
426+
427+
428+
384429

385430

386431
Advanced Topics

0 commit comments

Comments
 (0)