Skip to content

TST: Use pytest or decorator instead of checking ImportError #45152

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 3 commits into from
Jan 2, 2022
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
34 changes: 11 additions & 23 deletions pandas/tests/computation/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,24 @@


def test_compat():
# test we have compat with our version of nu
# test we have compat with our version of numexpr

from pandas.core.computation.check import NUMEXPR_INSTALLED

try:
import numexpr as ne
ne = pytest.importorskip("numexpr")

ver = ne.__version__
if Version(ver) < Version(VERSIONS["numexpr"]):
assert not NUMEXPR_INSTALLED
else:
assert NUMEXPR_INSTALLED
except ImportError:
pytest.skip("not testing numexpr version compat")
ver = ne.__version__
if Version(ver) < Version(VERSIONS["numexpr"]):
assert not NUMEXPR_INSTALLED
else:
assert NUMEXPR_INSTALLED


@pytest.mark.parametrize("engine", ENGINES)
@pytest.mark.parametrize("parser", expr.PARSERS)
def test_invalid_numexpr_version(engine, parser):
def testit():
a, b = 1, 2 # noqa:F841
res = pd.eval("a + b", engine=engine, parser=parser)
assert res == 3

if engine == "numexpr":
try:
import numexpr as ne # noqa:F401
except ImportError:
pytest.skip("no numexpr")
else:
testit()
else:
testit()
pytest.importorskip("numexpr")
a, b = 1, 2 # noqa:F841
res = pd.eval("a + b", engine=engine, parser=parser)
assert res == 3
55 changes: 22 additions & 33 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,11 @@ def test_stat_op_api(self, float_frame, float_string_frame):
assert_stat_op_api("sem", float_frame, float_string_frame)
assert_stat_op_api("median", float_frame, float_string_frame)

try:
from scipy.stats import ( # noqa:F401
kurtosis,
skew,
)

assert_stat_op_api("skew", float_frame, float_string_frame)
assert_stat_op_api("kurt", float_frame, float_string_frame)
except ImportError:
pass
@pytest.mark.filterwarnings("ignore:Dropping of nuisance:FutureWarning")
Copy link
Member

Choose a reason for hiding this comment

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

why is this necessary?

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'm getting

pandas/tests/frame/test_reductions.py::TestDataFrameAnalytics::test_stat_op_api_skew_kurt
  /pandas/tests/frame/test_reductions.py:160: FutureWarning: Dropping of nuisance columns in DataFrame reductions (with 'numeric_only=None') is deprecated; in a future version this will raise TypeError.  Select only valid columns before calling the reduction.
    getattr(float_string_frame, opname)(axis=0)

without it. Copying it from the original larger test, guessing it's based on the fixture data and assert_stat_op_api

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I hadn't noticed it was part of the original test, thanks for explaining

@td.skip_if_no_scipy
def test_stat_op_api_skew_kurt(self, float_frame, float_string_frame):
assert_stat_op_api("skew", float_frame, float_string_frame)
assert_stat_op_api("kurt", float_frame, float_string_frame)

def test_stat_op_calc(self, float_frame_with_na, mixed_float_frame):
def count(s):
Expand All @@ -315,20 +310,6 @@ def std(x):
def sem(x):
return np.std(x, ddof=1) / np.sqrt(len(x))

def skewness(x):
from scipy.stats import skew # noqa:F811

if len(x) < 3:
return np.nan
return skew(x, bias=False)

def kurt(x):
from scipy.stats import kurtosis # noqa:F811

if len(x) < 4:
return np.nan
return kurtosis(x, bias=False)

assert_stat_op_calc(
"nunique",
nunique,
Expand Down Expand Up @@ -371,16 +352,24 @@ def kurt(x):
check_dates=True,
)

try:
from scipy import ( # noqa:F401
kurtosis,
skew,
)
@td.skip_if_no_scipy
def test_stat_op_calc_skew_kurtosis(self, float_frame_with_na):
def skewness(x):
from scipy.stats import skew

if len(x) < 3:
return np.nan
return skew(x, bias=False)

def kurt(x):
from scipy.stats import kurtosis

if len(x) < 4:
return np.nan
return kurtosis(x, bias=False)

assert_stat_op_calc("skew", skewness, float_frame_with_na)
assert_stat_op_calc("kurt", kurt, float_frame_with_na)
except ImportError:
pass
assert_stat_op_calc("skew", skewness, float_frame_with_na)
assert_stat_op_calc("kurt", kurt, float_frame_with_na)

# TODO: Ensure warning isn't emitted in the first place
# ignore mean of empty slice and all-NaN
Expand Down