Skip to content

Skipif no scipy #18794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pandas.core.dtypes.common as com
import pandas.util.testing as tm
import pandas.util._test_decorators as td


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


def test_is_sparse():
@pytest.mark.parametrize("check_scipy", [
False, pytest.param(True, marks=td.skip_if_no_scipy)
])
def test_is_sparse(check_scipy):
assert com.is_sparse(pd.SparseArray([1, 2, 3]))
assert com.is_sparse(pd.SparseSeries([1, 2, 3]))

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

# This test will only skip if the previous assertions
# pass AND scipy is not installed.
sparse = pytest.importorskip("scipy.sparse")
assert not com.is_sparse(sparse.bsr_matrix([1, 2, 3]))
if check_scipy:
import scipy.sparse
assert not com.is_sparse(scipy.sparse.bsr_matrix([1, 2, 3]))


@td.skip_if_no_scipy
def test_is_scipy_sparse():
tm._skip_if_no_scipy()

from scipy.sparse import bsr_matrix
assert com.is_scipy_sparse(bsr_matrix([1, 2, 3]))

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


def test_is_extension_type():
@pytest.mark.parametrize("check_scipy", [
False, pytest.param(True, marks=td.skip_if_no_scipy)
])
def test_is_extension_type(check_scipy):
assert not com.is_extension_type([1, 2, 3])
assert not com.is_extension_type(np.array([1, 2, 3]))
assert not com.is_extension_type(pd.DatetimeIndex([1, 2, 3]))
Expand All @@ -517,10 +522,9 @@ def test_is_extension_type():
s = pd.Series([], dtype=dtype)
assert com.is_extension_type(s)

# This test will only skip if the previous assertions
# pass AND scipy is not installed.
sparse = pytest.importorskip("scipy.sparse")
assert not com.is_extension_type(sparse.bsr_matrix([1, 2, 3]))
if check_scipy:
import scipy.sparse
assert not com.is_extension_type(scipy.sparse.bsr_matrix([1, 2, 3]))


def test_is_complex_dtype():
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
_ensure_int32,
_ensure_categorical)
from pandas.util import testing as tm
import pandas.util._test_decorators as td


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


@td.skip_if_no_scipy
def test_is_scipy_sparse(spmatrix): # noqa: F811
tm._skip_if_no_scipy()
assert is_scipy_sparse(spmatrix([[0, 1]]))
assert not is_scipy_sparse(np.array([1]))

Expand Down
23 changes: 10 additions & 13 deletions pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import pandas.io.formats.printing as printing

import pandas.util.testing as tm
import pandas.util._test_decorators as td
from pandas.tests.frame.common import TestData


Expand All @@ -30,22 +31,22 @@ class TestDataFrameAnalytics(TestData):
# ---------------------------------------------------------------------=
# Correlation and covariance

@td.skip_if_no_scipy
def test_corr_pearson(self):
tm._skip_if_no_scipy()
self.frame['A'][:5] = nan
self.frame['B'][5:10] = nan

self._check_method('pearson')

@td.skip_if_no_scipy
def test_corr_kendall(self):
tm._skip_if_no_scipy()
self.frame['A'][:5] = nan
self.frame['B'][5:10] = nan

self._check_method('kendall')

@td.skip_if_no_scipy
def test_corr_spearman(self):
tm._skip_if_no_scipy()
self.frame['A'][:5] = nan
self.frame['B'][5:10] = nan

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

@td.skip_if_no_scipy
def test_corr_non_numeric(self):
tm._skip_if_no_scipy()
self.frame['A'][:5] = nan
self.frame['B'][5:10] = nan

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

@td.skip_if_no_scipy
def test_corr_nooverlap(self):
tm._skip_if_no_scipy()

