Skip to content

Commit 417797e

Browse files
author
Joe Hamman
authored
only use bottleneck version 1.0 and later (#1190)
* only use bottleneck version 1.0 and later * move bottleneck version warning to Rolling.__init__
1 parent 27d04a1 commit 417797e

File tree

7 files changed

+32
-5
lines changed

7 files changed

+32
-5
lines changed

doc/installing.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ For accelerating xarray
3030

3131
- `bottleneck <https://github.com/kwgoodman/bottleneck>`__: speeds up
3232
NaN-skipping and rolling window aggregations by a large factor
33+
(1.0 or later)
3334
- `cyordereddict <https://github.com/shoyer/cyordereddict>`__: speeds up most
3435
internal operations with xarray data structures (for python versions < 3.5)
3536

doc/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ Breaking changes
7474
``vmax`` when these kwargs are explicitly provided (:issue:`1191`). The
7575
automated level selection logic also slightly changed.
7676
By `Fabien Maussion <https://github.com/fmaussion>`_.
77-
- xarray no longer supports python 3.3 or versions of dask prior to v0.9.0.
77+
- xarray no longer supports python 3.3, versions of dask prior to v0.9.0,
78+
or versions of bottleneck prior to v1.0.
7879

7980
Deprecations
8081
~~~~~~~~~~~~

xarray/backends/common.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
except ImportError:
1919
from threading import Lock
2020

21+
2122
# Create a logger object, but don't add any handlers. Leave that to user code.
2223
logger = logging.getLogger(__name__)
2324

xarray/core/ops.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import inspect
77
import operator
88
import warnings
9+
from distutils.version import StrictVersion
910

1011
import numpy as np
1112
import pandas as pd
@@ -504,6 +505,9 @@ def inject_bottleneck_rolling_methods(cls):
504505

505506
# bottleneck rolling methods
506507
if has_bottleneck:
508+
if StrictVersion(bn.__version__) < StrictVersion('1.0'):
509+
return
510+
507511
for bn_name, method_name in BOTTLENECK_ROLLING_METHODS.items():
508512
f = getattr(bn, bn_name)
509513
func = cls._bottleneck_reduce(f)

xarray/core/rolling.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
from __future__ import division
33
from __future__ import print_function
44
import numpy as np
5+
import warnings
6+
from distutils.version import StrictVersion
57

68
from .pycompat import OrderedDict, zip
79
from .common import ImplementsRollingArrayReduce, full_like
810
from .combine import concat
9-
from .ops import inject_bottleneck_rolling_methods
11+
from .ops import inject_bottleneck_rolling_methods, has_bottleneck, bn
1012

1113

1214
class Rolling(object):
@@ -48,6 +50,13 @@ def __init__(self, obj, min_periods=None, center=False, **windows):
4850
rolling : type of input argument
4951
"""
5052

53+
if (has_bottleneck and
54+
(StrictVersion(bn.__version__) < StrictVersion('1.0'))):
55+
warnings.warn('xarray requires bottleneck version of 1.0 or '
56+
'greater for rolling operations. Rolling '
57+
'aggregation methods will use numpy instead'
58+
'of bottleneck.')
59+
5160
if len(windows) != 1:
5261
raise ValueError('exactly one dim/window should be provided')
5362

xarray/test/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import print_function
44
import warnings
55
from contextlib import contextmanager
6+
from distutils.version import StrictVersion
67

78
import numpy as np
89
from numpy.testing import assert_array_equal
@@ -68,6 +69,8 @@
6869

6970
try:
7071
import bottleneck
72+
if StrictVersion(bottleneck.__version__) < StrictVersion('1.0'):
73+
raise ImportError('Fall back to numpy')
7174
has_bottleneck = True
7275
except ImportError:
7376
has_bottleneck = False
@@ -76,6 +79,7 @@
7679
# Generally `pytest.importorskip('package')` inline is even easier
7780
requires_matplotlib = pytest.mark.skipif(not has_matplotlib, reason='requires matplotlib')
7881

82+
7983
def requires_scipy(test):
8084
return test if has_scipy else pytest.mark.skip('requires scipy')(test)
8185

@@ -192,16 +196,18 @@ def assertDataArrayIdentical(self, ar1, ar2):
192196
def assertDataArrayAllClose(self, ar1, ar2, rtol=1e-05, atol=1e-08):
193197
assert_xarray_allclose(ar1, ar2, rtol=rtol, atol=atol)
194198

199+
195200
def assert_xarray_equal(a, b):
196201
import xarray as xr
197202
___tracebackhide__ = True # noqa: F841
198203
assert type(a) == type(b)
199204
if isinstance(a, (xr.Variable, xr.DataArray, xr.Dataset)):
200205
assert a.equals(b), '{}\n{}'.format(a, b)
201-
else:
206+
else:
202207
raise TypeError('{} not supported by assertion comparison'
203208
.format(type(a)))
204209

210+
205211
def assert_xarray_identical(a, b):
206212
import xarray as xr
207213
___tracebackhide__ = True # noqa: F841
@@ -215,6 +221,7 @@ def assert_xarray_identical(a, b):
215221
raise TypeError('{} not supported by assertion comparison'
216222
.format(type(a)))
217223

224+
218225
def assert_xarray_allclose(a, b, rtol=1e-05, atol=1e-08):
219226
import xarray as xr
220227
___tracebackhide__ = True # noqa: F841
@@ -241,6 +248,7 @@ def assert_xarray_allclose(a, b, rtol=1e-05, atol=1e-08):
241248
raise TypeError('{} not supported by assertion comparison'
242249
.format(type(a)))
243250

251+
244252
class UnexpectedDataAccess(Exception):
245253
pass
246254

xarray/test/test_dataarray.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,7 +2381,9 @@ def da(request):
23812381
return da
23822382

23832383
if request.param == 2:
2384-
return DataArray([0, np.nan, 1, 2, np.nan, 3, 4, 5, np.nan, 6, 7], dims='time')
2384+
return DataArray([0, np.nan, 1, 2, np.nan, 3, 4, 5, np.nan, 6, 7],
2385+
dims='time')
2386+
23852387

23862388
def test_rolling_iter(da):
23872389

@@ -2393,8 +2395,9 @@ def test_rolling_iter(da):
23932395
for i, (label, window_da) in enumerate(rolling_obj):
23942396
assert label == da['time'].isel(time=i)
23952397

2398+
23962399
def test_rolling_properties(da):
2397-
pytest.importorskip('bottleneck')
2400+
pytest.importorskip('bottleneck', minversion='1.0')
23982401

23992402
rolling_obj = da.rolling(time=4)
24002403

0 commit comments

Comments
 (0)