Skip to content

Commit a845187

Browse files
WillAydjreback
authored andcommitted
Skipif no scipy (#18794)
1 parent 0303d0d commit a845187

17 files changed

+135
-148
lines changed

pandas/tests/dtypes/test_common.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import pandas.core.dtypes.common as com
1111
import pandas.util.testing as tm
12+
import pandas.util._test_decorators as td
1213

1314

1415
class TestPandasDtype(object):
@@ -132,21 +133,22 @@ def test_is_object():
132133
assert not com.is_object_dtype([1, 2, 3])
133134

134135

135-
def test_is_sparse():
136+
@pytest.mark.parametrize("check_scipy", [
137+
False, pytest.param(True, marks=td.skip_if_no_scipy)
138+
])
139+
def test_is_sparse(check_scipy):
136140
assert com.is_sparse(pd.SparseArray([1, 2, 3]))
137141
assert com.is_sparse(pd.SparseSeries([1, 2, 3]))
138142

139143
assert not com.is_sparse(np.array([1, 2, 3]))
140144

141-
# This test will only skip if the previous assertions
142-
# pass AND scipy is not installed.
143-
sparse = pytest.importorskip("scipy.sparse")
144-
assert not com.is_sparse(sparse.bsr_matrix([1, 2, 3]))
145+
if check_scipy:
146+
import scipy.sparse
147+
assert not com.is_sparse(scipy.sparse.bsr_matrix([1, 2, 3]))
145148

146149

150+
@td.skip_if_no_scipy
147151
def test_is_scipy_sparse():
148-
tm._skip_if_no_scipy()
149-
150152
from scipy.sparse import bsr_matrix
151153
assert com.is_scipy_sparse(bsr_matrix([1, 2, 3]))
152154

@@ -501,7 +503,10 @@ def test_is_bool_dtype():
501503
assert com.is_bool_dtype(pd.Index([True, False]))
502504

503505

504-
def test_is_extension_type():
506+
@pytest.mark.parametrize("check_scipy", [
507+
False, pytest.param(True, marks=td.skip_if_no_scipy)
508+
])
509+
def test_is_extension_type(check_scipy):
505510
assert not com.is_extension_type([1, 2, 3])
506511
assert not com.is_extension_type(np.array([1, 2, 3]))
507512
assert not com.is_extension_type(pd.DatetimeIndex([1, 2, 3]))
@@ -517,10 +522,9 @@ def test_is_extension_type():
517522
s = pd.Series([], dtype=dtype)
518523
assert com.is_extension_type(s)
519524

520-
# This test will only skip if the previous assertions
521-
# pass AND scipy is not installed.
522-
sparse = pytest.importorskip("scipy.sparse")
523-
assert not com.is_extension_type(sparse.bsr_matrix([1, 2, 3]))
525+
if check_scipy:
526+
import scipy.sparse
527+
assert not com.is_extension_type(scipy.sparse.bsr_matrix([1, 2, 3]))
524528

525529

526530
def test_is_complex_dtype():

pandas/tests/dtypes/test_inference.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
_ensure_int32,
3838
_ensure_categorical)
3939
from pandas.util import testing as tm
40+
import pandas.util._test_decorators as td
4041

4142

4243
@pytest.fixture(params=[True, False], ids=str)
@@ -1190,8 +1191,8 @@ def test_nan_to_nat_conversions():
11901191
assert (s[8].value == np.datetime64('NaT').astype(np.int64))
11911192

11921193

1194+
@td.skip_if_no_scipy
11931195
def test_is_scipy_sparse(spmatrix): # noqa: F811
1194-
tm._skip_if_no_scipy()
11951196
assert is_scipy_sparse(spmatrix([[0, 1]]))
11961197
assert not is_scipy_sparse(np.array([1]))
11971198

pandas/tests/frame/test_analytics.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import pandas.io.formats.printing as printing
2323

2424
import pandas.util.testing as tm
25+
import pandas.util._test_decorators as td
2526
from pandas.tests.frame.common import TestData
2627

2728

