Weather and climate data¶
xarray can leverage metadata that follows the Climate and Forecast (CF) conventions if present. Examples include automatic labelling of plots with descriptive names and units if proper metadata is present (see Plotting) and support for non-standard calendars used in climate science through the
cftime module (see Non-standard calendars and dates outside the Timestamp-valid range). There are also a number of geosciences-focused projects that build on xarray (see Xarray related projects).
CF-compliant coordinate variables¶
MetPy adds a
metpy accessor that allows accessing coordinates with appropriate CF metadata using generic names
time. There is also a cartopy_crs attribute that provides projection information, parsed from the appropriate CF metadata, as a Cartopy projection object. See their documentation for more information.
Non-standard calendars and dates outside the Timestamp-valid range¶
Through the standalone
cftime library and a custom subclass of
pandas.Index, xarray supports a subset of the indexing
functionality enabled through the standard
dates from non-standard calendars commonly used in climate science or dates
using a standard calendar, but outside the Timestamp-valid range
(approximately between years 1678 and 2262).
As of xarray version 0.11, by default,
will be used to represent times (either in indexes, as a
CFTimeIndex, or in data arrays with dtype object) if
any of the following are true:
The dates are from a non-standard calendar
Any dates are outside the Timestamp-valid range.
Otherwise pandas-compatible dates from a standard calendar will be
represented with the
np.datetime64[ns] data type, enabling the use of a
pandas.DatetimeIndex or arrays with dtype
and their full set of associated features.
For example, you can create a DataArray indexed by a time
coordinate with dates from a no-leap calendar and a
CFTimeIndex will automatically be used:
In : from itertools import product In : from cftime import DatetimeNoLeap In : dates = [DatetimeNoLeap(year, month, 1) for year, month in ...: product(range(1, 3), range(1, 13))] ...: In : da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo')
In : dates = xr.cftime_range(start='0001', periods=24, freq='MS', calendar='noleap') In : da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo')
strftime() we can also easily generate formatted strings from
the datetime values of a
CFTimeIndex directly or through the
dt() accessor for a
using the same formatting as the standard datetime.strftime convention .
In : dates.strftime('%c') Out: Index(['Tue Jan 1 00:00:00 1', 'Fri Feb 1 00:00:00 1', 'Fri Mar 1 00:00:00 1', 'Mon Apr 1 00:00:00 1', 'Wed May 1 00:00:00 1', 'Sat Jun 1 00:00:00 1', 'Mon Jul 1 00:00:00 1', 'Thu Aug 1 00:00:00 1', 'Sun Sep 1 00:00:00 1', 'Tue Oct 1 00:00:00 1', 'Fri Nov 1 00:00:00 1', 'Sun Dec 1 00:00:00 1', 'Wed Jan 1 00:00:00 2', 'Sat Feb 1 00:00:00 2', 'Sat Mar 1 00:00:00 2', 'Tue Apr 1 00:00:00 2', 'Thu May 1 00:00:00 2', 'Sun Jun 1 00:00:00 2', 'Tue Jul 1 00:00:00 2', 'Fri Aug 1 00:00:00 2', 'Mon Sep 1 00:00:00 2', 'Wed Oct 1 00:00:00 2', 'Sat Nov 1 00:00:00 2', 'Mon Dec 1 00:00:00 2'], dtype='object') In : da['time'].dt.strftime('%Y%m%d')