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 xarray as xr
import numpy as np
import pandas as pd
import seaborn as sns # pandas aware plotting library
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: (x: 2, y: 3)
Coordinates:
* x (x) <U1 'a' 'b'
Dimensions without coordinates: y
Data variables:
foo (x, y) float64 -1.295 0.4137 0.2767 -0.472 -0.01396 -0.3625
bar (x) int64 1 2
baz float64 3.142
In [2]: df = ds.to_dataframe()
In [3]: df.head()
Out[3]:
foo bar baz
x y
a 0 -1.294524 1 3.141593
1 0.413738 1 3.141593
2 0.276662 1 3.141593
b 0 -0.472035 2 3.141593
1 -0.013960 2 3.141593
In [4]: df.describe()