@@ -30,22 +31,22 @@ class TestDataFrameAnalytics(TestData):
3031
# ---------------------------------------------------------------------=
3132
# Correlation and covariance
3233

34+
@td.skip_if_no_scipy
3335
def test_corr_pearson(self):
34-
tm._skip_if_no_scipy()
3536
self.frame['A'][:5] = nan
3637
self.frame['B'][5:10] = nan
3738

3839
self._check_method('pearson')
3940

41+
@td.skip_if_no_scipy
4042
def test_corr_kendall(self):
41-
tm._skip_if_no_scipy()
4243
self.frame['A'][:5] = nan
4344
self.frame['B'][5:10] = nan
4445

4546
self._check_method('kendall')
4647

48+
@td.skip_if_no_scipy
4749
def test_corr_spearman(self):
48-
tm._skip_if_no_scipy()
4950
self.frame['A'][:5] = nan
5051
self.frame['B'][5:10] = nan
5152

@@ -62,8 +63,8 @@ def _check_method(self, method='pearson', check_minp=False):
6263
expected.loc['A', 'B'] = expected.loc['B', 'A'] = nan
6364
tm.assert_frame_equal(result, expected)
6465

66+
@td.skip_if_no_scipy
6567
def test_corr_non_numeric(self):
66-
tm._skip_if_no_scipy()
6768
self.frame['A'][:5] = nan
6869
self.frame['B'][5:10] = nan
6970

@@ -72,9 +73,8 @@ def test_corr_non_numeric(self):
7273
expected = self.mixed_frame.loc[:, ['A', 'B', 'C', 'D']].corr()
7374
tm.assert_frame_equal(result, expected)
7475

76+
@td.skip_if_no_scipy
7577
def test_corr_nooverlap(self):
76-
tm._skip_if_no_scipy()
77-
7878
# nothing in common
7979
for meth in ['pearson', 'kendall', 'spearman']:
8080
df = DataFrame({'A': [1, 1.5, 1, np.nan, np.nan, np.nan],
@@ -88,9 +88,8 @@ def test_corr_nooverlap(self):
8888
assert rs.loc['B', 'B'] == 1
8989
assert isna(rs.loc['C', 'C'])
9090

91+
@td.skip_if_no_scipy
9192
def test_corr_constant(self):
92-
tm._skip_if_no_scipy()
93-
9493
# constant --> all NA
9594

9695
for meth in ['pearson', 'spearman']:
@@ -106,9 +105,8 @@ def test_corr_int(self):
106105
df3.cov()
107106
df3.corr()
108107

108+
@td.skip_if_no_scipy
109109
def test_corr_int_and_boolean(self):
110-
tm._skip_if_no_scipy()
111-
112110
# when dtypes of pandas series are different
113111
# then ndarray will have dtype=object,
114112
# so it need to be properly handled
@@ -719,8 +717,8 @@ def test_sem(self):
719717
result = nanops.nansem(arr, axis=0)
720718
assert not (result < 0).any()
721719

720+
@td.skip_if_no_scipy
722721
def test_skew(self):
723-
tm._skip_if_no_scipy()
724722
from scipy.stats import skew
725723

726724
def alt(x):
@@ -730,9 +728,8 @@ def alt(x):
730728

731729
self._check_stat_op('skew', alt)
732730

731+
@td.skip_if_no_scipy
733732
def test_kurt(self):
734-
tm._skip_if_no_scipy()
735-
736733
from scipy.stats import kurtosis
737734

738735
def alt(x):

pandas/tests/frame/test_missing.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pandas.util.testing import assert_series_equal, assert_frame_equal
1717

1818
import pandas.util.testing as tm
19+
import pandas.util._test_decorators as td
1920
from pandas.tests.frame.common import TestData, _check_mixed_float
2021

2122

@@ -646,9 +647,8 @@ def test_interp_nan_idx(self):
646647
with pytest.raises(NotImplementedError):
647648
df.interpolate(method='values')
648649

650+
@td.skip_if_no_scipy
649651
def test_interp_various(self):
650-
tm._skip_if_no_scipy()
651-
652652
df = DataFrame({'A': [1, 2, np.nan, 4, 5, np.nan, 7],
653653
'C': [1, 2, 3, 5, 8, 13, 21]})
654654
df = df.set_index('C')
@@ -695,8 +695,8 @@ def test_interp_various(self):
695695
expected.A.loc[13] = 5
696696
assert_frame_equal(result, expected, check_dtype=False)
697697

698+
@td.skip_if_no_scipy
698699
def test_interp_alt_scipy(self):
699-
tm._skip_if_no_scipy()
700700
df = DataFrame({'A': [1, 2, np.nan, 4, 5, np.nan, 7],
701701
'C': [1, 2, 3, 5, 8, 13, 21]})
702702
result = df.interpolate(method='barycentric')
@@ -739,8 +739,6 @@ def test_interp_rowwise(self):
739739
expected[4] = expected[4].astype(np.float64)
740740
assert_frame_equal(result, expected)
741741

742-
# scipy route
743-
tm._skip_if_no_scipy()
744742
result = df.interpolate(axis=1, method='values')
745743
assert_frame_equal(result, expected)
746744

@@ -753,17 +751,20 @@ def test_rowwise_alt(self):
753751
1: [1, 2, 3, 4, 3, 2, 1, 0, -1]})
754752
df.interpolate(axis=0)
755753

