Skip to content

Commit 1808be4

Browse files
Illviljanmax-sixtykeewis
authored
Improve Dataset and DataArray docstrings (#4532)
* dataarray init docs to class * dataset init docs to class doc * Add a dataset example. * Add an example to dataarray * formatting * formatting * formatting * formatting * Remove imports, use dicts instead of dataarray * remove imports, use dict() instead of {}, add attrs. * add attrs * typo * print original dataset as well. * print original dataarray as well * Update whats-new.rst * Move whatsnew to 0.16.2 * Update doc/whats-new.rst * explicitly enable numpydoc and disable googledoc * black * Use isel instead of where. * Use isel instead of where. Co-authored-by: Maximilian Roos <[email protected]> Co-authored-by: Maximilian Roos <[email protected]> Co-authored-by: keewis <[email protected]> Co-authored-by: Keewis <[email protected]>
1 parent adc55ac commit 1808be4

File tree

4 files changed

+239
-113
lines changed

4 files changed

+239
-113
lines changed

doc/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111

112112
autodoc_typehints = "none"
113113

114+
napoleon_google_docstring = False
115+
napoleon_numpy_docstring = True
116+
114117
napoleon_use_param = False
115118
napoleon_use_rtype = True
116119
napoleon_preprocess_types = True

doc/whats-new.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,13 @@ Bug fixes
6262
Documentation
6363
~~~~~~~~~~~~~
6464

65+
- Update the docstring of :py:class:`DataArray` and :py:class:`Dataset`.
66+
(:pull:`4532`);
67+
By `Jimmy Westling <https://github.com/illviljan>`_.
6568
- Raise a more informative error when :py:meth:`DataArray.to_dataframe` is
66-
is called on a scalar (:issue:`4228`). By `Pieter Gijsbers <https://github.com/pgijsbers>`_.
69+
is called on a scalar, (:issue:`4228`);
70+
By `Pieter Gijsbers <https://github.com/pgijsbers>`_.
71+
6772

6873
Internal Changes
6974
~~~~~~~~~~~~~~~~

xarray/core/dataarray.py

Lines changed: 112 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -216,27 +216,125 @@ def __setitem__(self, key, value) -> None:
216216
class DataArray(AbstractArray, DataWithCoords):
217217
"""N-dimensional array with labeled coordinates and dimensions.
218218
219-
DataArray provides a wrapper around numpy ndarrays that uses labeled
220-
dimensions and coordinates to support metadata aware operations. The API is
221-
similar to that for the pandas Series or DataFrame, but DataArray objects
222-
can have any number of dimensions, and their contents have fixed data
223-
types.
219+
DataArray provides a wrapper around numpy ndarrays that uses
220+
labeled dimensions and coordinates to support metadata aware
221+
operations. The API is similar to that for the pandas Series or
222+
DataFrame, but DataArray objects can have any number of dimensions,
223+
and their contents have fixed data types.
224224
225225
Additional features over raw numpy arrays:
226226
227227
- Apply operations over dimensions by name: ``x.sum('time')``.
228-
- Select or assign values by integer location (like numpy): ``x[:10]``
229-
or by label (like pandas): ``x.loc['2014-01-01']`` or
228+
- Select or assign values by integer location (like numpy):
229+
``x[:10]`` or by label (like pandas): ``x.loc['2014-01-01']`` or
230230
``x.sel(time='2014-01-01')``.
231-
- Mathematical operations (e.g., ``x - y``) vectorize across multiple
232-
dimensions (known in numpy as "broadcasting") based on dimension names,
233-
regardless of their original order.
234-
- Keep track of arbitrary metadata in the form of a Python dictionary:
235-
``x.attrs``
231+
- Mathematical operations (e.g., ``x - y``) vectorize across
232+
multiple dimensions (known in numpy as "broadcasting") based on
233+
dimension names, regardless of their original order.
234+
- Keep track of arbitrary metadata in the form of a Python
235+
dictionary: ``x.attrs``
236236
- Convert to a pandas Series: ``x.to_series()``.
237237
238-
Getting items from or doing mathematical operations with a DataArray
239-
always returns another DataArray.
238+
Getting items from or doing mathematical operations with a
239+
DataArray always returns another DataArray.
240+
241+
Parameters
242+
----------
243+
data : array_like
244+
Values for this array. Must be an ``numpy.ndarray``, ndarray
245+
like, or castable to an ``ndarray``. If a self-described xarray
246+
or pandas object, attempts are made to use this array's
247+
metadata to fill in other unspecified arguments. A view of the
248+
array's data is used instead of a copy if possible.
249+
coords : sequence or dict of array_like, optional
250+
Coordinates (tick labels) to use for indexing along each
251+
dimension. The following notations are accepted:
252+
253+
- mapping {dimension name: array-like}
254+
- sequence of tuples that are valid arguments for
255+
``xarray.Variable()``
256+
- (dims, data)
257+
- (dims, data, attrs)
258+
- (dims, data, attrs, encoding)
259+
260+
Additionally, it is possible to define a coord whose name
261+
does not match the dimension name, or a coord based on multiple
262+
dimensions, with one of the following notations:
263+
264+
- mapping {coord name: DataArray}
265+
- mapping {coord name: Variable}
266+
- mapping {coord name: (dimension name, array-like)}
267+
- mapping {coord name: (tuple of dimension names, array-like)}
268+
269+
dims : hashable or sequence of hashable, optional
270+
Name(s) of the data dimension(s). Must be either a hashable
271+
(only for 1D data) or a sequence of hashables with length equal
272+
to the number of dimensions. If this argument is omitted,
273+
dimension names default to ``['dim_0', ... 'dim_n']``.
274+
name : str or None, optional
275+
Name of this array.
276+
attrs : dict_like or None, optional
277+
Attributes to assign to the new instance. By default, an empty
278+
attribute dictionary is initialized.
279+
280+
Examples
281+
--------
282+
Create data:
283+
284+
>>> np.random.seed(0)
285+
>>> temperature = 15 + 8 * np.random.randn(2, 2, 3)
286+
>>> precipitation = 10 * np.random.rand(2, 2, 3)
287+
>>> lon = [[-99.83, -99.32], [-99.79, -99.23]]
288+
>>> lat = [[42.25, 42.21], [42.63, 42.59]]
289+
>>> time = pd.date_range("2014-09-06", periods=3)
290+
>>> reference_time = pd.Timestamp("2014-09-05")
291+
292+
Initialize a dataarray with multiple dimensions:
293+
294+
>>> da = xr.DataArray(
295+
... data=temperature,
296+
... dims=["x", "y", "time"],
297+
... coords=dict(
298+
... lon=(["x", "y"], lon),
299+
... lat=(["x", "y"], lat),
300+
... time=time,
301+
... reference_time=reference_time,
302+
... ),
303+
... attrs=dict(
304+
... description="Ambient temperature.",
305+
... units="degC",
306+
... ),
307+
... )
308+
>>> da
309+
<xarray.DataArray (x: 2, y: 2, time: 3)>
310+
array([[[29.11241877, 18.20125767, 22.82990387],
311+
[32.92714559, 29.94046392, 7.18177696]],
312+
<BLANKLINE>
313+
[[22.60070734, 13.78914233, 14.17424919],
314+
[18.28478802, 16.15234857, 26.63418806]]])
315+
Coordinates:
316+
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
317+
lat (x, y) float64 42.25 42.21 42.63 42.59
318+
* time (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
319+
reference_time datetime64[ns] 2014-09-05
320+
Dimensions without coordinates: x, y
321+
Attributes:
322+
description: Ambient temperature.
323+
units: degC
324+
325+
Find out where the coldest temperature was:
326+
327+
>>> da.isel(da.argmin(...))
328+
<xarray.DataArray ()>
329+
array(7.18177696)
330+
Coordinates:
331+
lon float64 -99.32
332+
lat float64 42.21
333+
time datetime64[ns] 2014-09-08
334+
reference_time datetime64[ns] 2014-09-05
335+
Attributes:
336+
description: Ambient temperature.
337+
units: degC
240338
"""
241339

242340
_cache: Dict[str, Any]
@@ -274,45 +372,6 @@ def __init__(
274372
indexes: Dict[Hashable, pd.Index] = None,
275373
fastpath: bool = False,
276374
):
277-
"""
278-
Parameters
279-
----------
280-
data : array_like
281-
Values for this array. Must be an ``numpy.ndarray``, ndarray like,
282-
or castable to an ``ndarray``. If a self-described xarray or pandas
283-
object, attempts are made to use this array's metadata to fill in
284-
other unspecified arguments. A view of the array's data is used
285-
instead of a copy if possible.
286-
coords : sequence or dict of array_like, optional
287-
Coordinates (tick labels) to use for indexing along each dimension.
288-
The following notations are accepted:
289-
290-
- mapping {dimension name: array-like}
291-
- sequence of tuples that are valid arguments for xarray.Variable()
292-
- (dims, data)
293-
- (dims, data, attrs)
294-
- (dims, data, attrs, encoding)
295-
296-
Additionally, it is possible to define a coord whose name
297-
does not match the dimension name, or a coord based on multiple
298-
dimensions, with one of the following notations:
299-
300-
- mapping {coord name: DataArray}
301-
- mapping {coord name: Variable}
302-
- mapping {coord name: (dimension name, array-like)}
303-
- mapping {coord name: (tuple of dimension names, array-like)}
304-
305-
dims : hashable or sequence of hashable, optional
306-
Name(s) of the data dimension(s). Must be either a hashable (only
307-
for 1D data) or a sequence of hashables with length equal to the
308-
number of dimensions. If this argument is omitted, dimension names
309-
default to ``['dim_0', ... 'dim_n']``.
310-
name : str or None, optional
311-
Name of this array.
312-
attrs : dict_like or None, optional
313-
Attributes to assign to the new instance. By default, an empty
314-
attribute dictionary is initialized.
315-
"""
316375
if fastpath:
317376
variable = data
318377
assert dims is None

0 commit comments

Comments
 (0)