diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 39ba102ab3782..3973dc18abd59 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -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) diff --git a/pandas/tests/base/test_misc.py b/pandas/tests/base/test_misc.py index 4df55aefdcb06..01705ca31adcd 100644 --- a/pandas/tests/base/test_misc.py +++ b/pandas/tests/base/test_misc.py @@ -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, @@ -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 diff --git a/pandas/tests/generic/test_finalize.py b/pandas/tests/generic/test_finalize.py index c39973d7649e8..d85de12566fb6 100644 --- a/pandas/tests/generic/test_finalize.py +++ b/pandas/tests/generic/test_finalize.py @@ -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 [ diff --git a/pandas/tests/io/test_sql.py b/pandas/tests/io/test_sql.py index d5c6eccad4783..7139971f5f897 100644 --- a/pandas/tests/io/test_sql.py +++ b/pandas/tests/io/test_sql.py @@ -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