Toy weather data¶
Here is an example of how to easily manipulate a toy weather dataset using xarray and other recommended Python libraries:
Shared setup:
import numpy as np
import pandas as pd
import seaborn as sns # pandas aware plotting library
import xarray as xr
np.random.seed(123)
times = pd.date_range('2000-01-01', '2001-12-31', name='time')
annual_cycle = np.sin(2 * np.pi * (times.dayofyear.values / 365.25 - 0.28))
base = 10 + 15 * annual_cycle.reshape(-1, 1)
tmin_values = base + 3 * np.random.randn(annual_cycle.size, 3)
tmax_values = base + 10 + 3 * np.random.randn(annual_cycle.size, 3)
ds = xr.Dataset({'tmin': (('time', 'location'), tmin_values),
'tmax': (('time', 'location'), tmax_values)},
{'time': times, 'location': ['IA', 'IN', 'IL']})
Examine a dataset with pandas and seaborn¶
In [1]: ds
Out[1]:
<xarray.Dataset>
Dimensions: (location: 3, time: 731)
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2001-12-31
* location (location) <U2 'IA' 'IN' 'IL'
Data variables:
tmin (time, location) float64 -8.037 -1.788 -3.932 ... -1.346 -4.544
tmax (time, location) float64 12.98 3.31 6.779 ... 6.636 3.343 3.805
In [2]: df = ds.to_dataframe()
In [3]: df.head()
Out[3]:
tmin tmax
location time
IA 2000-01-01 -8.037369 12.980549
2000-01-02 -9.341157 0.447856
2000-01-03 -12.139719 5.322699
2000-01-04 -7.492914 1.889425
2000-01-05 -0.447129 0.791176
In [4]: df.describe()