Skip to content

Commit 739b347

Browse files
authored
Doctests fixes (#3846)
* start of doctest fixes * start of doctest fixes
1 parent 7927c2b commit 739b347

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,14 @@ def pytest_runtest_setup(item):
2121
pytest.skip(
2222
"set --run-network-tests to run test requiring an " "internet connection"
2323
)
24+
25+
26+
@pytest.fixture(autouse=True)
27+
def add_standard_imports(doctest_namespace):
28+
import numpy as np
29+
import pandas as pd
30+
import xarray as xr
31+
32+
doctest_namespace["np"] = np
33+
doctest_namespace["pd"] = pd
34+
doctest_namespace["xr"] = xr

doc/contributing.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ Bug reports must:
5151
<http://github.github.com/github-flavored-markdown/>`_::
5252

5353
```python
54-
>>> from xarray import Dataset
55-
>>> df = Dataset(...)
54+
>>> import xarray as xr
55+
>>> df = xr.Dataset(...)
5656
...
5757
```
5858

@@ -378,8 +378,8 @@ and then running::
378378

379379
pre-commit install
380380

381-
from the root of the xarray repository. You can skip the pre-commit checks with
382-
``git commit --no-verify``.
381+
from the root of the xarray repository. You can skip the pre-commit checks
382+
with ``git commit --no-verify``.
383383

384384

385385
Backwards Compatibility

xarray/core/dataarray.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,20 +1475,23 @@ def swap_dims(self, dims_dict: Mapping[Hashable, Hashable]) -> "DataArray":
14751475
14761476
Examples
14771477
--------
1478+
14781479
>>> arr = xr.DataArray(data=[0, 1], dims="x",
1479-
coords={"x": ["a", "b"], "y": ("x", [0, 1])})
1480+
... coords={"x": ["a", "b"], "y": ("x", [0, 1])})
14801481
>>> arr
14811482
<xarray.DataArray (x: 2)>
14821483
array([0, 1])
14831484
Coordinates:
14841485
* x (x) <U1 'a' 'b'
14851486
y (x) int64 0 1
1487+
14861488
>>> arr.swap_dims({"x": "y"})
14871489
<xarray.DataArray (y: 2)>
14881490
array([0, 1])
14891491
Coordinates:
14901492
x (y) <U1 'a' 'b'
14911493
* y (y) int64 0 1
1494+
14921495
>>> arr.swap_dims({"x": "z"})
14931496
<xarray.DataArray (z: 2)>
14941497
array([0, 1])
@@ -1718,7 +1721,7 @@ def stack(
17181721
Examples
17191722
--------
17201723
1721-
>>> arr = DataArray(np.arange(6).reshape(2, 3),
1724+
>>> arr = xr.DataArray(np.arange(6).reshape(2, 3),
17221725
... coords=[('x', ['a', 'b']), ('y', [0, 1, 2])])
17231726
>>> arr
17241727
<xarray.DataArray (x: 2, y: 3)>
@@ -1768,7 +1771,7 @@ def unstack(
17681771
Examples
17691772
--------
17701773
1771-
>>> arr = DataArray(np.arange(6).reshape(2, 3),
1774+
>>> arr = xr.DataArray(np.arange(6).reshape(2, 3),
17721775
... coords=[('x', ['a', 'b']), ('y', [0, 1, 2])])
17731776
>>> arr
17741777
<xarray.DataArray (x: 2, y: 3)>
@@ -1817,7 +1820,7 @@ def to_unstacked_dataset(self, dim, level=0):
18171820
Examples
18181821
--------
18191822
>>> import xarray as xr
1820-
>>> arr = DataArray(np.arange(6).reshape(2, 3),
1823+
>>> arr = xr.DataArray(np.arange(6).reshape(2, 3),
18211824
... coords=[('x', ['a', 'b']), ('y', [0, 1, 2])])
18221825
>>> data = xr.Dataset({'a': arr, 'b': arr.isel(y=0)})
18231826
>>> data
@@ -2623,7 +2626,7 @@ def plot(self) -> _PlotMethods:
26232626
"""
26242627
Access plotting functions for DataArray's
26252628
2626-
>>> d = DataArray([[1, 2], [3, 4]])
2629+
>>> d = xr.DataArray([[1, 2], [3, 4]])
26272630
26282631
For convenience just call this directly
26292632
@@ -2849,18 +2852,20 @@ def dot(
28492852
--------
28502853
28512854
>>> da_vals = np.arange(6 * 5 * 4).reshape((6, 5, 4))
2852-
>>> da = DataArray(da_vals, dims=['x', 'y', 'z'])
2855+
>>> da = xr.DataArray(da_vals, dims=['x', 'y', 'z'])
28532856
>>> dm_vals = np.arange(4)
2854-
>>> dm = DataArray(dm_vals, dims=['z'])
2857+
>>> dm = xr.DataArray(dm_vals, dims=['z'])
28552858
28562859
>>> dm.dims
28572860
('z')
2861+
28582862
>>> da.dims
28592863
('x', 'y', 'z')
28602864
28612865
>>> dot_result = da.dot(dm)
28622866
>>> dot_result.dims
28632867
('x', 'y')
2868+
28642869
"""
28652870
if isinstance(other, Dataset):
28662871
raise NotImplementedError(

xarray/core/dataset.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ def copy(self, deep: bool = False, data: Mapping = None) -> "Dataset":
10111011
10121012
>>> da = xr.DataArray(np.random.randn(2, 3))
10131013
>>> ds = xr.Dataset({'foo': da, 'bar': ('x', [-1, 2])},
1014-
coords={'x': ['one', 'two']})
1014+
... coords={'x': ['one', 'two']})
10151015
>>> ds.copy()
10161016
<xarray.Dataset>
10171017
Dimensions: (dim_0: 2, dim_1: 3, x: 2)
@@ -1021,6 +1021,7 @@ def copy(self, deep: bool = False, data: Mapping = None) -> "Dataset":
10211021
Data variables:
10221022
foo (dim_0, dim_1) float64 -0.8079 0.3897 -1.862 -0.6091 -1.051 -0.3003
10231023
bar (x) int64 -1 2
1024+
10241025
>>> ds_0 = ds.copy(deep=False)
10251026
>>> ds_0['foo'][0, 0] = 7
10261027
>>> ds_0
@@ -1032,6 +1033,7 @@ def copy(self, deep: bool = False, data: Mapping = None) -> "Dataset":
10321033
Data variables:
10331034
foo (dim_0, dim_1) float64 7.0 0.3897 -1.862 -0.6091 -1.051 -0.3003
10341035
bar (x) int64 -1 2
1036+
10351037
>>> ds
10361038
<xarray.Dataset>
10371039
Dimensions: (dim_0: 2, dim_1: 3, x: 2)
@@ -1055,6 +1057,7 @@ def copy(self, deep: bool = False, data: Mapping = None) -> "Dataset":
10551057
Data variables:
10561058
foo (dim_0, dim_1) int64 0 1 2 3 4 5
10571059
bar (x) <U1 'a' 'b'
1060+
10581061
>>> ds
10591062
<xarray.Dataset>
10601063
Dimensions: (dim_0: 2, dim_1: 3, x: 2)
@@ -2883,7 +2886,7 @@ def swap_dims(
28832886
Examples
28842887
--------
28852888
>>> ds = xr.Dataset(data_vars={"a": ("x", [5, 7]), "b": ("x", [0.1, 2.4])},
2886-
coords={"x": ["a", "b"], "y": ("x", [0, 1])})
2889+
... coords={"x": ["a", "b"], "y": ("x", [0, 1])})
28872890
>>> ds
28882891
<xarray.Dataset>
28892892
Dimensions: (x: 2)
@@ -2893,6 +2896,7 @@ def swap_dims(
28932896
Data variables:
28942897
a (x) int64 5 7
28952898
b (x) float64 0.1 2.4
2899+
28962900
>>> ds.swap_dims({"x": "y"})
28972901
<xarray.Dataset>
28982902
Dimensions: (y: 2)
@@ -2902,6 +2906,7 @@ def swap_dims(
29022906
Data variables:
29032907
a (y) int64 5 7
29042908
b (y) float64 0.1 2.4
2909+
29052910
>>> ds.swap_dims({"x": "z"})
29062911
<xarray.Dataset>
29072912
Dimensions: (z: 2)
@@ -3341,7 +3346,7 @@ def to_stacked_array(
33413346
33423347
Examples
33433348
--------
3344-
>>> data = Dataset(
3349+
>>> data = xr.Dataset(
33453350
... data_vars={'a': (('x', 'y'), [[0, 1, 2], [3, 4, 5]]),
33463351
... 'b': ('x', [6, 7])},
33473352
... coords={'y': ['u', 'v', 'w']}

xarray/core/rolling.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,22 @@ def construct(self, window_dim, stride=1, fill_value=dtypes.NA):
231231
232232
Examples
233233
--------
234-
>>> da = DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))
235-
>>>
234+
>>> da = xr.DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))
235+
236236
>>> rolling = da.rolling(b=3)
237237
>>> rolling.construct('window_dim')
238238
<xarray.DataArray (a: 2, b: 4, window_dim: 3)>
239239
array([[[np.nan, np.nan, 0], [np.nan, 0, 1], [0, 1, 2], [1, 2, 3]],
240240
[[np.nan, np.nan, 4], [np.nan, 4, 5], [4, 5, 6], [5, 6, 7]]])
241241
Dimensions without coordinates: a, b, window_dim
242-
>>>
242+
243243
>>> rolling = da.rolling(b=3, center=True)
244244
>>> rolling.construct('window_dim')
245245
<xarray.DataArray (a: 2, b: 4, window_dim: 3)>
246246
array([[[np.nan, 0, 1], [0, 1, 2], [1, 2, 3], [2, 3, np.nan]],
247247
[[np.nan, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, np.nan]]])
248248
Dimensions without coordinates: a, b, window_dim
249+
249250
"""
250251

251252
from .dataarray import DataArray
@@ -278,26 +279,26 @@ def reduce(self, func, **kwargs):
278279
279280
Examples
280281
--------
281-
>>> da = DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))
282-
>>>
282+
>>> da = xr.DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))
283283
>>> rolling = da.rolling(b=3)
284284
>>> rolling.construct('window_dim')
285285
<xarray.DataArray (a: 2, b: 4, window_dim: 3)>
286286
array([[[np.nan, np.nan, 0], [np.nan, 0, 1], [0, 1, 2], [1, 2, 3]],
287287
[[np.nan, np.nan, 4], [np.nan, 4, 5], [4, 5, 6], [5, 6, 7]]])
288288
Dimensions without coordinates: a, b, window_dim
289-
>>>
289+
290290
>>> rolling.reduce(np.sum)
291291
<xarray.DataArray (a: 2, b: 4)>
292292
array([[nan, nan, 3., 6.],
293293
[nan, nan, 15., 18.]])
294294
Dimensions without coordinates: a, b
295-
>>>
295+
296296
>>> rolling = da.rolling(b=3, min_periods=1)
297297
>>> rolling.reduce(np.nansum)
298298
<xarray.DataArray (a: 2, b: 4)>
299299
array([[ 0., 1., 3., 6.],
300300
[ 4., 9., 15., 18.]])
301+
301302
"""
302303
rolling_dim = utils.get_temp_dimname(self.obj.dims, "_rolling_dim")
303304
windows = self.construct(rolling_dim)

0 commit comments

Comments
 (0)