756-
def test_interp_leading_nans(self):
754+
@pytest.mark.parametrize("check_scipy", [
755+
False, pytest.param(True, marks=td.skip_if_no_scipy)
756+
])
757+
def test_interp_leading_nans(self, check_scipy):
757758
df = DataFrame({"A": [np.nan, np.nan, .5, .25, 0],
758759
"B": [np.nan, -3, -3.5, np.nan, -4]})
759760
result = df.interpolate()
760761
expected = df.copy()
761762
expected['B'].loc[3] = -3.75
762763
assert_frame_equal(result, expected)
763764

764-
tm._skip_if_no_scipy()
765-
result = df.interpolate(method='polynomial', order=1)
766-
assert_frame_equal(result, expected)
765+
if check_scipy:
766+
result = df.interpolate(method='polynomial', order=1)
767+
assert_frame_equal(result, expected)
767768

768769
def test_interp_raise_on_only_mixed(self):
769770
df = DataFrame({'A': [1, 2, np.nan, 4],

pandas/tests/plotting/test_datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,10 +643,10 @@ def test_secondary_y_ts(self):
643643
assert ax.get_yaxis().get_visible()
644644

645645
@pytest.mark.slow
646+
@td.skip_if_no_scipy
646647
def test_secondary_kde(self):
647648
if not self.mpl_ge_1_5_0:
648649
pytest.skip("mpl is not supported")
649-
tm._skip_if_no_scipy()
650650
_skip_if_no_scipy_gaussian_kde()
651651

652652
ser = Series(np.random.randn(10))

pandas/tests/plotting/test_deprecated.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
class TestDeprecatedNameSpace(TestPlotBase):
2525

2626
@pytest.mark.slow
27+
@td.skip_if_no_scipy
2728
def test_scatter_plot_legacy(self):
28-
tm._skip_if_no_scipy()
29-
3029
df = pd.DataFrame(randn(100, 2))
3130

3231
with tm.assert_produces_warning(FutureWarning):

pandas/tests/plotting/test_frame.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1398,8 +1398,8 @@ def test_boxplot_subplots_return_type(self):
13981398
check_ax_title=False)
13991399

14001400
@pytest.mark.slow
1401+
@td.skip_if_no_scipy
14011402
def test_kde_df(self):
1402-
tm._skip_if_no_scipy()
14031403
_skip_if_no_scipy_gaussian_kde()
14041404
if not self.mpl_ge_1_5_0:
14051405
pytest.skip("mpl is not supported")
@@ -1422,8 +1422,8 @@ def test_kde_df(self):
14221422
self._check_ax_scales(axes, yaxis='log')
14231423

14241424
@pytest.mark.slow
1425+
@td.skip_if_no_scipy
14251426
def test_kde_missing_vals(self):
1426-
tm._skip_if_no_scipy()
14271427
_skip_if_no_scipy_gaussian_kde()
14281428
if not self.mpl_ge_1_5_0:
14291429
pytest.skip("mpl is not supported")
@@ -1949,8 +1949,8 @@ def test_hist_colors(self):
19491949
tm.close()
19501950

19511951
@pytest.mark.slow
1952+
@td.skip_if_no_scipy
19521953
def test_kde_colors(self):
1953-
tm._skip_if_no_scipy()
19541954
_skip_if_no_scipy_gaussian_kde()
19551955
if not self.mpl_ge_1_5_0:
19561956
pytest.skip("mpl is not supported")
@@ -1974,8 +1974,8 @@ def test_kde_colors(self):
19741974
self._check_colors(ax.get_lines(), linecolors=rgba_colors)
19751975

19761976
@pytest.mark.slow
1977+
@td.skip_if_no_scipy
19771978
def test_kde_colors_and_styles_subplots(self):
1978-
tm._skip_if_no_scipy()
19791979
_skip_if_no_scipy_gaussian_kde()
19801980
if not self.mpl_ge_1_5_0:
19811981
pytest.skip("mpl is not supported")

pandas/tests/plotting/test_misc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def test_bootstrap_plot(self):
5252
@td.skip_if_no_mpl
5353
class TestDataFramePlots(TestPlotBase):
5454

55+
@td.skip_if_no_scipy
5556
def test_scatter_matrix_axis(self):
56-
tm._skip_if_no_scipy()
5757
scatter_matrix = plotting.scatter_matrix
5858

5959
with tm.RNGContext(42):

pandas/tests/plotting/test_series.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ def test_plot_fails_with_dupe_color_and_style(self):
589589
x.plot(style='k--', color='k', ax=ax)
590590

591591
@pytest.mark.slow
592+
@td.skip_if_no_scipy
592593
def test_hist_kde(self):
593594
if not self.mpl_ge_1_5_0:
594595
pytest.skip("mpl is not supported")
@@ -602,7 +603,6 @@ def test_hist_kde(self):
602603
ylabels = ax.get_yticklabels()
603604
self._check_text_labels(ylabels, [''] * len(ylabels))
604605

605-
tm._skip_if_no_scipy()
606606
_skip_if_no_scipy_gaussian_kde()
607607
_check_plot_works(self.ts.plot.kde)
608608
_check_plot_works(self.ts.plot.density)
@@ -615,8 +615,8 @@ def test_hist_kde(self):
615615
self._check_text_labels(ylabels, [''] * len(ylabels))
616616

617617
@pytest.mark.slow
618+
@td.skip_if_no_scipy
618619
def test_kde_kwargs(self):
619-
tm._skip_if_no_scipy()
620620
_skip_if_no_scipy_gaussian_kde()
621621
if not self.mpl_ge_1_5_0:
622622
pytest.skip("mpl is not supported")
@@ -633,8 +633,8 @@ def test_kde_kwargs(self):
633633
self._check_text_labels(ax.yaxis.get_label(), 'Density')
634634

635635
@pytest.mark.slow
636+
@td.skip_if_no_scipy
636637
def test_kde_missing_vals(self):
637-
tm._skip_if_no_scipy()
638638
_skip_if_no_scipy_gaussian_kde()
639639
if not self.mpl_ge_1_5_0:
640640
pytest.skip("mpl is not supported")
@@ -665,6 +665,7 @@ def test_hist_kwargs(self):
665665
tm.close()
666666

667667
@pytest.mark.slow
668+
@td.skip_if_no_scipy
668669
def test_hist_kde_color(self):
669670
if not self.mpl_ge_1_5_0:
670671
pytest.skip("mpl is not supported")
@@ -675,7 +676,6 @@ def test_hist_kde_color(self):
675676
assert len(ax.patches) == 10
676677
self._check_colors(ax.patches, facecolors=['b'] * 10)
677678

678-
tm._skip_if_no_scipy()
679679
_skip_if_no_scipy_gaussian_kde()
680680
_, ax = self.plt.subplots()
681681
ax = self.ts.plot.kde(logy=True, color='r', ax=ax)

0 commit comments

Comments
 (0)