Skip to content

Commit ad91a08

Browse files
committed
check for use_bottleneck in rank
1 parent 514db3a commit ad91a08

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

xarray/core/dataset.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6198,6 +6198,9 @@ def rank(self, dim, pct=False, keep_attrs=None):
61986198
ranked : Dataset
61996199
Variables that do not depend on `dim` are dropped.
62006200
"""
6201+
if not OPTIONS["use_bottleneck"]:
6202+
raise RuntimeError("rank requires bottleneck to be enabled")
6203+
62016204
if dim not in self.dims:
62026205
raise ValueError(f"Dataset does not contain the dimension: {dim}")
62036206

xarray/core/variable.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from .common import AbstractArray
2828
from .indexes import PandasIndex, wrap_pandas_index
2929
from .indexing import BasicIndexer, OuterIndexer, VectorizedIndexer, as_indexable
30-
from .options import _get_keep_attrs
30+
from .options import OPTIONS, _get_keep_attrs
3131
from .pycompat import (
3232
cupy_array_type,
3333
dask_array_type,
@@ -2016,6 +2016,9 @@ def rank(self, dim, pct=False):
20162016
--------
20172017
Dataset.rank, DataArray.rank
20182018
"""
2019+
if not OPTIONS["use_bottleneck"]:
2020+
raise RuntimeError("rank requires bottleneck to be enabled")
2021+
20192022
import bottleneck as bn
20202023

20212024
data = self.data

xarray/tests/test_dataset.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4969,6 +4969,12 @@ def test_rank(self):
49694969
with pytest.raises(ValueError, match=r"does not contain"):
49704970
x.rank("invalid_dim")
49714971

4972+
def test_rank_use_bottleneck(self):
4973+
ds = Dataset({"a": ("x", [0, np.nan, 2]), "b": ("y", [4, 6, 3, 4])})
4974+
with xr.set_options(use_bottleneck=False):
4975+
with pytest.raises(RuntimeError):
4976+
ds.rank("x")
4977+
49724978
def test_count(self):
49734979
ds = Dataset({"x": ("a", [np.nan, 1]), "y": 0, "z": np.nan})
49744980
expected = Dataset({"x": 1, "y": 1, "z": 0})

xarray/tests/test_variable.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,12 @@ def test_rank_dask_raises(self):
17181718
with pytest.raises(TypeError, match=r"arrays stored as dask"):
17191719
v.rank("x")
17201720

1721+
def test_rank_use_bottleneck(self):
1722+
v = Variable(["x"], [3.0, 1.0, np.nan, 2.0, 4.0])
1723+
with set_options(use_bottleneck=False):
1724+
with pytest.raises(RuntimeError):
1725+
v.rank("x")
1726+
17211727
@requires_bottleneck
17221728
def test_rank(self):
17231729
import bottleneck as bn

0 commit comments

Comments
 (0)