Skip to content

Commit 6126b85

Browse files
DEPR: keyword-only arguments for DataFrame/Series statistical methods (#58122)
* Enforce keyword-only arguments for dataframe/series statistical methods * Line not long anymore! * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Adjust test --------- Co-authored-by: Abdulaziz Aloqeely <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 583026b commit 6126b85

File tree

14 files changed

+61
-44
lines changed

14 files changed

+61
-44
lines changed

doc/source/user_guide/basics.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,15 @@ For example:
476476
.. ipython:: python
477477
478478
df
479-
df.mean(0)
480-
df.mean(1)
479+
df.mean(axis=0)
480+
df.mean(axis=1)
481481
482482
All such methods have a ``skipna`` option signaling whether to exclude missing
483483
data (``True`` by default):
484484

485485
.. ipython:: python
486486
487-
df.sum(0, skipna=False)
487+
df.sum(axis=0, skipna=False)
488488
df.sum(axis=1, skipna=True)
489489
490490
Combined with the broadcasting / arithmetic behavior, one can describe various
@@ -495,8 +495,8 @@ standard deviation of 1), very concisely:
495495
496496
ts_stand = (df - df.mean()) / df.std()
497497
ts_stand.std()
498-
xs_stand = df.sub(df.mean(1), axis=0).div(df.std(1), axis=0)
499-
xs_stand.std(1)
498+
xs_stand = df.sub(df.mean(axis=1), axis=0).div(df.std(axis=1), axis=0)
499+
xs_stand.std(axis=1)
500500
501501
Note that methods like :meth:`~DataFrame.cumsum` and :meth:`~DataFrame.cumprod`
502502
preserve the location of ``NaN`` values. This is somewhat different from

doc/source/user_guide/indexing.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ To select a row where each column meets its own criterion:
952952
953953
values = {'ids': ['a', 'b'], 'ids2': ['a', 'c'], 'vals': [1, 3]}
954954
955-
row_mask = df.isin(values).all(1)
955+
row_mask = df.isin(values).all(axis=1)
956956
957957
df[row_mask]
958958

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Other Deprecations
191191

192192
- Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`)
193193
- Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`)
194+
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.all`, :meth:`DataFrame.min`, :meth:`DataFrame.max`, :meth:`DataFrame.sum`, :meth:`DataFrame.prod`, :meth:`DataFrame.mean`, :meth:`DataFrame.median`, :meth:`DataFrame.sem`, :meth:`DataFrame.var`, :meth:`DataFrame.std`, :meth:`DataFrame.skew`, :meth:`DataFrame.kurt`, :meth:`Series.all`, :meth:`Series.min`, :meth:`Series.max`, :meth:`Series.sum`, :meth:`Series.prod`, :meth:`Series.mean`, :meth:`Series.median`, :meth:`Series.sem`, :meth:`Series.var`, :meth:`Series.std`, :meth:`Series.skew`, and :meth:`Series.kurt`. (:issue:`57087`)
194195
- Deprecated allowing non-keyword arguments in :meth:`Series.to_markdown` except ``buf``. (:issue:`57280`)
195196
- Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`)
196197
-

pandas/core/frame.py

+13
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
from pandas.util._decorators import (
6565
Appender,
6666
Substitution,
67+
deprecate_nonkeyword_arguments,
6768
doc,
6869
set_module,
6970
)
@@ -11543,6 +11544,7 @@ def all(
1154311544
**kwargs,
1154411545
) -> Series | bool: ...
1154511546

