Skip to content

Doctests fixes #3846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ def pytest_runtest_setup(item):
pytest.skip(
"set --run-network-tests to run test requiring an " "internet connection"
)


@pytest.fixture(autouse=True)
def add_standard_imports(doctest_namespace):
import numpy as np
import pandas as pd
import xarray as xr

doctest_namespace["np"] = np
doctest_namespace["pd"] = pd
doctest_namespace["xr"] = xr
8 changes: 4 additions & 4 deletions doc/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ Bug reports must:
<http://github.github.com/github-flavored-markdown/>`_::

```python
>>> from xarray import Dataset
>>> df = Dataset(...)
>>> import xarray as xr
>>> df = xr.Dataset(...)
...
```

Expand Down Expand Up @@ -378,8 +378,8 @@ and then running::

pre-commit install

from the root of the xarray repository. You can skip the pre-commit checks with
``git commit --no-verify``.
from the root of the xarray repository. You can skip the pre-commit checks
with ``git commit --no-verify``.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW the isort from pre-commit was doing some weird stuff, I think



Backwards Compatibility
Expand Down
19 changes: 12 additions & 7 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,20 +1475,23 @@ def swap_dims(self, dims_dict: Mapping[Hashable, Hashable]) -> "DataArray":

Examples
--------

>>> arr = xr.DataArray(data=[0, 1], dims="x",
coords={"x": ["a", "b"], "y": ("x", [0, 1])})
... coords={"x": ["a", "b"], "y": ("x", [0, 1])})
>>> arr
<xarray.DataArray (x: 2)>
array([0, 1])
Coordinates:
* x (x) <U1 'a' 'b'
y (x) int64 0 1

>>> arr.swap_dims({"x": "y"})
<xarray.DataArray (y: 2)>
array([0, 1])
Coordinates:
x (y) <U1 'a' 'b'
* y (y) int64 0 1

