Skip to content

Commit 76c7274

Browse files
DEPR: DataFrameGroupBy.corrwith (#58732)
1 parent ff550e6 commit 76c7274

File tree

10 files changed

+111
-25
lines changed

10 files changed

+111
-25
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ starting with 3.0, so it can be safely removed from your code.
263263
Other Deprecations
264264
^^^^^^^^^^^^^^^^^^
265265

266+
- Deprecated :meth:`.DataFrameGroupby.corrwith` (:issue:`57158`)
266267
- Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`)
267268
- Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`)
268269
- 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`)

pandas/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def pytest_collection_modifyitems(items, config) -> None:
150150
("is_categorical_dtype", "is_categorical_dtype is deprecated"),
151151
("is_sparse", "is_sparse is deprecated"),
152152
("DataFrameGroupBy.fillna", "DataFrameGroupBy.fillna is deprecated"),
153+
("DataFrameGroupBy.corrwith", "DataFrameGroupBy.corrwith is deprecated"),
153154
("NDFrame.replace", "Series.replace without 'value'"),
154155
("NDFrame.clip", "Downcasting behavior in Series and DataFrame methods"),
155156
("Series.idxmin", "The behavior of Series.idxmin"),

pandas/core/groupby/generic.py

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
Union,
2222
cast,
2323
)
24+
import warnings
2425

2526
import numpy as np
2627

@@ -32,6 +33,7 @@
3233
Substitution,
3334
doc,
3435
)
36+
from pandas.util._exceptions import find_stack_level
3537

3638
from pandas.core.dtypes.common import (
3739
ensure_int64,
@@ -2726,6 +2728,8 @@ def corrwith(
27262728
"""
27272729
Compute pairwise correlation.
27282730
2731+
.. deprecated:: 3.0.0
2732+
27292733
Pairwise correlation is computed between rows or columns of
27302734
DataFrame with rows or columns of Series or DataFrame. DataFrames
27312735
are first aligned along both axes before computing the
@@ -2785,6 +2789,11 @@ def corrwith(
27852789
2 0.755929 NaN
27862790
3 0.576557 NaN
27872791
"""
2792+
warnings.warn(
2793+
"DataFrameGroupBy.corrwith is deprecated",
2794+
FutureWarning,
2795+
stacklevel=find_stack_level(),
2796+
)
27882797
result = self._op_via_apply(
27892798
"corrwith",
27902799
other=other,

pandas/tests/groupby/test_all_methods.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ def test_multiindex_group_all_columns_when_empty(groupby_func):
2525
gb = df.groupby(["a", "b", "c"], group_keys=False)
2626
method = getattr(gb, groupby_func)
2727
args = get_groupby_method_args(groupby_func, df)
28-
29-
warn = FutureWarning if groupby_func == "fillna" else None
30-
warn_msg = "DataFrameGroupBy.fillna is deprecated"
28+
if groupby_func == "corrwith":
29+
warn = FutureWarning
30+
warn_msg = "DataFrameGroupBy.corrwith is deprecated"
31+
else:
32+
warn = None
33+
warn_msg = ""
3134
with tm.assert_produces_warning(warn, match=warn_msg):
3235
result = method(*args).index
3336
expected = df.index
@@ -42,18 +45,12 @@ def test_duplicate_columns(request, groupby_func, as_index):
4245
df = DataFrame([[1, 3, 6], [1, 4, 7], [2, 5, 8]], columns=list("abb"))
4346
args = get_groupby_method_args(groupby_func, df)
4447
gb = df.groupby("a", as_index=as_index)
45-
warn = FutureWarning if groupby_func == "fillna" else None
46-
warn_msg = "DataFrameGroupBy.fillna is deprecated"
47-
with tm.assert_produces_warning(warn, match=warn_msg):
48-
result = getattr(gb, groupby_func)(*args)
48+
result = getattr(gb, groupby_func)(*args)
4949

5050
expected_df = df.set_axis(["a", "b", "c"], axis=1)
5151
expected_args = get_groupby_method_args(groupby_func, expected_df)
5252
expected_gb = expected_df.groupby("a", as_index=as_index)
53-
warn = FutureWarning if groupby_func == "fillna" else None
54-
warn_msg = "DataFrameGroupBy.fillna is deprecated"
55-
with tm.assert_produces_warning(warn, match=warn_msg):
56-
expected = getattr(expected_gb, groupby_func)(*expected_args)
53+
expected = getattr(expected_gb, groupby_func)(*expected_args)
5754
if groupby_func not in ("size", "ngroup", "cumcount"):
5855
expected = expected.rename(columns={"c": "b"})
5956
tm.assert_equal(result, expected)
@@ -74,8 +71,12 @@ def test_dup_labels_output_shape(groupby_func, idx):
7471
grp_by = df.groupby([0])
7572

7673
args = get_groupby_method_args(groupby_func, df)
77-
warn = FutureWarning if groupby_func == "fillna" else None
78-
warn_msg = "DataFrameGroupBy.fillna is deprecated"
74+
if groupby_func == "corrwith":
75+
warn = FutureWarning
76+
warn_msg = "DataFrameGroupBy.corrwith is deprecated"
77+
else:
78+
warn = None
79+
warn_msg = ""
7980
with tm.assert_produces_warning(warn, match=warn_msg):
8081
result = getattr(grp_by, groupby_func)(*args)
8182

pandas/tests/groupby/test_apply.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,14 @@ def test_apply_is_unchanged_when_other_methods_are_called_first(reduction_func):
11971197
# Check output when another method is called before .apply()
11981198
grp = df.groupby(by="a")
11991199
args = get_groupby_method_args(reduction_func, df)
1200-
_ = getattr(grp, reduction_func)(*args)
1200+
if reduction_func == "corrwith":
1201+
warn = FutureWarning
1202+
msg = "DataFrameGroupBy.corrwith is deprecated"
1203+
else:
1204+
warn = None
1205+
msg = ""
1206+
with tm.assert_produces_warning(warn, match=msg):
1207+
_ = getattr(grp, reduction_func)(*args)
12011208
result = grp.apply(np.sum, axis=0, include_groups=False)
12021209
tm.assert_frame_equal(result, expected)
12031210

pandas/tests/groupby/test_categorical.py

+24-4
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,14 @@ def test_dataframe_groupby_on_2_categoricals_when_observed_is_true(reduction_fun
14731473
df_grp = df.groupby(["cat_1", "cat_2"], observed=True)
14741474

14751475
args = get_groupby_method_args(reduction_func, df)
1476-
res = getattr(df_grp, reduction_func)(*args)
1476+
if reduction_func == "corrwith":
1477+
warn = FutureWarning
1478+
warn_msg = "DataFrameGroupBy.corrwith is deprecated"
1479+
else:
1480+
warn = None
1481+
warn_msg = ""
1482+
with tm.assert_produces_warning(warn, match=warn_msg):
1483+
res = getattr(df_grp, reduction_func)(*args)
14771484

14781485
for cat in unobserved_cats:
14791486
assert cat not in res.index
@@ -1512,7 +1519,14 @@ def test_dataframe_groupby_on_2_categoricals_when_observed_is_false(
15121519
getattr(df_grp, reduction_func)(*args)
15131520
return
15141521

1515-
res = getattr(df_grp, reduction_func)(*args)
1522+
if reduction_func == "corrwith":
1523+
warn = FutureWarning
1524+
warn_msg = "DataFrameGroupBy.corrwith is deprecated"
1525+
else:
1526+
warn = None
1527+
warn_msg = ""
1528+
with tm.assert_produces_warning(warn, match=warn_msg):
1529+
res = getattr(df_grp, reduction_func)(*args)
15161530

15171531
expected = _results_for_groupbys_with_missing_categories[reduction_func]
15181532

@@ -1904,8 +1918,14 @@ def test_category_order_reducer(
19041918
):
19051919
getattr(gb, reduction_func)(*args)
19061920
return
1907-
1908-
op_result = getattr(gb, reduction_func)(*args)
1921+
if reduction_func == "corrwith":
1922+
warn = FutureWarning
1923+
warn_msg = "DataFrameGroupBy.corrwith is deprecated"
1924+
else:
1925+
warn = None
1926+
warn_msg = ""
1927+
with tm.assert_produces_warning(warn, match=warn_msg):
1928+
op_result = getattr(gb, reduction_func)(*args)
19091929
if as_index:
19101930
result = op_result.index.get_level_values("a").categories
19111931
else:

pandas/tests/groupby/test_groupby_dropna.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,14 @@ def test_categorical_reducers(reduction_func, observed, sort, as_index, index_ki
543543
return
544544

545545
gb_filled = df_filled.groupby(keys, observed=observed, sort=sort, as_index=True)
546-
expected = getattr(gb_filled, reduction_func)(*args_filled).reset_index()
546+
if reduction_func == "corrwith":
547+
warn = FutureWarning
548+
msg = "DataFrameGroupBy.corrwith is deprecated"
549+
else:
550+
warn = None
551+
msg = ""
552+
with tm.assert_produces_warning(warn, match=msg):
553+
expected = getattr(gb_filled, reduction_func)(*args_filled).reset_index()
547554
expected["x"] = expected["x"].cat.remove_categories([4])
548555
if index_kind == "multi":
549556
expected["x2"] = expected["x2"].cat.remove_categories([4])
@@ -567,7 +574,14 @@ def test_categorical_reducers(reduction_func, observed, sort, as_index, index_ki
567574
if as_index:
568575
expected = expected["size"].rename(None)
569576

570-
result = getattr(gb_keepna, reduction_func)(*args)
577+
if reduction_func == "corrwith":
578+
warn = FutureWarning
579+
msg = "DataFrameGroupBy.corrwith is deprecated"
580+
else:
581+
warn = None
582+
msg = ""
583+
with tm.assert_produces_warning(warn, match=msg):
584+
result = getattr(gb_keepna, reduction_func)(*args)
571585

572586
# size will return a Series, others are DataFrame
573587
tm.assert_equal(result, expected)

pandas/tests/groupby/test_numeric_only.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,14 @@ def test_numeric_only(kernel, has_arg, numeric_only, keys):
256256
method = getattr(gb, kernel)
257257
if has_arg and numeric_only is True:
258258
# Cases where b does not appear in the result
259-
result = method(*args, **kwargs)
259+
if kernel == "corrwith":
260+
warn = FutureWarning
261+
msg = "DataFrameGroupBy.corrwith is deprecated"
262+
else:
263+
warn = None
264+
msg = ""
265+
with tm.assert_produces_warning(warn, match=msg):
266+
result = method(*args, **kwargs)
260267
assert "b" not in result.columns
261268
elif (
262269
# kernels that work on any dtype and have numeric_only arg
@@ -296,7 +303,14 @@ def test_numeric_only(kernel, has_arg, numeric_only, keys):
296303
elif kernel == "idxmax":
297304
msg = "'>' not supported between instances of 'type' and 'type'"
298305
with pytest.raises(exception, match=msg):
299-
method(*args, **kwargs)
306+
if kernel == "corrwith":
307+
warn = FutureWarning
308+
msg = "DataFrameGroupBy.corrwith is deprecated"
309+
else:
310+
warn = None
311+
msg = ""
312+
with tm.assert_produces_warning(warn, match=msg):
313+
method(*args, **kwargs)
300314
elif not has_arg and numeric_only is not lib.no_default:
301315
with pytest.raises(
302316
TypeError, match="got an unexpected keyword argument 'numeric_only'"

pandas/tests/groupby/test_raises.py

+8
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ def test_groupby_raises_string(
183183
if groupby_func == "fillna":
184184
kind = "Series" if groupby_series else "DataFrame"
185185
warn_msg = f"{kind}GroupBy.fillna is deprecated"
186+
elif groupby_func == "corrwith":
187+
warn_msg = "DataFrameGroupBy.corrwith is deprecated"
186188
else:
187189
warn_msg = ""
188190
_call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg)
@@ -288,6 +290,8 @@ def test_groupby_raises_datetime(
288290
if groupby_func == "fillna":
289291
kind = "Series" if groupby_series else "DataFrame"
290292
warn_msg = f"{kind}GroupBy.fillna is deprecated"
293+
elif groupby_func == "corrwith":
294+
warn_msg = "DataFrameGroupBy.corrwith is deprecated"
291295
else:
292296
warn_msg = ""
293297
_call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg=warn_msg)
@@ -485,6 +489,8 @@ def test_groupby_raises_category(
485489
if groupby_func == "fillna":
486490
kind = "Series" if groupby_series else "DataFrame"
487491
warn_msg = f"{kind}GroupBy.fillna is deprecated"
492+
elif groupby_func == "corrwith":
493+
warn_msg = "DataFrameGroupBy.corrwith is deprecated"
488494
else:
489495
warn_msg = ""
490496
_call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg)
@@ -658,6 +664,8 @@ def test_groupby_raises_category_on_category(
658664
if groupby_func == "fillna":
659665
kind = "Series" if groupby_series else "DataFrame"
660666
warn_msg = f"{kind}GroupBy.fillna is deprecated"
667+
elif groupby_func == "corrwith":
668+
warn_msg = "DataFrameGroupBy.corrwith is deprecated"
661669
else:
662670
warn_msg = ""
663671
_call_and_check(klass, msg, how, gb, groupby_func, args, warn_msg)

pandas/tests/groupby/transform/test_transform.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,14 @@ def test_transform_agg_by_name(request, reduction_func, frame_or_series):
11041104
return
11051105

11061106
args = get_groupby_method_args(reduction_func, obj)
1107-
result = g.transform(func, *args)
1107+
if func == "corrwith":
1108+
warn = FutureWarning
1109+
msg = "DataFrameGroupBy.corrwith is deprecated"
1110+
else:
1111+
warn = None
1112+
msg = ""
1113+
with tm.assert_produces_warning(warn, match=msg):
1114+
result = g.transform(func, *args)
11081115

11091116
# this is the *definition* of a transformation
11101117
tm.assert_index_equal(result.index, obj.index)
@@ -1468,8 +1475,12 @@ def test_as_index_no_change(keys, df, groupby_func):
14681475
args = get_groupby_method_args(groupby_func, df)
14691476
gb_as_index_true = df.groupby(keys, as_index=True)
14701477
gb_as_index_false = df.groupby(keys, as_index=False)
1471-
warn = FutureWarning if groupby_func == "fillna" else None
1472-
msg = "DataFrameGroupBy.fillna is deprecated"
1478+
if groupby_func == "corrwith":
1479+
warn = FutureWarning
1480+
msg = "DataFrameGroupBy.corrwith is deprecated"
1481+
else:
1482+
warn = None
1483+
msg = ""
14731484
with tm.assert_produces_warning(warn, match=msg):
14741485
result = gb_as_index_true.transform(groupby_func, *args)
14751486
with tm.assert_produces_warning(warn, match=msg):

0 commit comments

Comments
 (0)