Skip to content

TST: Make tests less stateful #51598

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 9 commits into from
Feb 23, 2023
15 changes: 9 additions & 6 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3184,12 +3184,15 @@ def post_processor(
# Item "ExtensionDtype" of "Union[ExtensionDtype, str,
# dtype[Any], Type[object]]" has no attribute "numpy_dtype"
# [union-attr]
return type(orig_vals)(
vals.astype(
inference.numpy_dtype # type: ignore[union-attr]
),
result_mask,
)
with warnings.catch_warnings():
# vals.astype with nan can warn with numpy >1.24
warnings.filterwarnings("ignore", category=RuntimeWarning)
return type(orig_vals)(
vals.astype(
inference.numpy_dtype # type: ignore[union-attr]
),
result_mask,
)

elif not (
is_integer_dtype(inference)
Expand Down
24 changes: 11 additions & 13 deletions pandas/tests/base/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import numpy as np
import pytest

from pandas.compat import (
IS64,
PYPY,
)
from pandas.compat import PYPY

from pandas.core.dtypes.common import (
is_categorical_dtype,
Expand Down Expand Up @@ -87,26 +84,27 @@ def test_ndarray_compat_properties(index_or_series_obj):
@pytest.mark.skipif(PYPY, reason="not relevant for PyPy")
def test_memory_usage(index_or_series_obj):
obj = index_or_series_obj
# Clear index caches so that len(obj) == 0 report 0 memory usage
if isinstance(obj, Series):
is_ser = True
obj.index._engine.clear_mapping()
else:
is_ser = False
obj._engine.clear_mapping()

res = obj.memory_usage()
res_deep = obj.memory_usage(deep=True)

is_ser = isinstance(obj, Series)
is_object = is_object_dtype(obj) or (
isinstance(obj, Series) and is_object_dtype(obj.index)
)
is_object = is_object_dtype(obj) or (is_ser and is_object_dtype(obj.index))
is_categorical = is_categorical_dtype(obj.dtype) or (
isinstance(obj, Series) and is_categorical_dtype(obj.index.dtype)
is_ser and is_categorical_dtype(obj.index.dtype)
)
is_object_string = is_dtype_equal(obj, "string[python]") or (
is_ser and is_dtype_equal(obj.index.dtype, "string[python]")
)

if len(obj) == 0:
if isinstance(obj, Index):
expected = 0
else:
expected = 108 if IS64 else 64
expected = 0
assert res_deep == res == expected
elif is_object or is_categorical or is_object_string:
# only deep will pick them up
Expand Down
63 changes: 59 additions & 4 deletions pandas/tests/generic/test_finalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,16 +489,71 @@ def test_finalize_called_eval_numexpr():
(pd.Series([1]), pd.DataFrame({"A": [1]})),
(pd.DataFrame({"A": [1]}), pd.Series([1])),
],
ids=lambda x: f"({type(x[0]).__name__},{type(x[1]).__name__})",
)
def test_binops(request, args, annotate, all_binary_operators):
# This generates 624 tests... Is that needed?
left, right = args
if annotate == "both" and isinstance(left, int) or isinstance(right, int):
return

if isinstance(left, (pd.DataFrame, pd.Series)):
left.attrs = {}
if isinstance(right, (pd.DataFrame, pd.Series)):
right.attrs = {}

if annotate == "left" and isinstance(left, int):
pytest.skip("left is an int and doesn't support .attrs")
if annotate == "right" and isinstance(right, int):
pytest.skip("right is an int and doesn't support .attrs")

if not (isinstance(left, int) or isinstance(right, int)) and annotate != "both":
if not all_binary_operators.__name__.startswith("r"):
if annotate == "right" and isinstance(left, type(right)):
request.node.add_marker(
pytest.mark.xfail(
reason=f"{all_binary_operators} doesn't work when right has "
f"attrs and both are {type(left)}"
)
)
if not isinstance(left, type(right)):
if annotate == "left" and isinstance(left, pd.Series):
request.node.add_marker(
pytest.mark.xfail(
reason=f"{all_binary_operators} doesn't work when the "
"objects are different Series has attrs"
)
)
elif annotate == "right" and isinstance(right, pd.Series):
request.node.add_marker(
pytest.mark.xfail(
reason=f"{all_binary_operators} doesn't work when the "
"objects are different Series has attrs"
)
)
else:
if annotate == "left" and isinstance(left, type(right)):
request.node.add_marker(
pytest.mark.xfail(
reason=f"{all_binary_operators} doesn't work when left has "
f"attrs and both are {type(left)}"
)
)
if not isinstance(left, type(right)):
if annotate == "right" and isinstance(right, pd.Series):
request.node.add_marker(
pytest.mark.xfail(
reason=f"{all_binary_operators} doesn't work when the "
"objects are different Series has attrs"
)
)
elif annotate == "left" and isinstance(left, pd.Series):
request.node.add_marker(
pytest.mark.xfail(
reason=f"{all_binary_operators} doesn't work when the "
"objects are different Series has attrs"
)
)
if annotate in {"left", "both"} and not isinstance(left, int):
left.attrs = {"a": 1}
if annotate in {"left", "both"} and not isinstance(right, int):
if annotate in {"right", "both"} and not isinstance(right, int):
right.attrs = {"a": 1}

is_cmp = all_binary_operators in [
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,10 @@ def test_create_table(self):
insp = inspect(temp_conn)
assert insp.has_table("temp_frame")

# Cleanup
with sql.SQLDatabase(temp_conn, need_transaction=True) as pandasSQL:
pandasSQL.drop_table("temp_frame")

def test_drop_table(self):
from sqlalchemy import inspect

Expand Down