Skip to content

Commit da3d0d9

Browse files
jbrockmendelWillAyd
authored andcommitted
DEPR: remove previously-deprecated broadcast/reduce kwargs from DataFrame.apply (pandas-dev#29017)
1 parent 9486f04 commit da3d0d9

File tree

4 files changed

+4
-94
lines changed

4 files changed

+4
-94
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ Removal of prior version deprecations/changes
227227
- Removed the previously deprecated :meth:`ExtensionArray._formatting_values`. Use :attr:`ExtensionArray._formatter` instead. (:issue:`23601`)
228228
- Removed the previously deprecated ``IntervalIndex.from_intervals`` in favor of the :class:`IntervalIndex` constructor (:issue:`19263`)
229229
- Ability to read pickles containing :class:`Categorical` instances created with pre-0.16 version of pandas has been removed (:issue:`27538`)
230+
- Removed the previously deprecated ``reduce`` and ``broadcast`` arguments from :meth:`DataFrame.apply` (:issue:`18577`)
230231
-
231232

232233
.. _whatsnew_1000.performance:

pandas/core/apply.py

+1-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import inspect
2-
import warnings
32

43
import numpy as np
54

@@ -21,9 +20,7 @@ def frame_apply(
2120
obj,
2221
func,
2322
axis=0,
24-
broadcast=None,
2523
raw=False,
26-
reduce=None,
2724
result_type=None,
2825
ignore_failures=False,
2926
args=None,
@@ -40,9 +37,7 @@ def frame_apply(
4037
return klass(
4138
obj,
4239
func,
43-
broadcast=broadcast,
4440
raw=raw,
45-
reduce=reduce,
4641
result_type=result_type,
4742
ignore_failures=ignore_failures,
4843
args=args,
@@ -51,18 +46,7 @@ def frame_apply(
5146

5247

5348
class FrameApply:
54-
def __init__(
55-
self,
56-
obj,
57-
func,
58-
broadcast,
59-
raw,
60-
reduce,
61-
result_type,
62-
ignore_failures,
63-
args,
64-
kwds,
65-
):
49+
def __init__(self, obj, func, raw, result_type, ignore_failures, args, kwds):
6650
self.obj = obj
6751
self.raw = raw
6852
self.ignore_failures = ignore_failures
@@ -75,34 +59,6 @@ def __init__(
7559
"of {None, 'reduce', 'broadcast', 'expand'}"
7660
)
7761

78-
if broadcast is not None:
79-
warnings.warn(
80-
"The broadcast argument is deprecated and will "
81-
"be removed in a future version. You can specify "
82-
"result_type='broadcast' to broadcast the result "
83-
"to the original dimensions",
84-
FutureWarning,
85-
stacklevel=4,
86-
)
87-
if broadcast:
88-
result_type = "broadcast"
89-
90-
if reduce is not None:
91-
warnings.warn(
92-
"The reduce argument is deprecated and will "
93-
"be removed in a future version. You can specify "
94-
"result_type='reduce' to try to reduce the result "
95-
"to the original dimensions",
96-
FutureWarning,
97-
stacklevel=4,
98-
)
99-
if reduce:
100-
101-
if result_type is not None:
102-
raise ValueError("cannot pass both reduce=True and result_type")
103-
104-
result_type = "reduce"
105-
10662
self.result_type = result_type
10763

10864
# curry if needed

pandas/core/frame.py

+2-38
Original file line numberDiff line numberDiff line change
@@ -6648,15 +6648,7 @@ def transform(self, func, axis=0, *args, **kwargs):
66486648
return super().transform(func, *args, **kwargs)
66496649

66506650
def apply(
6651-
self,
6652-
func,
6653-
axis=0,
6654-
broadcast=None,
6655-
raw=False,
6656-
reduce=None,
6657-
result_type=None,
6658-
args=(),
6659-
**kwds
6651+
self, func, axis=0, raw=False, reduce=None, result_type=None, args=(), **kwds
66606652
):
66616653
"""
66626654
Apply a function along an axis of the DataFrame.
@@ -6676,42 +6668,16 @@ def apply(
66766668
66776669
* 0 or 'index': apply function to each column.
66786670
* 1 or 'columns': apply function to each row.
6679-
broadcast : bool, optional
6680-
Only relevant for aggregation functions:
6681-
6682-
* ``False`` or ``None`` : returns a Series whose length is the
6683-
length of the index or the number of columns (based on the
6684-
`axis` parameter)
6685-
* ``True`` : results will be broadcast to the original shape
6686-
of the frame, the original index and columns will be retained.
6687-
6688-
.. deprecated:: 0.23.0
6689-
This argument will be removed in a future version, replaced
6690-
by result_type='broadcast'.
66916671
66926672
raw : bool, default False
6693-
Determines if row or column is passed as a Series or ndarry object:
6673+
Determines if row or column is passed as a Series or ndarray object:
66946674
66956675
* ``False`` : passes each row or column as a Series to the
66966676
function.
66976677
* ``True`` : the passed function will receive ndarray objects
66986678
instead.
66996679
If you are just applying a NumPy reduction function this will
67006680
achieve much better performance.
6701-
reduce : bool or None, default None
6702-
Try to apply reduction procedures. If the DataFrame is empty,
6703-
`apply` will use `reduce` to determine whether the result
6704-
should be a Series or a DataFrame. If ``reduce=None`` (the
6705-
default), `apply`'s return value will be guessed by calling
6706-
`func` on an empty Series
6707-
(note: while guessing, exceptions raised by `func` will be
6708-
ignored).
6709-
If ``reduce=True`` a Series will always be returned, and if
6710-
``reduce=False`` a DataFrame will always be returned.
6711-
6712-
.. deprecated:: 0.23.0
6713-
This argument will be removed in a future version, replaced
6714-
by ``result_type='reduce'``.
67156681
67166682
result_type : {'expand', 'reduce', 'broadcast', None}, default None
67176683
These only act when ``axis=1`` (columns):
@@ -6825,9 +6791,7 @@ def apply(
68256791
self,
68266792
func=func,
68276793
axis=axis,
6828-
broadcast=broadcast,
68296794
raw=raw,
6830-
reduce=reduce,
68316795
result_type=result_type,
68326796
args=args,
68336797
kwds=kwds,

pandas/tests/frame/test_apply.py

-11
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,6 @@ def test_nunique_empty(self):
137137
expected = Series([], index=pd.Index([]))
138138
assert_series_equal(result, expected)
139139

140-
def test_apply_deprecate_reduce(self):
141-
empty_frame = DataFrame()
142-
143-
x = []
144-
with tm.assert_produces_warning(FutureWarning):
145-
empty_frame.apply(x.append, axis=1, reduce=True)
146-
147140
def test_apply_standard_nonunique(self):
148141
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "a", "c"])
149142

@@ -170,10 +163,6 @@ def test_apply_with_string_funcs(self, float_frame, func, args, kwds):
170163
expected = getattr(float_frame, func)(*args, **kwds)
171164
tm.assert_series_equal(result, expected)
172165

173-
def test_apply_broadcast_deprecated(self, float_frame):
174-
with tm.assert_produces_warning(FutureWarning):
175-
float_frame.apply(np.mean, broadcast=True)
176-
177166
def test_apply_broadcast(self, float_frame, int_frame_const_col):
178167

179168
# scalars

0 commit comments

Comments
 (0)