In [1]:
import numpy as np
import pandas as pd
import xarray as xr
import seaborn as sns
In [2]:
np.random.seed(123)
In [3]:
times = pd.date_range('2000-01-01', '2001-12-31', name='time')
annual_cycle = np.sin(2 * np.pi * (times.dayofyear / 365.25 - 0.28))
In [4]:
base = 10 + 15 * annual_cycle.reshape(-1, 1)
In [31]:
tmin_values = base + 3 * np.random.randn(annual_cycle.size, 3)
tmax_values = base + 100 + 3 * np.random.randn(annual_cycle.size, 3)
In [35]:
ds = xr.Dataset({'tmin': (('time', 'location'), tmin_values),
'tmax': (('time', 'location'), tmax_values)},
{'time': times, 'location': ['IA', 'IN', 'IL']})
In [7]:
ds
Out[7]:
In [36]:
df = ds.to_dataframe()
In [50]:
df.head()
Out[50]:
In [38]:
df.describe()
Out[38]:
In [39]:
ds.mean(dim='location').to_dataframe().plot()
Out[39]:
In [26]:
import matplotlib.pyplot as plt
%pylab inline
plt.show()
In [47]:
a = sns.pairplot(df.reset_index(), vars=ds.data_vars)
In [43]:
sns.set(style="ticks", color_codes=True)
In [48]:
ds.data_vars
Out[48]:
In [51]:
freeze = (ds['tmin'] <= 0).groupby('time.month').mean('time')
In [55]:
freeze.to_pandas().plot()
Out[55]:
In [56]:
monthly_avg = ds.resample('1MS', dim='time', how='mean')
In [63]:
monthly_avg.sel(location='IA').to_dataframe().plot(style='s-')
Out[63]:
In [65]:
climatology = ds.groupby('time.month').mean('time')
In [66]:
anomalies = ds.groupby('time.month') - climatology
In [71]:
anomalies.mean('location').to_dataframe()[['tmin', 'tmax']].plot()
Out[71]:
In [72]:
some_missing = ds.tmin.sel(time=ds['time.day'] > 15).reindex_like(ds)
In [73]:
filled = some_missing.groupby('time.month').fillna(climatology.tmin)
In [74]:
both = xr.Dataset({'some_missing': some_missing, 'filled': filled})
In [76]:
df = both.sel(time='2000').mean('location').reset_coords(drop=True).to_dataframe()
In [77]:
df[['filled', 'some_missing']].plot()
Out[77]:
In [ ]: