Skip to content

Commit 8834afa

Browse files
authored
Apply blackdoc to the documentation (#4012)
* replace tabs with spaces * fix some invalid code * add missing prompts * apply blackdoc * reformat the plotting docs code * whats-new.rst entry
1 parent 4e196f7 commit 8834afa

21 files changed

+917
-755
lines changed

doc/combining.rst

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ Combining data
44
--------------
55

66
.. ipython:: python
7-
:suppress:
7+
:suppress:
88
99
import numpy as np
1010
import pandas as pd
1111
import xarray as xr
12+
1213
np.random.seed(123456)
1314
1415
* For combining datasets or data arrays along a single dimension, see concatenate_.
@@ -28,11 +29,10 @@ that dimension:
2829

2930
.. ipython:: python
3031
31-
arr = xr.DataArray(np.random.randn(2, 3),
32-
[('x', ['a', 'b']), ('y', [10, 20, 30])])
32+
arr = xr.DataArray(np.random.randn(2, 3), [("x", ["a", "b"]), ("y", [10, 20, 30])])
3333
arr[:, :1]
3434
# this resembles how you would use np.concatenate
35-
xr.concat([arr[:, :1], arr[:, 1:]], dim='y')
35+
xr.concat([arr[:, :1], arr[:, 1:]], dim="y")
3636
3737
In addition to combining along an existing dimension, ``concat`` can create a
3838
new dimension by stacking lower dimensional arrays together:
@@ -41,30 +41,30 @@ new dimension by stacking lower dimensional arrays together:
4141
4242
arr[0]
4343
# to combine these 1d arrays into a 2d array in numpy, you would use np.array
44-
xr.concat([arr[0], arr[1]], 'x')
44+
xr.concat([arr[0], arr[1]], "x")
4545
4646
If the second argument to ``concat`` is a new dimension name, the arrays will
4747
be concatenated along that new dimension, which is always inserted as the first
4848
dimension:
4949

5050
.. ipython:: python
5151
52-
xr.concat([arr[0], arr[1]], 'new_dim')
52+
xr.concat([arr[0], arr[1]], "new_dim")
5353
5454
The second argument to ``concat`` can also be an :py:class:`~pandas.Index` or
5555
:py:class:`~xarray.DataArray` object as well as a string, in which case it is
5656
used to label the values along the new dimension:
5757

5858
.. ipython:: python
5959
60-
xr.concat([arr[0], arr[1]], pd.Index([-90, -100], name='new_dim'))
60+
xr.concat([arr[0], arr[1]], pd.Index([-90, -100], name="new_dim"))
6161
6262
Of course, ``concat`` also works on ``Dataset`` objects:
6363

6464
.. ipython:: python
6565
66-
ds = arr.to_dataset(name='foo')
67-
xr.concat([ds.sel(x='a'), ds.sel(x='b')], 'x')
66+
ds = arr.to_dataset(name="foo")
67+
xr.concat([ds.sel(x="a"), ds.sel(x="b")], "x")
6868
6969
:py:func:`~xarray.concat` has a number of options which provide deeper control
7070
over which variables are concatenated and how it handles conflicting variables
@@ -84,16 +84,16 @@ To combine variables and coordinates between multiple ``DataArray`` and/or
8484

8585
.. ipython:: python
8686
87-
xr.merge([ds, ds.rename({'foo': 'bar'})])
88-
xr.merge([xr.DataArray(n, name='var%d' % n) for n in range(5)])
87+
xr.merge([ds, ds.rename({"foo": "bar"})])
88+
xr.merge([xr.DataArray(n, name="var%d" % n) for n in range(5)])
8989
9090
If you merge another dataset (or a dictionary including data array objects), by
9191
default the resulting dataset will be aligned on the **union** of all index
9292
coordinates:
9393

9494
.. ipython:: python
9595
96-
other = xr.Dataset({'bar': ('x', [1, 2, 3, 4]), 'x': list('abcd')})
96+
other = xr.Dataset({"bar": ("x", [1, 2, 3, 4]), "x": list("abcd")})
9797
xr.merge([ds, other])
9898
9999
This ensures that ``merge`` is non-destructive. ``xarray.MergeError`` is raised
@@ -116,7 +116,7 @@ used in the :py:class:`~xarray.Dataset` constructor:
116116

117117
.. ipython:: python
118118
119-
xr.Dataset({'a': arr[:-1], 'b': arr[1:]})
119+
xr.Dataset({"a": arr[:-1], "b": arr[1:]})
120120
121121
.. _combine:
122122

@@ -131,8 +131,8 @@ are filled with ``NaN``. For example:
131131

132132
.. ipython:: python
133133
134-
ar0 = xr.DataArray([[0, 0], [0, 0]], [('x', ['a', 'b']), ('y', [-1, 0])])
135-
ar1 = xr.DataArray([[1, 1], [1, 1]], [('x', ['b', 'c']), ('y', [0, 1])])
134+
ar0 = xr.DataArray([[0, 0], [0, 0]], [("x", ["a", "b"]), ("y", [-1, 0])])
135+
ar1 = xr.DataArray([[1, 1], [1, 1]], [("x", ["b", "c"]), ("y", [0, 1])])
136136
ar0.combine_first(ar1)
137137
ar1.combine_first(ar0)
138138
@@ -152,7 +152,7 @@ variables with new values:
152152

153153
.. ipython:: python
154154
155-
ds.update({'space': ('space', [10.2, 9.4, 3.9])})
155+
ds.update({"space": ("space", [10.2, 9.4, 3.9])})
156156
157157
However, dimensions are still required to be consistent between different
158158
Dataset variables, so you cannot change the size of a dimension unless you
@@ -170,7 +170,7 @@ syntax:
170170

171171
.. ipython:: python
172172
173-
ds['baz'] = xr.DataArray([9, 9, 9, 9, 9], coords=[('x', list('abcde'))])
173+
ds["baz"] = xr.DataArray([9, 9, 9, 9, 9], coords=[("x", list("abcde"))])
174174
ds.baz
175175
176176
Equals and identical
@@ -193,16 +193,16 @@ object:
193193

194194
.. ipython:: python
195195
196-
arr.identical(arr.rename('bar'))
196+
arr.identical(arr.rename("bar"))
197197
198198
:py:attr:`~xarray.Dataset.broadcast_equals` does a more relaxed form of equality
199199
check that allows variables to have different dimensions, as long as values
200200
are constant along those new dimensions:
201201

202202
.. ipython:: python
203203
204-
left = xr.Dataset(coords={'x': 0})
205-
right = xr.Dataset({'x': [0, 0, 0]})
204+
left = xr.Dataset(coords={"x": 0})
205+
right = xr.Dataset({"x": [0, 0, 0]})
206206
left.broadcast_equals(right)
207207
208208
Like pandas objects, two xarray objects are still equal or identical if they have
@@ -231,9 +231,9 @@ coordinates as long as any non-missing values agree or are disjoint:
231231

232232
.. ipython:: python
233233
234-
ds1 = xr.Dataset({'a': ('x', [10, 20, 30, np.nan])}, {'x': [1, 2, 3, 4]})
235-
ds2 = xr.Dataset({'a': ('x', [np.nan, 30, 40, 50])}, {'x': [2, 3, 4, 5]})
236-
xr.merge([ds1, ds2], compat='no_conflicts')
234+
ds1 = xr.Dataset({"a": ("x", [10, 20, 30, np.nan])}, {"x": [1, 2, 3, 4]})
235+
ds2 = xr.Dataset({"a": ("x", [np.nan, 30, 40, 50])}, {"x": [2, 3, 4, 5]})
236+
xr.merge([ds1, ds2], compat="no_conflicts")
237237
238238
Note that due to the underlying representation of missing values as floating
239239
point numbers (``NaN``), variable data type is not always preserved when merging
@@ -273,10 +273,12 @@ datasets into a doubly-nested list, e.g:
273273

274274
.. ipython:: python
275275
276-
arr = xr.DataArray(name='temperature', data=np.random.randint(5, size=(2, 2)), dims=['x', 'y'])
276+
arr = xr.DataArray(
277+
name="temperature", data=np.random.randint(5, size=(2, 2)), dims=["x", "y"]
278+
)
277279
arr
278280
ds_grid = [[arr, arr], [arr, arr]]
279-
xr.combine_nested(ds_grid, concat_dim=['x', 'y'])
281+
xr.combine_nested(ds_grid, concat_dim=["x", "y"])
280282
281283
:py:func:`~xarray.combine_nested` can also be used to explicitly merge datasets
282284
with different variables. For example if we have 4 datasets, which are divided
@@ -286,10 +288,10 @@ we wish to use ``merge`` instead of ``concat``:
286288

287289
.. ipython:: python
288290
289-
temp = xr.DataArray(name='temperature', data=np.random.randn(2), dims=['t'])
290-
precip = xr.DataArray(name='precipitation', data=np.random.randn(2), dims=['t'])
291+
temp = xr.DataArray(name="temperature", data=np.random.randn(2), dims=["t"])
292+
precip = xr.DataArray(name="precipitation", data=np.random.randn(2), dims=["t"])
291293
ds_grid = [[temp, precip], [temp, precip]]
292-
xr.combine_nested(ds_grid, concat_dim=['t', None])
294+
xr.combine_nested(ds_grid, concat_dim=["t", None])
293295
294296
:py:func:`~xarray.combine_by_coords` is for combining objects which have dimension
295297
coordinates which specify their relationship to and order relative to one
@@ -302,8 +304,8 @@ coordinates, not on their position in the list passed to ``combine_by_coords``.
302304
.. ipython:: python
303305
:okwarning:
304306
305-
x1 = xr.DataArray(name='foo', data=np.random.randn(3), coords=[('x', [0, 1, 2])])
306-
x2 = xr.DataArray(name='foo', data=np.random.randn(3), coords=[('x', [3, 4, 5])])
307+
x1 = xr.DataArray(name="foo", data=np.random.randn(3), coords=[("x", [0, 1, 2])])
308+
x2 = xr.DataArray(name="foo", data=np.random.randn(3), coords=[("x", [3, 4, 5])])
307309
xr.combine_by_coords([x2, x1])
308310
309311
These functions can be used by :py:func:`~xarray.open_mfdataset` to open many

0 commit comments

Comments
 (0)