Skip to content

Commit a84ea12

Browse files
authored
some docs updates (#2746)
* Friendlier io title. * Fix lists. * Fix *args, **kwargs "inline emphasis..." * misc * Reference xarray_extras for csv writing. Closes #2289 * Add metpy accessor. Closes #461 * fix transpose docstring. Closes #2576 * Revert "Fix lists." This reverts commit 39983a5. * Revert "Fix *args, **kwargs" This reverts commit 1b9da35. * Add MetPy to related projects. * Add Weather and Climate specific page. * Add hvplot. * Note open_dataset, mfdataset open files as read-only (closes #2345). * Update metpy 1 Co-Authored-By: dcherian <[email protected]> * Update doc/weather-climate.rst Co-Authored-By: dcherian <[email protected]>
1 parent c770eec commit a84ea12

11 files changed

+202
-153
lines changed

doc/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Documentation
5252
* :doc:`reshaping`
5353
* :doc:`combining`
5454
* :doc:`time-series`
55+
* :doc:`weather-climate`
5556
* :doc:`pandas`
5657
* :doc:`io`
5758
* :doc:`dask`
@@ -70,6 +71,7 @@ Documentation
7071
reshaping
7172
combining
7273
time-series
74+
weather-climate
7375
pandas
7476
io
7577
dask

doc/io.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
.. _io:
22

3-
Serialization and IO
4-
====================
3+
Reading and writing files
4+
=========================
55

66
xarray supports direct serialization and IO to several file formats, from
77
simple :ref:`io.pickle` files to the more flexible :ref:`io.netcdf`
8-
format.
8+
format (recommended).
99

1010
.. ipython:: python
1111
:suppress:
@@ -739,11 +739,14 @@ options are listed on the PseudoNetCDF page.
739739
.. _PseudoNetCDF: http://github.com/barronh/PseudoNetCDF
740740

741741

742-
Formats supported by Pandas
743-
---------------------------
742+
CSV and other formats supported by Pandas
743+
-----------------------------------------
744744

745745
For more options (tabular formats and CSV files in particular), consider
746746
exporting your objects to pandas and using its broad range of `IO tools`_.
747+
For CSV files, one might also consider `xarray_extras`_.
748+
749+
.. _xarray_extras: https://xarray-extras.readthedocs.io/en/latest/api/csv.html
747750

748751
.. _IO tools: http://pandas.pydata.org/pandas-docs/stable/io.html
749752

doc/plotting.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ For more extensive plotting applications consider the following projects:
3939
data structures for building even complex visualizations easily." Includes
4040
native support for xarray objects.
4141

42+
- `hvplot <https://hvplot.pyviz.org/>`_: ``hvplot`` makes it very easy to produce
43+
dynamic plots (backed by ``Holoviews`` or ``Geoviews``) by adding a ``hvplot``
44+
accessor to DataArrays.
45+
4246
- `Cartopy <http://scitools.org.uk/cartopy/>`_: Provides cartographic
4347
tools.
4448

doc/related-projects.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Geosciences
1313
- `aospy <https://aospy.readthedocs.io>`_: Automated analysis and management of gridded climate data.
1414
- `infinite-diff <https://github.com/spencerahill/infinite-diff>`_: xarray-based finite-differencing, focused on gridded climate/meterology data
1515
- `marc_analysis <https://github.com/darothen/marc_analysis>`_: Analysis package for CESM/MARC experiments and output.
16+
- `MetPy <https://unidata.github.io/MetPy/dev/index.html>`_: A collection of tools in Python for reading, visualizing, and performing calculations with weather data.
1617
- `MPAS-Analysis <http://mpas-analysis.readthedocs.io>`_: Analysis for simulations produced with Model for Prediction Across Scales (MPAS) components and the Accelerated Climate Model for Energy (ACME).
1718
- `OGGM <http://oggm.org/>`_: Open Global Glacier Model
1819
- `Oocgcm <https://oocgcm.readthedocs.io/>`_: Analysis of large gridded geophysical datasets

doc/time-series.rst

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -212,140 +212,3 @@ Data that has indices outside of the given ``tolerance`` are set to ``NaN``.
212212
213213
For more examples of using grouped operations on a time dimension, see
214214
:ref:`toy weather data`.
215-
216-
217-
.. _CFTimeIndex:
218-
219-
Non-standard calendars and dates outside the Timestamp-valid range
220-
------------------------------------------------------------------
221-
222-
Through the standalone ``cftime`` library and a custom subclass of
223-
:py:class:`pandas.Index`, xarray supports a subset of the indexing
224-
functionality enabled through the standard :py:class:`pandas.DatetimeIndex` for
225-
dates from non-standard calendars commonly used in climate science or dates
226-
using a standard calendar, but outside the `Timestamp-valid range`_
227-
(approximately between years 1678 and 2262).
228-
229-
.. note::
230-
231-
As of xarray version 0.11, by default, :py:class:`cftime.datetime` objects
232-
will be used to represent times (either in indexes, as a
233-
:py:class:`~xarray.CFTimeIndex`, or in data arrays with dtype object) if
234-
any of the following are true:
235-
236-
- The dates are from a non-standard calendar
237-
- Any dates are outside the Timestamp-valid range.
238-
239-
Otherwise pandas-compatible dates from a standard calendar will be
240-
represented with the ``np.datetime64[ns]`` data type, enabling the use of a
241-
:py:class:`pandas.DatetimeIndex` or arrays with dtype ``np.datetime64[ns]``
242-
and their full set of associated features.
243-
244-
For example, you can create a DataArray indexed by a time
245-
coordinate with dates from a no-leap calendar and a
246-
:py:class:`~xarray.CFTimeIndex` will automatically be used:
247-
248-
.. ipython:: python
249-
250-
from itertools import product
251-
from cftime import DatetimeNoLeap
252-
dates = [DatetimeNoLeap(year, month, 1) for year, month in
253-
product(range(1, 3), range(1, 13))]
254-
da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo')
255-
256-
xarray also includes a :py:func:`~xarray.cftime_range` function, which enables
257-
creating a :py:class:`~xarray.CFTimeIndex` with regularly-spaced dates. For
258-
instance, we can create the same dates and DataArray we created above using:
259-
260-
.. ipython:: python
261-
262-
dates = xr.cftime_range(start='0001', periods=24, freq='MS', calendar='noleap')
263-
da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo')
264-
265-
For data indexed by a :py:class:`~xarray.CFTimeIndex` xarray currently supports:
266-
267-
- `Partial datetime string indexing`_ using strictly `ISO 8601-format`_ partial
268-
datetime strings:
269-
270-
.. ipython:: python
271-
272-
da.sel(time='0001')
273-
da.sel(time=slice('0001-05', '0002-02'))
274-
275-
- Access of basic datetime components via the ``dt`` accessor (in this case
276-
just "year", "month", "day", "hour", "minute", "second", "microsecond",
277-
"season", "dayofyear", and "dayofweek"):
278-
279-
.. ipython:: python
280-
281-
da.time.dt.year
282-
da.time.dt.month
283-
da.time.dt.season
284-
da.time.dt.dayofyear
285-
da.time.dt.dayofweek
286-
287-
- Group-by operations based on datetime accessor attributes (e.g. by month of
288-
the year):
289-
290-
.. ipython:: python
291-
292-
da.groupby('time.month').sum()
293-
294-
- Interpolation using :py:class:`cftime.datetime` objects:
295-
296-
.. ipython:: python
297-
298-
da.interp(time=[DatetimeNoLeap(1, 1, 15), DatetimeNoLeap(1, 2, 15)])
299-
300-
- Interpolation using datetime strings:
301-
302-
.. ipython:: python
303-
304-
da.interp(time=['0001-01-15', '0001-02-15'])
305-
306-
- Differentiation:
307-
308-
.. ipython:: python
309-
310-
da.differentiate('time')
311-
312-
- Serialization:
313-
314-
.. ipython:: python
315-
316-
da.to_netcdf('example-no-leap.nc')
317-
xr.open_dataset('example-no-leap.nc')
318-
319-
- And resampling along the time dimension for data indexed by a :py:class:`~xarray.CFTimeIndex`:
320-
321-
.. ipython:: python
322-
323-
da.resample(time='81T', closed='right', label='right', base=3).mean()
324-
325-
.. note::
326-
327-
328-
For some use-cases it may still be useful to convert from
329-
a :py:class:`~xarray.CFTimeIndex` to a :py:class:`pandas.DatetimeIndex`,
330-
despite the difference in calendar types. The recommended way of doing this
331-
is to use the built-in :py:meth:`~xarray.CFTimeIndex.to_datetimeindex`
332-
method:
333-
334-
.. ipython:: python
335-
:okwarning:
336-
337-
modern_times = xr.cftime_range('2000', periods=24, freq='MS', calendar='noleap')
338-
da = xr.DataArray(range(24), [('time', modern_times)])
339-
da
340-
datetimeindex = da.indexes['time'].to_datetimeindex()
341-
da['time'] = datetimeindex
342-
343-
However in this case one should use caution to only perform operations which
344-
do not depend on differences between dates (e.g. differentiation,
345-
interpolation, or upsampling with resample), as these could introduce subtle
346-
and silent errors due to the difference in calendar types between the dates
347-
encoded in your data and the dates stored in memory.
348-
349-
.. _Timestamp-valid range: https://pandas.pydata.org/pandas-docs/stable/timeseries.html#timestamp-limitations
350-
.. _ISO 8601-format: https://en.wikipedia.org/wiki/ISO_8601
351-
.. _partial datetime string indexing: https://pandas.pydata.org/pandas-docs/stable/timeseries.html#partial-string-indexing

doc/weather-climate.rst

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
.. _weather-climate:
2+
3+
Weather and climate data
4+
========================
5+
6+
.. ipython:: python
7+
:suppress:
8+
9+
import xarray as xr
10+
11+
``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 :ref:`plotting`) and support for non-standard calendars used in climate science through the ``cftime`` module (see :ref:`CFTimeIndex`). There are also a number of geosciences-focused projects that build on xarray (see :ref:`related-projects`).
12+
13+
.. _Climate and Forecast (CF) conventions: http://cfconventions.org
14+
15+
.. _metpy_accessor:
16+
17+
CF-compliant coordinate variables
18+
---------------------------------
19+
20+
`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.
21+
22+
.. _`MetPy`: https://unidata.github.io/MetPy/dev/index.html
23+
.. _`their documentation`: https://unidata.github.io/MetPy/dev/tutorials/xarray_tutorial.html#coordinates
24+
.. _`Cartopy`: https://scitools.org.uk/cartopy/docs/latest/crs/projections.html
25+
26+
.. _CFTimeIndex:
27+
28+
Non-standard calendars and dates outside the Timestamp-valid range
29+
------------------------------------------------------------------
30+
31+
Through the standalone ``cftime`` library and a custom subclass of
32+
:py:class:`pandas.Index`, xarray supports a subset of the indexing
33+
functionality enabled through the standard :py:class:`pandas.DatetimeIndex` for
34+
dates from non-standard calendars commonly used in climate science or dates
35+
using a standard calendar, but outside the `Timestamp-valid range`_
36+
(approximately between years 1678 and 2262).
37+
38+
.. note::
39+
40+
As of xarray version 0.11, by default, :py:class:`cftime.datetime` objects
41+
will be used to represent times (either in indexes, as a
42+
:py:class:`~xarray.CFTimeIndex`, or in data arrays with dtype object) if
43+
any of the following are true:
44+
45+
- The dates are from a non-standard calendar
46+
- Any dates are outside the Timestamp-valid range.
47+
48+
Otherwise pandas-compatible dates from a standard calendar will be
49+
represented with the ``np.datetime64[ns]`` data type, enabling the use of a
50+
:py:class:`pandas.DatetimeIndex` or arrays with dtype ``np.datetime64[ns]``
51+
and their full set of associated features.
52+
53+
For example, you can create a DataArray indexed by a time
54+
coordinate with dates from a no-leap calendar and a
55+
:py:class:`~xarray.CFTimeIndex` will automatically be used:
56+
57+
.. ipython:: python
58+
59+
from itertools import product
60+
from cftime import DatetimeNoLeap
61+
dates = [DatetimeNoLeap(year, month, 1) for year, month in
62+
product(range(1, 3), range(1, 13))]
63+
da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo')
64+
65+
xarray also includes a :py:func:`~xarray.cftime_range` function, which enables
66+
creating a :py:class:`~xarray.CFTimeIndex` with regularly-spaced dates. For
67+
instance, we can create the same dates and DataArray we created above using:
68+
69+
.. ipython:: python
70+
71+
dates = xr.cftime_range(start='0001', periods=24, freq='MS', calendar='noleap')
72+
da = xr.DataArray(np.arange(24), coords=[dates], dims=['time'], name='foo')
73+
74+
For data indexed by a :py:class:`~xarray.CFTimeIndex` xarray currently supports:
75+
76+
- `Partial datetime string indexing`_ using strictly `ISO 8601-format`_ partial
77+
datetime strings:
78+
79+
.. ipython:: python
80+
81+
da.sel(time='0001')
82+
da.sel(time=slice('0001-05', '0002-02'))
83+
84+
- Access of basic datetime components via the ``dt`` accessor (in this case
85+
just "year", "month", "day", "hour", "minute", "second", "microsecond",
86+
"season", "dayofyear", and "dayofweek"):
87+
88+
.. ipython:: python
89+
90+
da.time.dt.year
91+
da.time.dt.month
92+
da.time.dt.season
93+
da.time.dt.dayofyear
94+
da.time.dt.dayofweek
95+
96+
- Group-by operations based on datetime accessor attributes (e.g. by month of
97+
the year):
98+
99+
.. ipython:: python
100+
101+
da.groupby('time.month').sum()
102+
103+
- Interpolation using :py:class:`cftime.datetime` objects:
104+
105+
.. ipython:: python
106+
107+
da.interp(time=[DatetimeNoLeap(1, 1, 15), DatetimeNoLeap(1, 2, 15)])
108+
109+
- Interpolation using datetime strings:
110+
111+
.. ipython:: python
112+
113+
da.interp(time=['0001-01-15', '0001-02-15'])
114+
115+
- Differentiation:
116+
117+
.. ipython:: python
118+
119+
da.differentiate('time')
120+
121+
- Serialization:
122+
123+
.. ipython:: python
124+
125+
da.to_netcdf('example-no-leap.nc')
126+
xr.open_dataset('example-no-leap.nc')
127+
128+
- And resampling along the time dimension for data indexed by a :py:class:`~xarray.CFTimeIndex`:
129+
130+
.. ipython:: python
131+
132+
da.resample(time='81T', closed='right', label='right', base=3).mean()
133+
134+
.. note::
135+
136+
137+
For some use-cases it may still be useful to convert from
138+
a :py:class:`~xarray.CFTimeIndex` to a :py:class:`pandas.DatetimeIndex`,
139+
despite the difference in calendar types. The recommended way of doing this
140+
is to use the built-in :py:meth:`~xarray.CFTimeIndex.to_datetimeindex`
141+
method:
142+
143+
.. ipython:: python
144+
:okwarning:
145+
146+
modern_times = xr.cftime_range('2000', periods=24, freq='MS', calendar='noleap')
147+
da = xr.DataArray(range(24), [('time', modern_times)])
148+
da
149+
datetimeindex = da.indexes['time'].to_datetimeindex()
150+
da['time'] = datetimeindex
151+
152+
However in this case one should use caution to only perform operations which
153+
do not depend on differences between dates (e.g. differentiation,
154+
interpolation, or upsampling with resample), as these could introduce subtle
155+
and silent errors due to the difference in calendar types between the dates
156+
encoded in your data and the dates stored in memory.
157+
158+
.. _Timestamp-valid range: https://pandas.pydata.org/pandas-docs/stable/timeseries.html#timestamp-limitations
159+
.. _ISO 8601-format: https://en.wikipedia.org/wiki/ISO_8601
160+
.. _partial datetime string indexing: https://pandas.pydata.org/pandas-docs/stable/timeseries.html#partial-string-indexing

doc/whats-new.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Bug fixes
9696
from higher frequencies to lower frequencies. Datapoints outside the bounds
9797
of the original time coordinate are now filled with NaN (:issue:`2197`). By
9898
`Spencer Clark <https://github.com/spencerkclark>`_.
99-
- Line plots with the `x` argument set to a non-dimensional coord now plot the correct data for 1D DataArrays.
99+
- Line plots with the ``x`` argument set to a non-dimensional coord now plot the correct data for 1D DataArrays.
100100
(:issue:`27251). By `Tom Nicholas <http://github.com/TomNicholas>`_.
101101
- Subtracting a scalar ``cftime.datetime`` object from a
102102
:py:class:`CFTimeIndex` now results in a :py:class:`pandas.TimedeltaIndex`

0 commit comments

Comments
 (0)