# nothing in common
for meth in ['pearson', 'kendall', 'spearman']:
df = DataFrame({'A': [1, 1.5, 1, np.nan, np.nan, np.nan],
Expand All @@ -88,9 +88,8 @@ def test_corr_nooverlap(self):
assert rs.loc['B', 'B'] == 1
assert isna(rs.loc['C', 'C'])

@td.skip_if_no_scipy
def test_corr_constant(self):
tm._skip_if_no_scipy()

# constant --> all NA

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

@td.skip_if_no_scipy
def test_corr_int_and_boolean(self):
tm._skip_if_no_scipy()

# when dtypes of pandas series are different
# then ndarray will have dtype=object,
# so it need to be properly handled
Expand Down Expand Up @@ -719,8 +717,8 @@ def test_sem(self):
result = nanops.nansem(arr, axis=0)
assert not (result < 0).any()

@td.skip_if_no_scipy
def test_skew(self):
tm._skip_if_no_scipy()
from scipy.stats import skew

def alt(x):
Expand All @@ -730,9 +728,8 @@ def alt(x):

self._check_stat_op('skew', alt)

@td.skip_if_no_scipy
def test_kurt(self):
tm._skip_if_no_scipy()

from scipy.stats import kurtosis

def alt(x):
Expand Down
19 changes: 10 additions & 9 deletions pandas/tests/frame/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pandas.util.testing import assert_series_equal, assert_frame_equal

import pandas.util.testing as tm
import pandas.util._test_decorators as td
from pandas.tests.frame.common import TestData, _check_mixed_float


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

@td.skip_if_no_scipy
def test_interp_various(self):
tm._skip_if_no_scipy()

df = DataFrame({'A': [1, 2, np.nan, 4, 5, np.nan, 7],
'C': [1, 2, 3, 5, 8, 13, 21]})
df = df.set_index('C')
Expand Down Expand Up @@ -695,8 +695,8 @@ def test_interp_various(self):
expected.A.loc[13] = 5
assert_frame_equal(result, expected, check_dtype=False)

@td.skip_if_no_scipy
def test_interp_alt_scipy(self):
tm._skip_if_no_scipy()
df = DataFrame({'A': [1, 2, np.nan, 4, 5, np.nan, 7],
'C': [1, 2, 3, 5, 8, 13, 21]})
result = df.interpolate(method='barycentric')
Expand Down Expand Up @@ -739,8 +739,6 @@ def test_interp_rowwise(self):
expected[4] = expected[4].astype(np.float64)
assert_frame_equal(result, expected)

# scipy route
tm._skip_if_no_scipy()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally did not replace this with a decorator because it looks unnecessary (none of the subsequent code requires spicy)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

result = df.interpolate(axis=1, method='values')
assert_frame_equal(result, expected)

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

def test_interp_leading_nans(self):
@pytest.mark.parametrize("check_scipy", [
False, pytest.param(True, marks=td.skip_if_no_scipy)
])
def test_interp_leading_nans(self, check_scipy):
df = DataFrame({"A": [np.nan, np.nan, .5, .25, 0],
"B": [np.nan, -3, -3.5, np.nan, -4]})
result = df.interpolate()
expected = df.copy()
expected['B'].loc[3] = -3.75
assert_frame_equal(result, expected)

tm._skip_if_no_scipy()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of using kwargs for this function as noted in the subsequent comment, but it would slightly change the test given expected is a copy of the interpolation being done without scipy. The subsequent scipy interpolation is still compared back to that object for equality, which may or may not be intentional

result = df.interpolate(method='polynomial', order=1)
assert_frame_equal(result, expected)
if check_scipy:
result = df.interpolate(method='polynomial', order=1)
assert_frame_equal(result, expected)

def test_interp_raise_on_only_mixed(self):
df = DataFrame({'A': [1, 2, np.nan, 4],
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,10 @@ def test_secondary_y_ts(self):
assert ax.get_yaxis().get_visible()

@pytest.mark.slow
@td.skip_if_no_scipy
def test_secondary_kde(self):
if not self.mpl_ge_1_5_0:
pytest.skip("mpl is not supported")
tm._skip_if_no_scipy()
_skip_if_no_scipy_gaussian_kde()

ser = Series(np.random.randn(10))
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/plotting/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
class TestDeprecatedNameSpace(TestPlotBase):

@pytest.mark.slow
@td.skip_if_no_scipy
def test_scatter_plot_legacy(self):
tm._skip_if_no_scipy()

df = pd.DataFrame(randn(100, 2))

with tm.assert_produces_warning(FutureWarning):
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,8 +1398,8 @@ def test_boxplot_subplots_return_type(self):
check_ax_title=False)

@pytest.mark.slow
@td.skip_if_no_scipy
def test_kde_df(self):
tm._skip_if_no_scipy()
_skip_if_no_scipy_gaussian_kde()
if not self.mpl_ge_1_5_0:
pytest.skip("mpl is not supported")
Expand All @@ -1422,8 +1422,8 @@ def test_kde_df(self):
self._check_ax_scales(axes, yaxis='log')

@pytest.mark.slow
@td.skip_if_no_scipy
def test_kde_missing_vals(self):
tm._skip_if_no_scipy()
_skip_if_no_scipy_gaussian_kde()
if not self.mpl_ge_1_5_0:
pytest.skip("mpl is not supported")
Expand Down Expand Up @@ -1949,8 +1949,8 @@ def test_hist_colors(self):
tm.close()

@pytest.mark.slow
@td.skip_if_no_scipy
def test_kde_colors(self):
tm._skip_if_no_scipy()
_skip_if_no_scipy_gaussian_kde()
if not self.mpl_ge_1_5_0:
pytest.skip("mpl is not supported")
Expand All @@ -1974,8 +1974,8 @@ def test_kde_colors(self):
self._check_colors(ax.get_lines(), linecolors=rgba_colors)

@pytest.mark.slow
@td.skip_if_no_scipy
def test_kde_colors_and_styles_subplots(self):
tm._skip_if_no_scipy()
_skip_if_no_scipy_gaussian_kde()
if not self.mpl_ge_1_5_0:
pytest.skip("mpl is not supported")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def test_bootstrap_plot(self):
@td.skip_if_no_mpl
class TestDataFramePlots(TestPlotBase):

@td.skip_if_no_scipy
def test_scatter_matrix_axis(self):
tm._skip_if_no_scipy()
scatter_matrix = plotting.scatter_matrix

with tm.RNGContext(42):
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ def test_plot_fails_with_dupe_color_and_style(self):
x.plot(style='k--', color='k', ax=ax)

@pytest.mark.slow
@td.skip_if_no_scipy
def test_hist_kde(self):
if not self.mpl_ge_1_5_0:
pytest.skip("mpl is not supported")
Expand All @@ -602,7 +603,6 @@ def test_hist_kde(self):
ylabels = ax.get_yticklabels()
self._check_text_labels(ylabels, [''] * len(ylabels))

tm._skip_if_no_scipy()
_skip_if_no_scipy_gaussian_kde()
_check_plot_works(self.ts.plot.kde)
_check_plot_works(self.ts.plot.density)
Expand All @@ -615,8 +615,8 @@ def test_hist_kde(self):
self._check_text_labels(ylabels, [''] * len(ylabels))

@pytest.mark.slow
@td.skip_if_no_scipy
def test_kde_kwargs(self):
tm._skip_if_no_scipy()
_skip_if_no_scipy_gaussian_kde()
if not self.mpl_ge_1_5_0:
pytest.skip("mpl is not supported")
Expand All @@ -633,8 +633,8 @@ def test_kde_kwargs(self):
self._check_text_labels(ax.yaxis.get_label(), 'Density')

@pytest.mark.slow
@td.skip_if_no_scipy
def test_kde_missing_vals(self):
tm._skip_if_no_scipy()
_skip_if_no_scipy_gaussian_kde()
if not self.mpl_ge_1_5_0:
pytest.skip("mpl is not supported")
Expand Down Expand Up @@ -665,6 +665,7 @@ def test_hist_kwargs(self):
tm.close()

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

tm._skip_if_no_scipy()
_skip_if_no_scipy_gaussian_kde()
_, ax = self.plt.subplots()
ax = self.ts.plot.kde(logy=True, color='r', ax=ax)
Expand Down
Loading