Skip to content

Commit 654c6dd

Browse files
authored
CLN: Enforce deprecation of axis=None in DataFrame reductions (#57684)
* CLN: Enforce deprecation of axis=None in DataFrame reductions * Remove test * cleanup * Skip ASV benchmark
1 parent 0be8f98 commit 654c6dd

File tree

8 files changed

+19
-58
lines changed

8 files changed

+19
-58
lines changed

asv_bench/benchmarks/stat_ops.py

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def setup(self, op, axis):
3333
("median", 1),
3434
("median", None),
3535
("std", 1),
36+
("std", None),
3637
)
3738
):
3839
# Skipping cases where datetime aggregations are not implemented

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ Removal of prior version deprecations/changes
198198
- All arguments in :meth:`Series.to_dict` are now keyword only (:issue:`56493`)
199199
- Changed the default value of ``observed`` in :meth:`DataFrame.groupby` and :meth:`Series.groupby` to ``True`` (:issue:`51811`)
200200
- Enforced deprecation disallowing parsing datetimes with mixed time zones unless user passes ``utc=True`` to :func:`to_datetime` (:issue:`57275`)
201+
- Enforced deprecation of ``axis=None`` acting the same as ``axis=0`` in the DataFrame reductions ``sum``, ``prod``, ``std``, ``var``, and ``sem``, passing ``axis=None`` will now reduce over both axes; this is particularly the case when doing e.g. ``numpy.sum(df)`` (:issue:`21597`)
201202
- Enforced silent-downcasting deprecation for :ref:`all relevant methods <whatsnew_220.silent_downcasting>` (:issue:`54710`)
202203
- In :meth:`DataFrame.stack`, the default value of ``future_stack`` is now ``True``; specifying ``False`` will raise a ``FutureWarning`` (:issue:`55448`)
203204
- Methods ``apply``, ``agg``, and ``transform`` will no longer replace NumPy functions (e.g. ``np.sum``) and built-in functions (e.g. ``min``) with the equivalent pandas implementation; use string aliases (e.g. ``"sum"`` and ``"min"``) if you desire to use the pandas implementation (:issue:`53974`)

pandas/core/frame.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -11559,7 +11559,9 @@ def sum(
1155911559
min_count=min_count,
1156011560
**kwargs,
1156111561
)
11562-
return result.__finalize__(self, method="sum")
11562+
if isinstance(result, Series):
11563+
result = result.__finalize__(self, method="sum")
11564+
return result
1156311565

