#!/usr/bin/env python # coding: utf-8 # # What's new in xarray? # # An udpate for PyData NYC 2019. # In[1]: import xarray as xr xr.__version__ # ## Rich HTML repr # In[2]: xr.set_options(display_style="html") xr.tutorial.load_dataset('rasm').chunk() # #   # #   # #   # #   # #   # #   # #   # # ## Flexible Array Support (NEP18) # # Create sparse array and put it in an xarray Dataset. # In[3]: import sparse coords = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] data = [10, 20, 30, 40, 50] s = sparse.COO(coords, data, shape=(5, 5)) das = xr.DataArray(s, dims=['lat', 'lon']) das # In[4]: das.mean(dim='lon') # Put it inside a dask array. # In[5]: das.chunk() # Create sparse array from pandas multiindex. # In[6]: import pandas as pd import numpy as np tuples = [('a', 0), ('a', 2), ('b', 1), ('c', 3)] index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second']) s = pd.Series(np.zeros(len(index)), index=index) s # In[7]: xr.DataArray.from_series(s) # In[8]: das = xr.DataArray.from_series(s, sparse=True) das # In[9]: das.sel(first='a', second=0).data.todense() # Hypothetically should work for `cupy` arrays, `pint` arrays, etc. # #   # #   # #   # #   # #   # #   # #   # # ## CFTimeIndex # # For non-standard calendars. # In[10]: day = np.arange(0, 360*10) ds = xr.Dataset(coords={'time': ('time', day, {'units': 'days since 4000-01-01', 'calendar': '360_day'})}) ds = xr.decode_cf(ds) ds # In[11]: ds.indexes # In[12]: ds.groupby('time.month') # In[ ]: