Skip to content

Commit 77e6556

Browse files
pilkibunjorisvandenbossche
pilkibun
authored andcommitted
CLN: introduce test decorator skip_if_np_lt(ver_string) (#26901)
1 parent baa77c3 commit 77e6556

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

pandas/compat/numpy/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
""" support numpy compatiblitiy across versions """
22

3-
import re
4-
import numpy as np
53
from distutils.version import LooseVersion
4+
import re
65

6+
import numpy as np
77

88
# numpy versioning
99
_np_version = np.__version__
@@ -62,6 +62,7 @@ def np_array_datetime64_compat(arr, *args, **kwargs):
6262

6363

6464
__all__ = ['np',
65+
'_np_version',
6566
'_np_version_under1p14',
6667
'_np_version_under1p15',
6768
'_np_version_under1p16',

pandas/tests/arrays/sparse/test_array.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import pytest
77

88
from pandas._libs.sparse import IntIndex
9-
from pandas.compat.numpy import _np_version_under1p16
109
import pandas.util._test_decorators as td
1110

1211
import pandas as pd
@@ -175,8 +174,8 @@ def test_constructor_inferred_fill_value(self, data, fill_value):
175174
@pytest.mark.parametrize('format', ['coo', 'csc', 'csr'])
176175
@pytest.mark.parametrize('size', [
177176
pytest.param(0,
178-
marks=pytest.mark.skipif(_np_version_under1p16,
179-
reason='NumPy-11383')),
177+
marks=td.skip_if_np_lt("1.16",
178+
reason='NumPy-11383')),
180179
10
181180
])
182181
@td.skip_if_no_scipy
@@ -870,7 +869,7 @@ def test_all(self, data, pos, neg):
870869
([1, 2, 1], 1, 0),
871870
([1.0, 2.0, 1.0], 1.0, 0.0)
872871
])
873-
@td.skip_if_np_lt_115 # prior didn't dispatch
872+
@td.skip_if_np_lt("1.15") # prior didn't dispatch
874873
def test_numpy_all(self, data, pos, neg):
875874
# GH 17570
876875
out = np.all(SparseArray(data))
@@ -916,7 +915,7 @@ def test_any(self, data, pos, neg):
916915
([0, 2, 0], 2, 0),
917916
([0.0, 2.0, 0.0], 2.0, 0.0)
918917
])
919-
@td.skip_if_np_lt_115 # prior didn't dispatch
918+
@td.skip_if_np_lt("1.15") # prior didn't dispatch
920919
def test_numpy_any(self, data, pos, neg):
921920
# GH 17570
922921
out = np.any(SparseArray(data))

pandas/tests/frame/test_analytics.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1565,21 +1565,21 @@ def test_any_all_bool_only(self):
15651565
(np.all, {'A': pd.Series([0, 1], dtype=int)}, False),
15661566
(np.any, {'A': pd.Series([0, 1], dtype=int)}, True),
15671567
pytest.param(np.all, {'A': pd.Series([0, 1], dtype='M8[ns]')}, False,
1568-
marks=[td.skip_if_np_lt_115]),
1568+
marks=[td.skip_if_np_lt("1.15")]),
15691569
pytest.param(np.any, {'A': pd.Series([0, 1], dtype='M8[ns]')}, True,
1570-
marks=[td.skip_if_np_lt_115]),
1570+
marks=[td.skip_if_np_lt("1.15")]),
15711571
pytest.param(np.all, {'A': pd.Series([1, 2], dtype='M8[ns]')}, True,
1572-
marks=[td.skip_if_np_lt_115]),
1572+
marks=[td.skip_if_np_lt("1.15")]),
15731573
pytest.param(np.any, {'A': pd.Series([1, 2], dtype='M8[ns]')}, True,
1574-
marks=[td.skip_if_np_lt_115]),
1574+
marks=[td.skip_if_np_lt("1.15")]),
15751575
pytest.param(np.all, {'A': pd.Series([0, 1], dtype='m8[ns]')}, False,
1576-
marks=[td.skip_if_np_lt_115]),
1576+
marks=[td.skip_if_np_lt("1.15")]),
15771577
pytest.param(np.any, {'A': pd.Series([0, 1], dtype='m8[ns]')}, True,
1578-
marks=[td.skip_if_np_lt_115]),
1578+
marks=[td.skip_if_np_lt("1.15")]),
15791579
pytest.param(np.all, {'A': pd.Series([1, 2], dtype='m8[ns]')}, True,
1580-
marks=[td.skip_if_np_lt_115]),
1580+
marks=[td.skip_if_np_lt("1.15")]),
15811581
pytest.param(np.any, {'A': pd.Series([1, 2], dtype='m8[ns]')}, True,
1582-
marks=[td.skip_if_np_lt_115]),
1582+
marks=[td.skip_if_np_lt("1.15")]),
15831583
(np.all, {'A': pd.Series([0, 1], dtype='category')}, False),
15841584
(np.any, {'A': pd.Series([0, 1], dtype='category')}, True),
15851585
(np.all, {'A': pd.Series([1, 2], dtype='category')}, True),

pandas/tests/series/test_analytics.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ def test_value_counts_categorical_not_ordered(self):
11051105
dict(keepdims=True),
11061106
dict(out=object()),
11071107
])
1108-
@td.skip_if_np_lt_115
1108+
@td.skip_if_np_lt("1.15")
11091109
def test_validate_any_all_out_keepdims_raises(self, kwargs, func):
11101110
s = pd.Series([1, 2])
11111111
param = list(kwargs)[0]
@@ -1117,7 +1117,7 @@ def test_validate_any_all_out_keepdims_raises(self, kwargs, func):
11171117
with pytest.raises(ValueError, match=msg):
11181118
func(s, **kwargs)
11191119

1120-
@td.skip_if_np_lt_115
1120+
@td.skip_if_np_lt("1.15")
11211121
def test_validate_sum_initial(self):
11221122
s = pd.Series([1, 2])
11231123
msg = (r"the 'initial' parameter is not "
@@ -1136,7 +1136,7 @@ def test_validate_median_initial(self):
11361136
# method instead of the ufunc.
11371137
s.median(overwrite_input=True)
11381138

1139-
@td.skip_if_np_lt_115
1139+
@td.skip_if_np_lt("1.15")
11401140
def test_validate_stat_keepdims(self):
11411141
s = pd.Series([1, 2])
11421142
msg = (r"the 'keepdims' parameter is not "

pandas/util/_test_decorators.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ def test_foo():
2323
2424
For more information, refer to the ``pytest`` documentation on ``skipif``.
2525
"""
26+
from distutils.version import LooseVersion
2627
import locale
2728
from typing import Optional
2829

2930
from _pytest.mark.structures import MarkDecorator
3031
import pytest
3132

3233
from pandas.compat import is_platform_32bit, is_platform_windows
33-
from pandas.compat.numpy import _np_version_under1p15
34+
from pandas.compat.numpy import _np_version
3435

3536
from pandas.core.computation.expressions import (
3637
_NUMEXPR_INSTALLED, _USE_NUMEXPR)
@@ -142,8 +143,6 @@ def skip_if_no(
142143

143144
skip_if_no_mpl = pytest.mark.skipif(_skip_if_no_mpl(),
144145
reason="Missing matplotlib dependency")
145-
skip_if_np_lt_115 = pytest.mark.skipif(_np_version_under1p15,
146-
reason="NumPy 1.15 or greater required")
147146
skip_if_mpl = pytest.mark.skipif(not _skip_if_no_mpl(),
148147
reason="matplotlib is present")
149148
skip_if_32bit = pytest.mark.skipif(is_platform_32bit(),
@@ -168,6 +167,13 @@ def skip_if_no(
168167
installed=_NUMEXPR_INSTALLED))
169168

170169

170+
def skip_if_np_lt(ver_str, reason=None, *args, **kwds):
171+
if reason is None:
172+
reason = "NumPy %s or greater required" % ver_str
173+
return pytest.mark.skipif(_np_version < LooseVersion(ver_str),
174+
reason=reason, *args, **kwds)
175+
176+
171177
def parametrize_fixture_doc(*args):
172178
"""
173179
Intended for use as a decorator for parametrized fixture,

0 commit comments

Comments
 (0)