Skip to content

Commit c29567b

Browse files
authored
TST: Make tests less stateful (#51598)
1 parent ae7b6d6 commit c29567b

File tree

4 files changed

+83
-23
lines changed

4 files changed

+83
-23
lines changed

pandas/core/groupby/groupby.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,12 +3184,15 @@ def post_processor(
31843184
# Item "ExtensionDtype" of "Union[ExtensionDtype, str,
31853185
# dtype[Any], Type[object]]" has no attribute "numpy_dtype"
31863186
# [union-attr]
3187-
return type(orig_vals)(
3188-
vals.astype(
3189-
inference.numpy_dtype # type: ignore[union-attr]
3190-
),
3191-
result_mask,
3192-
)
3187+
with warnings.catch_warnings():
3188+
# vals.astype with nan can warn with numpy >1.24
3189+
warnings.filterwarnings("ignore", category=RuntimeWarning)
3190+
return type(orig_vals)(
3191+
vals.astype(
3192+
inference.numpy_dtype # type: ignore[union-attr]
3193+
),
3194+
result_mask,
3195+
)
31933196

31943197
elif not (
31953198
is_integer_dtype(inference)

pandas/tests/base/test_misc.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import numpy as np
44
import pytest
55

6-
from pandas.compat import (
7-
IS64,
8-
PYPY,
9-
)
6+
from pandas.compat import PYPY
107

118
from pandas.core.dtypes.common import (
129
is_categorical_dtype,
@@ -87,26 +84,27 @@ def test_ndarray_compat_properties(index_or_series_obj):
8784
@pytest.mark.skipif(PYPY, reason="not relevant for PyPy")
8885
def test_memory_usage(index_or_series_obj):
8986
obj = index_or_series_obj
87+
# Clear index caches so that len(obj) == 0 report 0 memory usage
88+
if isinstance(obj, Series):
89+
is_ser = True
90+
obj.index._engine.clear_mapping()
91+
else:
92+
is_ser = False
93+
obj._engine.clear_mapping()
9094

9195
res = obj.memory_usage()
9296
res_deep = obj.memory_usage(deep=True)
9397

94-
is_ser = isinstance(obj, Series)
95-
is_object = is_object_dtype(obj) or (
96-
isinstance(obj, Series) and is_object_dtype(obj.index)
97-
)
98+
is_object = is_object_dtype(obj) or (is_ser and is_object_dtype(obj.index))
9899
is_categorical = is_categorical_dtype(obj.dtype) or (
99-
isinstance(obj, Series) and is_categorical_dtype(obj.index.dtype)
100+
is_ser and is_categorical_dtype(obj.index.dtype)
100101
)
101102
is_object_string = is_dtype_equal(obj, "string[python]") or (
102103
is_ser and is_dtype_equal(obj.index.dtype, "string[python]")
103104
)
104105

105106
if len(obj) == 0:
106-
if isinstance(obj, Index):
107-
expected = 0
108-
else:
109-
expected = 108 if IS64 else 64
107+
expected = 0
110108
assert res_deep == res == expected
111109
elif is_object or is_categorical or is_object_string:
112110
# only deep will pick them up

pandas/tests/generic/test_finalize.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,16 +489,71 @@ def test_finalize_called_eval_numexpr():
489489
(pd.Series([1]), pd.DataFrame({"A": [1]})),
490490
(pd.DataFrame({"A": [1]}), pd.Series([1])),
491491
],
492+
ids=lambda x: f"({type(x[0]).__name__},{type(x[1]).__name__})",
492493
)
493494
def test_binops(request, args, annotate, all_binary_operators):
494495
# This generates 624 tests... Is that needed?
495496
left, right = args
496-
if annotate == "both" and isinstance(left, int) or isinstance(right, int):
497-
return
498-
497+
if isinstance(left, (pd.DataFrame, pd.Series)):
498+
left.attrs = {}
499+
if isinstance(right, (pd.DataFrame, pd.Series)):
500+
right.attrs = {}
501+
502+
if annotate == "left" and isinstance(left, int):
503+
pytest.skip("left is an int and doesn't support .attrs")
504+
if annotate == "right" and isinstance(right, int):
505+
pytest.skip("right is an int and doesn't support .attrs")
506+
507+
if not (isinstance(left, int) or isinstance(right, int)) and annotate != "both":
508+
if not all_binary_operators.__name__.startswith("r"):
509+
if annotate == "right" and isinstance(left, type(right)):
510+
request.node.add_marker(
511+
pytest.mark.xfail(
512+
reason=f"{all_binary_operators} doesn't work when right has "
513+
f"attrs and both are {type(left)}"
514+
)
515+
)
516+
if not isinstance(left, type(right)):
517+
if annotate == "left" and isinstance(left, pd.Series):
518+
request.node.add_marker(
519+
pytest.mark.xfail(
520+
reason=f"{all_binary_operators} doesn't work when the "
521+
"objects are different Series has attrs"
522+
)
523+
)
524+
elif annotate == "right" and isinstance(right, pd.Series):
525+
request.node.add_marker(
526+
pytest.mark.xfail(
527+
reason=f"{all_binary_operators} doesn't work when the "
528+
"objects are different Series has attrs"
529+
)
530+
)
531+
else:
532+
if annotate == "left" and isinstance(left, type(right)):
533+
request.node.add_marker(
534+
pytest.mark.xfail(
535+
reason=f"{all_binary_operators} doesn't work when left has "
536+
f"attrs and both are {type(left)}"
537+
)
538+
)
539+
if not isinstance(left, type(right)):
540+
if annotate == "right" and isinstance(right, pd.Series):
541+
request.node.add_marker(
542+
pytest.mark.xfail(
543+
reason=f"{all_binary_operators} doesn't work when the "
544+
"objects are different Series has attrs"
545+
)
546+
)
547+
elif annotate == "left" and isinstance(left, pd.Series):
548+
request.node.add_marker(
549+
pytest.mark.xfail(
550+
reason=f"{all_binary_operators} doesn't work when the "
551+
"objects are different Series has attrs"
552+
)
553+
)
499554
if annotate in {"left", "both"} and not isinstance(left, int):
500555
left.attrs = {"a": 1}
501-
if annotate in {"left", "both"} and not isinstance(right, int):
556+
if annotate in {"right", "both"} and not isinstance(right, int):
502557
right.attrs = {"a": 1}
503558

504559
is_cmp = all_binary_operators in [

pandas/tests/io/test_sql.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,10 @@ def test_create_table(self):
18071807
insp = inspect(temp_conn)
18081808
assert insp.has_table("temp_frame")
18091809

1810+
# Cleanup
1811+
with sql.SQLDatabase(temp_conn, need_transaction=True) as pandasSQL:
1812+
pandasSQL.drop_table("temp_frame")
1813+
18101814
def test_drop_table(self):
18111815
from sqlalchemy import inspect
18121816

0 commit comments

Comments
 (0)