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 x
, y
, vertical
and 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 pandas.DatetimeIndex
for
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).
Note
As of xarray version 0.11, by default, cftime.datetime
objects
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 np.datetime64[ns]
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 [1]: from itertools import product
In [2]: from cftime import DatetimeNoLeap
In [3]: dates = [DatetimeNoLeap(year, month, 1) for year, month in
...: product(range(1, 3), range(1, 13))]
...:
In [4]: da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo')
xarray also includes a cftime_range()
function, which enables
creating a CFTimeIndex
with regularly-spaced dates. For
instance, we can create the same dates and DataArray we created above using:
In [5]: dates = xr.cftime_range(start='0001', periods=24, freq='MS', calendar='noleap')
In [6]: da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo')
With strftime()
we can also easily generate formatted strings from
the datetime values of a CFTimeIndex
directly or through the
dt()
accessor for a DataArray
using the same formatting as the standard datetime.strftime convention .
In [7]: dates.strftime('%c')
Out[7]:
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 [8]: da['time'].dt.strftime('%Y%m%d')