>>> arr.swap_dims({"x": "z"})
<xarray.DataArray (z: 2)>
array([0, 1])
Expand Down Expand Up @@ -1718,7 +1721,7 @@ def stack(
Examples
--------

>>> arr = DataArray(np.arange(6).reshape(2, 3),
>>> arr = xr.DataArray(np.arange(6).reshape(2, 3),
... coords=[('x', ['a', 'b']), ('y', [0, 1, 2])])
>>> arr
<xarray.DataArray (x: 2, y: 3)>
Expand Down Expand Up @@ -1768,7 +1771,7 @@ def unstack(
Examples
--------

>>> arr = DataArray(np.arange(6).reshape(2, 3),
>>> arr = xr.DataArray(np.arange(6).reshape(2, 3),
... coords=[('x', ['a', 'b']), ('y', [0, 1, 2])])
>>> arr
<xarray.DataArray (x: 2, y: 3)>
Expand Down Expand Up @@ -1817,7 +1820,7 @@ def to_unstacked_dataset(self, dim, level=0):
Examples
--------
>>> import xarray as xr
>>> arr = DataArray(np.arange(6).reshape(2, 3),
>>> arr = xr.DataArray(np.arange(6).reshape(2, 3),
... coords=[('x', ['a', 'b']), ('y', [0, 1, 2])])
>>> data = xr.Dataset({'a': arr, 'b': arr.isel(y=0)})
>>> data
Expand Down Expand Up @@ -2629,7 +2632,7 @@ def plot(self) -> _PlotMethods:
"""
Access plotting functions for DataArray's

>>> d = DataArray([[1, 2], [3, 4]])
>>> d = xr.DataArray([[1, 2], [3, 4]])

For convenience just call this directly

Expand Down Expand Up @@ -2855,18 +2858,20 @@ def dot(
--------

>>> da_vals = np.arange(6 * 5 * 4).reshape((6, 5, 4))
>>> da = DataArray(da_vals, dims=['x', 'y', 'z'])
>>> da = xr.DataArray(da_vals, dims=['x', 'y', 'z'])
>>> dm_vals = np.arange(4)
>>> dm = DataArray(dm_vals, dims=['z'])
>>> dm = xr.DataArray(dm_vals, dims=['z'])

>>> dm.dims
('z')

>>> da.dims
('x', 'y', 'z')

>>> dot_result = da.dot(dm)
>>> dot_result.dims
('x', 'y')

"""
if isinstance(other, Dataset):
raise NotImplementedError(
Expand Down
11 changes: 8 additions & 3 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ def copy(self, deep: bool = False, data: Mapping = None) -> "Dataset":

>>> da = xr.DataArray(np.random.randn(2, 3))
>>> ds = xr.Dataset({'foo': da, 'bar': ('x', [-1, 2])},
coords={'x': ['one', 'two']})
... coords={'x': ['one', 'two']})
>>> ds.copy()
<xarray.Dataset>
Dimensions: (dim_0: 2, dim_1: 3, x: 2)
Expand All @@ -1021,6 +1021,7 @@ def copy(self, deep: bool = False, data: Mapping = None) -> "Dataset":
Data variables:
foo (dim_0, dim_1) float64 -0.8079 0.3897 -1.862 -0.6091 -1.051 -0.3003
bar (x) int64 -1 2

>>> ds_0 = ds.copy(deep=False)
>>> ds_0['foo'][0, 0] = 7
>>> ds_0
Expand All @@ -1032,6 +1033,7 @@ def copy(self, deep: bool = False, data: Mapping = None) -> "Dataset":
Data variables:
foo (dim_0, dim_1) float64 7.0 0.3897 -1.862 -0.6091 -1.051 -0.3003
bar (x) int64 -1 2

>>> ds
<xarray.Dataset>
Dimensions: (dim_0: 2, dim_1: 3, x: 2)
Expand All @@ -1055,6 +1057,7 @@ def copy(self, deep: bool = False, data: Mapping = None) -> "Dataset":
Data variables:
foo (dim_0, dim_1) int64 0 1 2 3 4 5
bar (x) <U1 'a' 'b'

>>> ds
<xarray.Dataset>
Dimensions: (dim_0: 2, dim_1: 3, x: 2)
Expand Down Expand Up @@ -2883,7 +2886,7 @@ def swap_dims(
Examples
--------
>>> ds = xr.Dataset(data_vars={"a": ("x", [5, 7]), "b": ("x", [0.1, 2.4])},
coords={"x": ["a", "b"], "y": ("x", [0, 1])})
... coords={"x": ["a", "b"], "y": ("x", [0, 1])})
>>> ds
<xarray.Dataset>
Dimensions: (x: 2)
Expand All @@ -2893,6 +2896,7 @@ def swap_dims(
Data variables:
a (x) int64 5 7
b (x) float64 0.1 2.4

>>> ds.swap_dims({"x": "y"})
<xarray.Dataset>
Dimensions: (y: 2)
Expand All @@ -2902,6 +2906,7 @@ def swap_dims(
Data variables:
a (y) int64 5 7
b (y) float64 0.1 2.4

>>> ds.swap_dims({"x": "z"})
<xarray.Dataset>
Dimensions: (z: 2)
Expand Down Expand Up @@ -3341,7 +3346,7 @@ def to_stacked_array(

Examples
--------
>>> data = Dataset(
>>> data = xr.Dataset(
... data_vars={'a': (('x', 'y'), [[0, 1, 2], [3, 4, 5]]),
... 'b': ('x', [6, 7])},
... coords={'y': ['u', 'v', 'w']}
Expand Down
15 changes: 8 additions & 7 deletions xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,22 @@ def construct(self, window_dim, stride=1, fill_value=dtypes.NA):

Examples
--------
>>> da = DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))
>>>
>>> da = xr.DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))

>>> rolling = da.rolling(b=3)
>>> rolling.construct('window_dim')
<xarray.DataArray (a: 2, b: 4, window_dim: 3)>
array([[[np.nan, np.nan, 0], [np.nan, 0, 1], [0, 1, 2], [1, 2, 3]],
[[np.nan, np.nan, 4], [np.nan, 4, 5], [4, 5, 6], [5, 6, 7]]])
Dimensions without coordinates: a, b, window_dim
>>>

>>> rolling = da.rolling(b=3, center=True)
>>> rolling.construct('window_dim')
<xarray.DataArray (a: 2, b: 4, window_dim: 3)>
array([[[np.nan, 0, 1], [0, 1, 2], [1, 2, 3], [2, 3, np.nan]],
[[np.nan, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, np.nan]]])
Dimensions without coordinates: a, b, window_dim

"""

from .dataarray import DataArray
Expand Down Expand Up @@ -278,26 +279,26 @@ def reduce(self, func, **kwargs):

Examples
--------
>>> da = DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))
>>>
>>> da = xr.DataArray(np.arange(8).reshape(2, 4), dims=('a', 'b'))
>>> rolling = da.rolling(b=3)
>>> rolling.construct('window_dim')
<xarray.DataArray (a: 2, b: 4, window_dim: 3)>
array([[[np.nan, np.nan, 0], [np.nan, 0, 1], [0, 1, 2], [1, 2, 3]],
[[np.nan, np.nan, 4], [np.nan, 4, 5], [4, 5, 6], [5, 6, 7]]])
Dimensions without coordinates: a, b, window_dim
>>>

>>> rolling.reduce(np.sum)
<xarray.DataArray (a: 2, b: 4)>
array([[nan, nan, 3., 6.],
[nan, nan, 15., 18.]])
Dimensions without coordinates: a, b
>>>

>>> rolling = da.rolling(b=3, min_periods=1)
>>> rolling.reduce(np.nansum)
<xarray.DataArray (a: 2, b: 4)>
array([[ 0., 1., 3., 6.],
[ 4., 9., 15., 18.]])

"""
rolling_dim = utils.get_temp_dimname(self.obj.dims, "_rolling_dim")
windows = self.construct(rolling_dim)
Expand Down