Skip to content

Commit 23dc2fc

Browse files
authored
Fix doctests (#4408)
* update the CFTimeIndex reprs * update the variable reprs and fix a typo * fix the examples in the datetime accessors * make sure numpy.random is always seeded in doctests * fix more doctests * explain the seeding of numpy.random * shorten the expected array reprs * fix the failing conventions doctests * attempt to fix the failing coding.strings doctest * fix the failing save_mfdataset doctest * fix the indexing doctests * always use change to a temporary directory before executing a test which makes manually removing paths unnecessary * fix the filter_by_attrs doctests
1 parent f70c881 commit 23dc2fc

23 files changed

+557
-424
lines changed

conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def pytest_runtest_setup(item):
2424

2525

2626
@pytest.fixture(autouse=True)
27-
def add_standard_imports(doctest_namespace):
27+
def add_standard_imports(doctest_namespace, tmpdir):
2828
import numpy as np
2929
import pandas as pd
3030

@@ -33,3 +33,9 @@ def add_standard_imports(doctest_namespace):
3333
doctest_namespace["np"] = np
3434
doctest_namespace["pd"] = pd
3535
doctest_namespace["xr"] = xr
36+
37+
# always seed numpy.random to make the examples deterministic
38+
np.random.seed(0)
39+
40+
# always switch to the temporary directory, so files get written there
41+
tmpdir.chdir()

xarray/backends/api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,17 @@ def save_mfdataset(
11891189
11901190
Save a dataset into one netCDF per year of data:
11911191
1192+
>>> ds = xr.Dataset(
1193+
... {"a": ("time", np.linspace(0, 1, 48))},
1194+
... coords={"time": pd.date_range("2010-01-01", freq="M", periods=48)},
1195+
... )
1196+
>>> ds
1197+
<xarray.Dataset>
1198+
Dimensions: (time: 48)
1199+
Coordinates:
1200+
* time (time) datetime64[ns] 2010-01-31 2010-02-28 ... 2013-12-31
1201+
Data variables:
1202+
a (time) float64 0.0 0.02128 0.04255 0.06383 ... 0.9574 0.9787 1.0
11921203
>>> years, datasets = zip(*ds.groupby("time.year"))
11931204
>>> paths = ["%s.nc" % y for y in years]
11941205
>>> xr.save_mfdataset(datasets, paths)

xarray/coding/cftime_offsets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ def cftime_range(
939939
>>> xr.cftime_range(start="2000", periods=6, freq="2MS", calendar="noleap")
940940
CFTimeIndex([2000-01-01 00:00:00, 2000-03-01 00:00:00, 2000-05-01 00:00:00,
941941
2000-07-01 00:00:00, 2000-09-01 00:00:00, 2000-11-01 00:00:00],
942-
dtype='object')
942+
dtype='object', length=6, calendar='noleap')
943943
944944
As in the standard pandas function, three of the ``start``, ``end``,
945945
``periods``, or ``freq`` arguments must be specified at a given time, with

xarray/coding/cftimeindex.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,11 @@ def shift(self, n, freq):
523523
--------
524524
>>> index = xr.cftime_range("2000", periods=1, freq="M")
525525
>>> index
526-
CFTimeIndex([2000-01-31 00:00:00], dtype='object')
526+
CFTimeIndex([2000-01-31 00:00:00],
527+
dtype='object', length=1, calendar='gregorian')
527528
>>> index.shift(1, "M")
528-
CFTimeIndex([2000-02-29 00:00:00], dtype='object')
529+
CFTimeIndex([2000-02-29 00:00:00],
530+
dtype='object', length=1, calendar='gregorian')
529531
"""
530532
from .cftime_offsets import to_offset
531533

@@ -611,7 +613,8 @@ def to_datetimeindex(self, unsafe=False):
611613
>>> import xarray as xr
612614
>>> times = xr.cftime_range("2000", periods=2, calendar="gregorian")
613615
>>> times
614-
CFTimeIndex([2000-01-01 00:00:00, 2000-01-02 00:00:00], dtype='object')
616+
CFTimeIndex([2000-01-01 00:00:00, 2000-01-02 00:00:00],
617+
dtype='object', length=2, calendar='gregorian')
615618
>>> times.to_datetimeindex()
616619
DatetimeIndex(['2000-01-01', '2000-01-02'], dtype='datetime64[ns]', freq=None)
617620
"""

xarray/coding/strings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ class StackedBytesArray(indexing.ExplicitlyIndexedNDArrayMixin):
199199
"""Wrapper around array-like objects to create a new indexable object where
200200
values, when accessed, are automatically stacked along the last dimension.
201201
202-
>>> StackedBytesArray(np.array(["a", "b", "c"]))[:]
203-
array('abc',
204-
dtype='|S3')
202+
>>> indexer = indexing.BasicIndexer((slice(None),))
203+
>>> StackedBytesArray(np.array(["a", "b", "c"], dtype="S1"))[indexer]
204+
array(b'abc', dtype='|S3')
205205
"""
206206

207207
def __init__(self, array):

xarray/conventions.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ class NativeEndiannessArray(indexing.ExplicitlyIndexedNDArrayMixin):
2424
>>> x.dtype
2525
dtype('>i2')
2626
27-
>>> NativeEndianArray(x).dtype
27+
>>> NativeEndiannessArray(x).dtype
2828
dtype('int16')
2929
30-
>>> NativeEndianArray(x)[:].dtype
30+
>>> indexer = indexing.BasicIndexer((slice(None),))
31+
>>> NativeEndiannessArray(x)[indexer].dtype
3132
dtype('int16')
3233
"""
3334

@@ -53,12 +54,13 @@ class BoolTypeArray(indexing.ExplicitlyIndexedNDArrayMixin):
5354
>>> x = np.array([1, 0, 1, 1, 0], dtype="i1")
5455
5556
>>> x.dtype
56-
dtype('>i2')
57+
dtype('int8')
5758
5859
>>> BoolTypeArray(x).dtype
5960
dtype('bool')
6061
61-
>>> BoolTypeArray(x)[:].dtype
62+
>>> indexer = indexing.BasicIndexer((slice(None),))
63+
>>> BoolTypeArray(x)[indexer].dtype
6264
dtype('bool')
6365
"""
6466

xarray/core/accessor_dt.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -249,30 +249,30 @@ class DatetimeAccessor(Properties):
249249
>>> ts
250250
<xarray.DataArray (time: 10)>
251251
array(['2000-01-01T00:00:00.000000000', '2000-01-02T00:00:00.000000000',
252-
'2000-01-03T00:00:00.000000000', '2000-01-04T00:00:00.000000000',
253-
'2000-01-05T00:00:00.000000000', '2000-01-06T00:00:00.000000000',
254-
'2000-01-07T00:00:00.000000000', '2000-01-08T00:00:00.000000000',
255-
'2000-01-09T00:00:00.000000000', '2000-01-10T00:00:00.000000000'],
256-
dtype='datetime64[ns]')
252+
'2000-01-03T00:00:00.000000000', '2000-01-04T00:00:00.000000000',
253+
'2000-01-05T00:00:00.000000000', '2000-01-06T00:00:00.000000000',
254+
'2000-01-07T00:00:00.000000000', '2000-01-08T00:00:00.000000000',
255+
'2000-01-09T00:00:00.000000000', '2000-01-10T00:00:00.000000000'],
256+
dtype='datetime64[ns]')
257257
Coordinates:
258-
* time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2000-01-10
259-
>>> ts.dt
260-
<xarray.core.accessor_dt.DatetimeAccessor object at 0x118b54d68>
258+
* time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2000-01-10
259+
>>> ts.dt # doctest: +ELLIPSIS
260+
<xarray.core.accessor_dt.DatetimeAccessor object at 0x...>
261261
>>> ts.dt.dayofyear
262262
<xarray.DataArray 'dayofyear' (time: 10)>
263263
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
264264
Coordinates:
265-
* time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2000-01-10
265+
* time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2000-01-10
266266
>>> ts.dt.quarter
267267
<xarray.DataArray 'quarter' (time: 10)>
268268
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
269269
Coordinates:
270-
* time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2000-01-10
270+
* time (time) datetime64[ns] 2000-01-01 2000-01-02 ... 2000-01-10
271271
272272
"""
273273

274274
def strftime(self, date_format):
275-
'''
275+
"""
276276
Return an array of formatted strings specified by date_format, which
277277
supports the same string format as the python standard library. Details
278278
of the string format can be found in `python string format doc
@@ -290,13 +290,12 @@ def strftime(self, date_format):
290290
291291
Examples
292292
--------
293+
>>> import datetime
293294
>>> rng = xr.Dataset({"time": datetime.datetime(2000, 1, 1)})
294295
>>> rng["time"].dt.strftime("%B %d, %Y, %r")
295296
<xarray.DataArray 'strftime' ()>
296297
array('January 01, 2000, 12:00:00 AM', dtype=object)
297298
"""
298-
299-
'''
300299
obj_type = type(self._obj)
301300

302301
result = _strftime(self._obj.data, date_format)
@@ -398,32 +397,32 @@ class TimedeltaAccessor(Properties):
398397
>>> ts
399398
<xarray.DataArray (time: 20)>
400399
array([ 86400000000000, 108000000000000, 129600000000000, 151200000000000,
401-
172800000000000, 194400000000000, 216000000000000, 237600000000000,
402-
259200000000000, 280800000000000, 302400000000000, 324000000000000,
403-
345600000000000, 367200000000000, 388800000000000, 410400000000000,
404-
432000000000000, 453600000000000, 475200000000000, 496800000000000],
405-
dtype='timedelta64[ns]')
400+
172800000000000, 194400000000000, 216000000000000, 237600000000000,
401+
259200000000000, 280800000000000, 302400000000000, 324000000000000,
402+
345600000000000, 367200000000000, 388800000000000, 410400000000000,
403+
432000000000000, 453600000000000, 475200000000000, 496800000000000],
404+
dtype='timedelta64[ns]')
406405
Coordinates:
407-
* time (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
408-
>>> ts.dt
409-
<xarray.core.accessor_dt.TimedeltaAccessor object at 0x109a27d68>
406+
* time (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
407+
>>> ts.dt # doctest: +ELLIPSIS
408+
<xarray.core.accessor_dt.TimedeltaAccessor object at 0x...>
410409
>>> ts.dt.days
411410
<xarray.DataArray 'days' (time: 20)>
412411
array([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5])
413412
Coordinates:
414-
* time (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
413+
* time (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
415414
>>> ts.dt.microseconds
416415
<xarray.DataArray 'microseconds' (time: 20)>
417416
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
418417
Coordinates:
419-
* time (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
418+
* time (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
420419
>>> ts.dt.seconds
421420
<xarray.DataArray 'seconds' (time: 20)>
422421
array([ 0, 21600, 43200, 64800, 0, 21600, 43200, 64800, 0,
423-
21600, 43200, 64800, 0, 21600, 43200, 64800, 0, 21600,
424-
43200, 64800])
422+
21600, 43200, 64800, 0, 21600, 43200, 64800, 0, 21600,
423+
43200, 64800])
425424
Coordinates:
426-
* time (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
425+
* time (time) timedelta64[ns] 1 days 00:00:00 ... 5 days 18:00:00
427426
"""
428427

429428
days = Properties._tslib_field_accessor(

xarray/core/accessor_str.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class StringAccessor:
6868
for applicable DataArrays.
6969
7070
>>> da = xr.DataArray(["some", "text", "in", "an", "array"])
71-
>>> ds.str.len()
71+
>>> da.str.len()
7272
<xarray.DataArray (dim_0: 5)>
7373
array([4, 4, 2, 2, 5])
7474
Dimensions without coordinates: dim_0

0 commit comments

Comments
 (0)