Skip to content

Commit 4efdd18

Browse files
committed
TST: update frame reductions tests to assert PandasFutureWarning for numeric_only=None using tm.assert_produces_warning
1 parent 11de335 commit 4efdd18

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

pandas/tests/frame/test_reductions.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,41 @@ def test_axis_1_sum_na(self, string_dtype_no_object, skipna, min_count):
850850
def test_sum_prod_nanops(self, method, unit, numeric_only):
851851
idx = ["a", "b", "c"]
852852
df = DataFrame({"a": [unit, unit], "b": [unit, np.nan], "c": [np.nan, np.nan]})
853+
# New behavior: numeric_only=None is deprecated; emit a warning but
854+
# continue to accept it during the deprecation period.
855+
if numeric_only is None:
856+
from pandas import errors
857+
858+
with tm.assert_produces_warning(errors.PandasFutureWarning):
859+
# run the same checks as below while asserting we warned
860+
result = getattr(df, method)(numeric_only=numeric_only)
861+
expected = Series([unit, unit, unit], index=idx, dtype="float64")
862+
tm.assert_series_equal(result, expected)
863+
864+
result = getattr(df, method)(numeric_only=numeric_only, min_count=1)
865+
expected = Series([unit, unit, np.nan], index=idx)
866+
tm.assert_series_equal(result, expected)
867+
868+
result = getattr(df, method)(numeric_only=numeric_only, min_count=0)
869+
expected = Series([unit, unit, unit], index=idx, dtype="float64")
870+
tm.assert_series_equal(result, expected)
871+
872+
result = getattr(df.iloc[1:], method)(
873+
numeric_only=numeric_only, min_count=1
874+
)
875+
expected = Series([unit, np.nan, np.nan], index=idx)
876+
tm.assert_series_equal(result, expected)
877+
878+
# min_count > 1 cases
879+
df2 = DataFrame({"A": [unit] * 10, "B": [unit] * 5 + [np.nan] * 5})
880+
result = getattr(df2, method)(numeric_only=numeric_only, min_count=5)
881+
expected = Series(result, index=["A", "B"])
882+
tm.assert_series_equal(result, expected)
883+
884+
result = getattr(df2, method)(numeric_only=numeric_only, min_count=6)
885+
expected = Series(result, index=["A", "B"])
886+
tm.assert_series_equal(result, expected)
887+
return
853888
# The default
854889
result = getattr(df, method)(numeric_only=numeric_only)
855890
expected = Series([unit, unit, unit], index=idx, dtype="float64")
@@ -1757,8 +1792,14 @@ def test_any_all_categorical_dtype_nuisance_column(self, all_boolean_reductions)
17571792
with pytest.raises(TypeError, match="does not support operation"):
17581793
getattr(df, all_boolean_reductions)(bool_only=False)
17591794

1760-
with pytest.raises(TypeError, match="does not support operation"):
1761-
getattr(df, all_boolean_reductions)(bool_only=None)
1795+
# With the deprecation in place, passing None should emit a
1796+
# PandasFutureWarning and then the operation should raise the
1797+
# original TypeError. Capture both.
1798+
from pandas import errors
1799+
1800+
with tm.assert_produces_warning(errors.PandasFutureWarning):
1801+
with pytest.raises(TypeError, match="does not support operation"):
1802+
getattr(df, all_boolean_reductions)(bool_only=None)
17621803

17631804
with pytest.raises(TypeError, match="does not support operation"):
17641805
getattr(np, all_boolean_reductions)(df, axis=0)
@@ -1995,6 +2036,20 @@ def test_minmax_extensionarray(method, numeric_only):
19952036
int64_info = np.iinfo("int64")
19962037
ser = Series([int64_info.max, None, int64_info.min], dtype=pd.Int64Dtype())
19972038
df = DataFrame({"Int64": ser})
2039+
# New behavior: numeric_only=None is deprecated; emit a warning but
2040+
# continue to accept it during the deprecation period.
2041+
if numeric_only is None:
2042+
from pandas import errors
2043+
2044+
with tm.assert_produces_warning(errors.PandasFutureWarning):
2045+
result = getattr(df, method)(numeric_only=numeric_only)
2046+
expected = Series(
2047+
[getattr(int64_info, method)],
2048+
dtype="Int64",
2049+
index=Index(["Int64"]),
2050+
)
2051+
tm.assert_series_equal(result, expected)
2052+
return
19982053
result = getattr(df, method)(numeric_only=numeric_only)
19992054
expected = Series(
20002055
[getattr(int64_info, method)],

0 commit comments

Comments
 (0)