1156411566
@doc(make_doc("prod", ndim=2))
1156511567
def prod(
@@ -11577,7 +11579,9 @@ def prod(
1157711579
min_count=min_count,
1157811580
**kwargs,
1157911581
)
11580-
return result.__finalize__(self, method="prod")
11582+
if isinstance(result, Series):
11583+
result = result.__finalize__(self, method="prod")
11584+
return result
1158111585

1158211586
# error: Signature of "mean" incompatible with supertype "NDFrame"
1158311587
@overload # type: ignore[override]

pandas/core/generic.py

+2-30
Original file line numberDiff line numberDiff line change
@@ -11447,7 +11447,7 @@ def _stat_function_ddof(
1144711447
self,
1144811448
name: str,
1144911449
func,
11450-
axis: Axis | None | lib.NoDefault = lib.no_default,
11450+
axis: Axis | None = 0,
1145111451
skipna: bool = True,
1145211452
ddof: int = 1,
1145311453
numeric_only: bool = False,
@@ -11456,20 +11456,6 @@ def _stat_function_ddof(
1145611456
nv.validate_stat_ddof_func((), kwargs, fname=name)
1145711457
validate_bool_kwarg(skipna, "skipna", none_allowed=False)
1145811458

11459-
if axis is None:
11460-
if self.ndim > 1:
11461-
warnings.warn(
11462-
f"The behavior of {type(self).__name__}.{name} with axis=None "
11463-
"is deprecated, in a future version this will reduce over both "
11464-
"axes and return a scalar. To retain the old behavior, pass "
11465-
"axis=0 (or do not pass axis)",
11466-
FutureWarning,
11467-
stacklevel=find_stack_level(),
11468-
)
11469-
axis = 0
11470-
elif axis is lib.no_default:
11471-
axis = 0
11472-
1147311459
return self._reduce(
1147411460
func, name, axis=axis, numeric_only=numeric_only, skipna=skipna, ddof=ddof
1147511461
)
@@ -11621,7 +11607,7 @@ def _min_count_stat_function(
1162111607
self,
1162211608
name: str,
1162311609
func,
11624-
axis: Axis | None | lib.NoDefault = lib.no_default,
11610+
axis: Axis | None = 0,
1162511611
skipna: bool = True,
1162611612
numeric_only: bool = False,
1162711613
min_count: int = 0,
@@ -11632,20 +11618,6 @@ def _min_count_stat_function(
1163211618

1163311619
validate_bool_kwarg(skipna, "skipna", none_allowed=False)
1163411620

11635-
if axis is None:
11636-
if self.ndim > 1:
11637-
warnings.warn(
11638-
f"The behavior of {type(self).__name__}.{name} with axis=None "
11639-
"is deprecated, in a future version this will reduce over both "
11640-
"axes and return a scalar. To retain the old behavior, pass "
11641-
"axis=0 (or do not pass axis)",
11642-
FutureWarning,
11643-
stacklevel=find_stack_level(),
11644-
)
11645-
axis = 0
11646-
elif axis is lib.no_default:
11647-
axis = 0
11648-
1164911621
return self._reduce(
1165011622
func,
1165111623
name=name,

pandas/tests/frame/test_npfuncs.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,16 @@ def test_np_sqrt(self, float_frame):
2727

2828
tm.assert_frame_equal(result, float_frame.apply(np.sqrt))
2929

30-
def test_sum_deprecated_axis_behavior(self):
31-
# GH#52042 deprecated behavior of df.sum(axis=None), which gets
30+
def test_sum_axis_behavior(self):
31+
# GH#52042 df.sum(axis=None) now reduces over both axes, which gets
3232
# called when we do np.sum(df)
3333

3434
arr = np.random.default_rng(2).standard_normal((4, 3))
3535
df = DataFrame(arr)
3636

37-
msg = "The behavior of DataFrame.sum with axis=None is deprecated"
38-
with tm.assert_produces_warning(
39-
FutureWarning, match=msg, check_stacklevel=False
40-
):
41-
res = np.sum(df)
42-
43-
with tm.assert_produces_warning(FutureWarning, match=msg):
44-
expected = df.sum(axis=None)
45-
tm.assert_series_equal(res, expected)
37+
res = np.sum(df)
38+
expected = df.to_numpy().sum(axis=None)
39+
assert res == expected
4640

4741
def test_np_ravel(self):
4842
# GH26247

pandas/tests/groupby/aggregate/test_aggregate.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ def test_agg_apply_corner(ts, tsframe):
132132
tm.assert_frame_equal(grouped.sum(), exp_df)
133133
tm.assert_frame_equal(grouped.agg("sum"), exp_df)
134134

135-
msg = "The behavior of DataFrame.sum with axis=None is deprecated"
136-
with tm.assert_produces_warning(FutureWarning, match=msg, check_stacklevel=False):
137-
res = grouped.apply(np.sum)
135+
res = grouped.apply(np.sum, axis=0)
138136
tm.assert_frame_equal(res, exp_df)
139137

140138

pandas/tests/groupby/test_raises.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,7 @@ def test_groupby_raises_string_np(
222222
"Could not convert string .* to numeric",
223223
),
224224
}[groupby_func_np]
225-
if how == "transform" and groupby_func_np is np.sum and not groupby_series:
226-
warn_msg = "The behavior of DataFrame.sum with axis=None is deprecated"
227-
else:
228-
warn_msg = ""
229-
_call_and_check(klass, msg, how, gb, groupby_func_np, (), warn_msg)
225+
_call_and_check(klass, msg, how, gb, groupby_func_np, ())
230226

231227

232228
@pytest.mark.parametrize("how", ["method", "agg", "transform"])

pandas/tests/window/test_expanding.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def test_expanding_corr_pairwise(frame):
310310
@pytest.mark.parametrize(
311311
"func,static_comp",
312312
[
313-
("sum", np.sum),
313+
("sum", lambda x: np.sum(x, axis=0)),
314314
("mean", lambda x: np.mean(x, axis=0)),
315315
("max", lambda x: np.max(x, axis=0)),
316316
("min", lambda x: np.min(x, axis=0)),
@@ -324,12 +324,7 @@ def test_expanding_func(func, static_comp, frame_or_series):
324324
result = getattr(obj, func)()
325325
assert isinstance(result, frame_or_series)
326326

327-
msg = "The behavior of DataFrame.sum with axis=None is deprecated"
328-
warn = None
329-
if frame_or_series is DataFrame and static_comp is np.sum:
330-
warn = FutureWarning
331-
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
332-
expected = static_comp(data[:11])
327+
expected = static_comp(data[:11])
333328
if frame_or_series is Series:
334329
tm.assert_almost_equal(result[10], expected)
335330
else:

0 commit comments

Comments
 (0)