11547+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="all")
1154611548
@doc(make_doc("all", ndim=1))
1154711549
def all(
1154811550
self,
@@ -11589,6 +11591,7 @@ def min(
1158911591
**kwargs,
1159011592
) -> Series | Any: ...
1159111593

11594+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="min")
1159211595
@doc(make_doc("min", ndim=2))
1159311596
def min(
1159411597
self,
@@ -11635,6 +11638,7 @@ def max(
1163511638
**kwargs,
1163611639
) -> Series | Any: ...
1163711640

11641+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="max")
1163811642
@doc(make_doc("max", ndim=2))
1163911643
def max(
1164011644
self,
@@ -11650,6 +11654,7 @@ def max(
1165011654
result = result.__finalize__(self, method="max")
1165111655
return result
1165211656

11657+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="sum")
1165311658
@doc(make_doc("sum", ndim=2))
1165411659
def sum(
1165511660
self,
@@ -11670,6 +11675,7 @@ def sum(
1167011675
result = result.__finalize__(self, method="sum")
1167111676
return result
1167211677

11678+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="prod")
1167311679
@doc(make_doc("prod", ndim=2))
1167411680
def prod(
1167511681
self,
@@ -11721,6 +11727,7 @@ def mean(
1172111727
**kwargs,
1172211728
) -> Series | Any: ...
1172311729

11730+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="mean")
1172411731
@doc(make_doc("mean", ndim=2))
1172511732
def mean(
1172611733
self,
@@ -11767,6 +11774,7 @@ def median(
1176711774
**kwargs,
1176811775
) -> Series | Any: ...
1176911776

11777+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="median")
1177011778
@doc(make_doc("median", ndim=2))
1177111779
def median(
1177211780
self,
@@ -11816,6 +11824,7 @@ def sem(
1181611824
**kwargs,
1181711825
) -> Series | Any: ...
1181811826

11827+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="sem")
1181911828
@doc(make_doc("sem", ndim=2))
1182011829
def sem(
1182111830
self,
@@ -11866,6 +11875,7 @@ def var(
1186611875
**kwargs,
1186711876
) -> Series | Any: ...
1186811877

11878+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="var")
1186911879
@doc(make_doc("var", ndim=2))
1187011880
def var(
1187111881
self,
@@ -11916,6 +11926,7 @@ def std(
1191611926
**kwargs,
1191711927
) -> Series | Any: ...
1191811928

11929+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="std")
1191911930
@doc(make_doc("std", ndim=2))
1192011931
def std(
1192111932
self,
@@ -11963,6 +11974,7 @@ def skew(
1196311974
**kwargs,
1196411975
) -> Series | Any: ...
1196511976

11977+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="skew")
1196611978
@doc(make_doc("skew", ndim=2))
1196711979
def skew(
1196811980
self,
@@ -12009,6 +12021,7 @@ def kurt(
1200912021
**kwargs,
1201012022
) -> Series | Any: ...
1201112023

12024+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="kurt")
1201212025
@doc(make_doc("kurt", ndim=2))
1201312026
def kurt(
1201412027
self,

pandas/core/series.py

+12
Original file line numberDiff line numberDiff line change
@@ -6187,6 +6187,7 @@ def any( # type: ignore[override]
61876187
filter_type="bool",
61886188
)
61896189

6190+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="all")
61906191
@Appender(make_doc("all", ndim=1))
61916192
def all(
61926193
self,
@@ -6206,6 +6207,7 @@ def all(
62066207
filter_type="bool",
62076208
)
62086209

6210+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="min")
62096211
@doc(make_doc("min", ndim=1))
62106212
def min(
62116213
self,
@@ -6218,6 +6220,7 @@ def min(
62186220
self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs
62196221
)
62206222

6223+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="max")
62216224
@doc(make_doc("max", ndim=1))
62226225
def max(
62236226
self,
@@ -6230,6 +6233,7 @@ def max(
62306233
self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs
62316234
)
62326235

6236+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="sum")
62336237
@doc(make_doc("sum", ndim=1))
62346238
def sum(
62356239
self,
@@ -6248,6 +6252,7 @@ def sum(
62486252
**kwargs,
62496253
)
62506254

6255+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="prod")
62516256
@doc(make_doc("prod", ndim=1))
62526257
def prod(
62536258
self,
@@ -6266,6 +6271,7 @@ def prod(
62666271
**kwargs,
62676272
)
62686273

6274+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="mean")
62696275
@doc(make_doc("mean", ndim=1))
62706276
def mean(
62716277
self,
@@ -6278,6 +6284,7 @@ def mean(
62786284
self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs
62796285
)
62806286

6287+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="median")
62816288
@doc(make_doc("median", ndim=1))
62826289
def median(
62836290
self,
@@ -6290,6 +6297,7 @@ def median(
62906297
self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs
62916298
)
62926299

6300+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="sem")
62936301
@doc(make_doc("sem", ndim=1))
62946302
def sem(
62956303
self,
@@ -6308,6 +6316,7 @@ def sem(
63086316
**kwargs,
63096317
)
63106318

6319+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="var")
63116320
@doc(make_doc("var", ndim=1))
63126321
def var(
63136322
self,
@@ -6326,6 +6335,7 @@ def var(
63266335
**kwargs,
63276336
)
63286337

6338+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="std")
63296339
@doc(make_doc("std", ndim=1))
63306340
def std(
63316341
self,
@@ -6344,6 +6354,7 @@ def std(
63446354
**kwargs,
63456355
)
63466356

6357+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="skew")
63476358
@doc(make_doc("skew", ndim=1))
63486359
def skew(
63496360
self,
@@ -6356,6 +6367,7 @@ def skew(
63566367
self, axis=axis, skipna=skipna, numeric_only=numeric_only, **kwargs
63576368
)
63586369

6370+
@deprecate_nonkeyword_arguments(version="3.0", allowed_args=["self"], name="kurt")
63596371
@doc(make_doc("kurt", ndim=1))
63606372
def kurt(
63616373
self,

pandas/tests/apply/test_frame_apply.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ def test_apply_yield_list(float_frame):
402402

403403
def test_apply_reduce_Series(float_frame):
404404
float_frame.iloc[::2, float_frame.columns.get_loc("A")] = np.nan
405-
expected = float_frame.mean(1)
405+
expected = float_frame.mean(axis=1)
406406
result = float_frame.apply(np.mean, axis=1)
407407
tm.assert_series_equal(result, expected)
408408

pandas/tests/apply/test_str.py

+8-17
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,18 @@
1919

2020
@pytest.mark.parametrize("func", ["sum", "mean", "min", "max", "std"])
2121
@pytest.mark.parametrize(
22-
"args,kwds",
22+
"kwds",
2323
[
24-
pytest.param([], {}, id="no_args_or_kwds"),
25-
pytest.param([1], {}, id="axis_from_args"),
26-
pytest.param([], {"axis": 1}, id="axis_from_kwds"),
27-
pytest.param([], {"numeric_only": True}, id="optional_kwds"),
28-
pytest.param([1, True], {"numeric_only": True}, id="args_and_kwds"),
24+
pytest.param({}, id="no_kwds"),
25+
pytest.param({"axis": 1}, id="on_axis"),
26+
pytest.param({"numeric_only": True}, id="func_kwds"),
27+
pytest.param({"axis": 1, "numeric_only": True}, id="axis_and_func_kwds"),
2928
],
3029
)
3130
@pytest.mark.parametrize("how", ["agg", "apply"])
32-
def test_apply_with_string_funcs(request, float_frame, func, args, kwds, how):
33-
if len(args) > 1 and how == "agg":
34-
request.applymarker(
35-
pytest.mark.xfail(
36-
raises=TypeError,
37-
reason="agg/apply signature mismatch - agg passes 2nd "
38-
"argument to func",
39-
)
40-
)
41-
result = getattr(float_frame, how)(func, *args, **kwds)
42-
expected = getattr(float_frame, func)(*args, **kwds)
31+
def test_apply_with_string_funcs(request, float_frame, func, kwds, how):
32+
result = getattr(float_frame, how)(func, **kwds)
33+
expected = getattr(float_frame, func)(**kwds)
4334
tm.assert_series_equal(result, expected)
4435

4536

pandas/tests/frame/methods/test_asof.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ def test_basic(self, date_range_frame):
3636
dates = date_range("1/1/1990", periods=N * 3, freq="25s")
3737

3838
result = df.asof(dates)
39-
assert result.notna().all(1).all()
39+
assert result.notna().all(axis=1).all()
4040
lb = df.index[14]
4141
ub = df.index[30]
4242

4343
dates = list(dates)
4444

4545
result = df.asof(dates)
46-
assert result.notna().all(1).all()
46+
assert result.notna().all(axis=1).all()
4747

4848
mask = (result.index >= lb) & (result.index < ub)
4949
rs = result[mask]
50-
assert (rs == 14).all(1).all()
50+
assert (rs == 14).all(axis=1).all()
5151

5252
def test_subset(self, date_range_frame):
5353
N = 10

pandas/tests/frame/methods/test_fillna.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def test_fillna_dict_series(self):
466466

467467
# disable this for now
468468
with pytest.raises(NotImplementedError, match="column by column"):
469-
df.fillna(df.max(1), axis=1)
469+
df.fillna(df.max(axis=1), axis=1)
470470

471471
def test_fillna_dataframe(self):
472472
# GH#8377

pandas/tests/frame/test_reductions.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ def test_operators_timedelta64(self):
773773
tm.assert_series_equal(result, expected)
774774

775775
# works when only those columns are selected
776-
result = mixed[["A", "B"]].min(1)
776+
result = mixed[["A", "B"]].min(axis=1)
777777
expected = Series([timedelta(days=-1)] * 3)
778778
tm.assert_series_equal(result, expected)
779779

@@ -832,8 +832,8 @@ def test_std_datetime64_with_nat(self, values, skipna, request, unit):
832832
def test_sum_corner(self):
833833
empty_frame = DataFrame()
834834

835-
axis0 = empty_frame.sum(0)
836-
axis1 = empty_frame.sum(1)
835+
axis0 = empty_frame.sum(axis=0)
836+
axis1 = empty_frame.sum(axis=1)
837837
assert isinstance(axis0, Series)
838838
assert isinstance(axis1, Series)
839839
assert len(axis0) == 0
@@ -967,8 +967,8 @@ def test_sum_object(self, float_frame):
967967
def test_sum_bool(self, float_frame):
968968
# ensure this works, bug report
969969
bools = np.isnan(float_frame)
970-
bools.sum(1)
971-
bools.sum(0)
970+
bools.sum(axis=1)
971+
bools.sum(axis=0)
972972

973973
def test_sum_mixed_datetime(self):
974974
# GH#30886
@@ -990,7 +990,7 @@ def test_mean_corner(self, float_frame, float_string_frame):
990990

991991
# take mean of boolean column
992992
float_frame["bool"] = float_frame["A"] > 0
993-
means = float_frame.mean(0)
993+
means = float_frame.mean(axis=0)
994994
assert means["bool"] == float_frame["bool"].values.mean()
995995

996996
def test_mean_datetimelike(self):
@@ -1043,13 +1043,13 @@ def test_mean_extensionarray_numeric_only_true(self):
10431043

10441044
def test_stats_mixed_type(self, float_string_frame):
10451045
with pytest.raises(TypeError, match="could not convert"):
1046-
float_string_frame.std(1)
1046+
float_string_frame.std(axis=1)
10471047
with pytest.raises(TypeError, match="could not convert"):
1048-
float_string_frame.var(1)
1048+
float_string_frame.var(axis=1)
10491049
with pytest.raises(TypeError, match="unsupported operand type"):
1050-
float_string_frame.mean(1)
1050+
float_string_frame.mean(axis=1)
10511051
with pytest.raises(TypeError, match="could not convert"):
1052-
float_string_frame.skew(1)
1052+
float_string_frame.skew(axis=1)
10531053

10541054
def test_sum_bools(self):
10551055
df = DataFrame(index=range(1), columns=range(10))
@@ -1331,11 +1331,11 @@ def test_any_all_extra(self):
13311331
result = df[["A", "B"]].any(axis=1, bool_only=True)
13321332
tm.assert_series_equal(result, expected)
13331333

1334-
result = df.all(1)
1334+
result = df.all(axis=1)
13351335
expected = Series([True, False, False], index=["a", "b", "c"])
13361336
tm.assert_series_equal(result, expected)
13371337

1338-
result = df.all(1, bool_only=True)
1338+
result = df.all(axis=1, bool_only=True)
13391339
tm.assert_series_equal(result, expected)
13401340

13411341
# Axis is None

0 commit comments

Comments
 (0)