Skip to content

Commit 3fbe809

Browse files
rhshadrachpmhatre1
authored andcommitted
CLN: Enforce deprecation of Series.agg using Series.apply (pandas-dev#57332)
1 parent afb155b commit 3fbe809

File tree

4 files changed

+22
-57
lines changed

4 files changed

+22
-57
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Removal of prior version deprecations/changes
120120
- Removed ``pandas.value_counts``, use :meth:`Series.value_counts` instead (:issue:`53493`)
121121
- Removed ``read_gbq`` and ``DataFrame.to_gbq``. Use ``pandas_gbq.read_gbq`` and ``pandas_gbq.to_gbq`` instead https://pandas-gbq.readthedocs.io/en/latest/api.html (:issue:`55525`)
122122
- Removed deprecated argument ``obj`` in :meth:`.DataFrameGroupBy.get_group` and :meth:`.SeriesGroupBy.get_group` (:issue:`53545`)
123+
- Removed deprecated behavior of :meth:`Series.agg` using :meth:`Series.apply` (:issue:`53325`)
123124
- Removed the ``ArrayManager`` (:issue:`55043`)
124125
- Removed the ``fastpath`` argument from the :class:`Series` constructor (:issue:`55466`)
125126
- Removed the ``is_boolean``, ``is_integer``, ``is_floating``, ``holds_integer``, ``is_numeric``, ``is_categorical``, ``is_object``, and ``is_interval`` attributes of :class:`Index` (:issue:`50042`)

pandas/core/apply.py

+1-16
Original file line numberDiff line numberDiff line change
@@ -1430,22 +1430,7 @@ def agg(self):
14301430
func = self.func
14311431
# string, list-like, and dict-like are entirely handled in super
14321432
assert callable(func)
1433-
1434-
# GH53325: The setup below is just to keep current behavior while emitting a
1435-
# deprecation message. In the future this will all be replaced with a simple
1436-
# `result = f(self.obj, *self.args, **self.kwargs)`.
1437-
try:
1438-
result = obj.apply(func, args=self.args, **self.kwargs)
1439-
except (ValueError, AttributeError, TypeError):
1440-
result = func(obj, *self.args, **self.kwargs)
1441-
else:
1442-
msg = (
1443-
f"using {func} in {type(obj).__name__}.agg cannot aggregate and "
1444-
f"has been deprecated. Use {type(obj).__name__}.transform to "
1445-
f"keep behavior unchanged."
1446-
)
1447-
warnings.warn(msg, FutureWarning, stacklevel=find_stack_level())
1448-
1433+
result = func(obj, *self.args, **self.kwargs)
14491434
return result
14501435

14511436
def apply_empty_result(self) -> Series:

pandas/tests/apply/test_frame_apply.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1688,18 +1688,14 @@ def foo2(x, b=2, c=0):
16881688
expected = df + 7
16891689
tm.assert_frame_equal(result, expected)
16901690

1691-
msg = "using .+ in Series.agg cannot aggregate and"
1692-
1693-
with tm.assert_produces_warning(FutureWarning, match=msg):
1694-
result = df.agg([foo1, foo2], 0, 3, c=4)
1691+
result = df.agg([foo1, foo2], 0, 3, c=4)
16951692
expected = DataFrame(
16961693
[[8, 8], [9, 9], [10, 10]], columns=[["x", "x"], ["foo1", "foo2"]]
16971694
)
16981695
tm.assert_frame_equal(result, expected)
16991696

17001697
# TODO: the result below is wrong, should be fixed (GH53325)
1701-
with tm.assert_produces_warning(FutureWarning, match=msg):
1702-
result = df.agg({"x": foo1}, 0, 3, c=4)
1698+
result = df.agg({"x": foo1}, 0, 3, c=4)
17031699
expected = DataFrame([2, 3, 4], columns=["x"])
17041700
tm.assert_frame_equal(result, expected)
17051701

pandas/tests/apply/test_series_apply.py

+18-35
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,7 @@ def f(x, a=0, b=0, c=0):
104104
return x + a + 10 * b + 100 * c
105105

106106
s = Series([1, 2])
107-
msg = (
108-
"in Series.agg cannot aggregate and has been deprecated. "
109-
"Use Series.transform to keep behavior unchanged."
110-
)
111-
with tm.assert_produces_warning(FutureWarning, match=msg):
112-
result = s.agg(f, 0, *args, **kwargs)
107+
result = s.agg(f, 0, *args, **kwargs)
113108
expected = s + increment
114109
tm.assert_series_equal(result, expected)
115110

@@ -124,13 +119,9 @@ def foo1(x, a=1, c=0):
124119
def foo2(x, b=2, c=0):
125120
return x + b + c
126121

127-
msg = "using .+ in Series.agg cannot aggregate and"
128-
with tm.assert_produces_warning(FutureWarning, match=msg):
129-
s.agg(foo1, 0, 3, c=4)
130-
with tm.assert_produces_warning(FutureWarning, match=msg):
131-
s.agg([foo1, foo2], 0, 3, c=4)
132-
with tm.assert_produces_warning(FutureWarning, match=msg):
133-
s.agg({"a": foo1, "b": foo2}, 0, 3, c=4)
122+
s.agg(foo1, 0, 3, c=4)
123+
s.agg([foo1, foo2], 0, 3, c=4)
124+
s.agg({"a": foo1, "b": foo2}, 0, 3, c=4)
134125

135126

136127
def test_series_apply_map_box_timestamps(by_row):
@@ -410,34 +401,26 @@ def test_apply_map_evaluate_lambdas_the_same(string_series, func, by_row):
410401

411402
def test_agg_evaluate_lambdas(string_series):
412403
# GH53325
413-
# in the future, the result will be a Series class.
414-
415-
with tm.assert_produces_warning(FutureWarning):
416-
result = string_series.agg(lambda x: type(x))
417-
assert isinstance(result, Series) and len(result) == len(string_series)
404+
result = string_series.agg(lambda x: type(x))
405+
assert result is Series
418406

419-
with tm.assert_produces_warning(FutureWarning):
420-
result = string_series.agg(type)
421-
assert isinstance(result, Series) and len(result) == len(string_series)
407+
result = string_series.agg(type)
408+
assert result is Series
422409

423410

424411
@pytest.mark.parametrize("op_name", ["agg", "apply"])
425412
def test_with_nested_series(datetime_series, op_name):
426-
# GH 2316
413+
# GH 2316 & GH52123
427414
# .agg with a reducer and a transform, what to do
428-
msg = "cannot aggregate"
429-
warning = FutureWarning if op_name == "agg" else None
430-
with tm.assert_produces_warning(warning, match=msg):
431-
# GH52123
432-
result = getattr(datetime_series, op_name)(
433-
lambda x: Series([x, x**2], index=["x", "x^2"])
434-
)
435-
expected = DataFrame({"x": datetime_series, "x^2": datetime_series**2})
436-
tm.assert_frame_equal(result, expected)
437-
438-
with tm.assert_produces_warning(FutureWarning, match=msg):
439-
result = datetime_series.agg(lambda x: Series([x, x**2], index=["x", "x^2"]))
440-
tm.assert_frame_equal(result, expected)
415+
result = getattr(datetime_series, op_name)(
416+
lambda x: Series([x, x**2], index=["x", "x^2"])
417+
)
418+
if op_name == "apply":
419+
expected = DataFrame({"x": datetime_series, "x^2": datetime_series**2})
420+
tm.assert_frame_equal(result, expected)
421+
else:
422+
expected = Series([datetime_series, datetime_series**2], index=["x", "x^2"])
423+
tm.assert_series_equal(result, expected)
441424

442425

443426
def test_replicate_describe(string_series):

0 commit comments

Comments
 (0)