diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f5681142a0048..cb3bd6d7ef183 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4818,6 +4818,8 @@ def apply(self, func, axis=0, broadcast=False, raw=False, reduce=None, while guessing, exceptions raised by func will be ignored). If reduce is True a Series will always be returned, and if False a DataFrame will always be returned. + + .. deprecated:: 0.22.0 args : tuple Positional arguments to pass to function in addition to the array/series @@ -4850,6 +4852,10 @@ def apply(self, func, axis=0, broadcast=False, raw=False, reduce=None, axis = self._get_axis_number(axis) ignore_failures = kwds.pop('ignore_failures', False) + if reduce is not None: + warnings.warn("the reduce keyword is deprecated", + FutureWarning, stacklevel=7) + # dispatch to agg if axis == 0 and isinstance(func, (list, dict)): return self.aggregate(func, axis=axis, *args, **kwds) @@ -5760,8 +5766,10 @@ def f(x): # numeric_only and yet we have tried a # column-by-column reduction, where we have mixed type. # So let's just do what we can - result = self.apply(f, reduce=False, - ignore_failures=True) + result = self._apply_standard(f, + axis=axis, + reduce=False, + ignore_failures=True) if result.ndim == self.ndim: result = result.iloc[0] return result diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 55f04bf189f0c..4867496123f99 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -82,22 +82,24 @@ def test_apply_empty(self): assert_frame_equal(xp, rs) # reduce with an empty DataFrame - x = [] - result = self.empty.apply(x.append, axis=1, reduce=False) - assert_frame_equal(result, self.empty) - result = self.empty.apply(x.append, axis=1, reduce=True) - assert_series_equal(result, Series( - [], index=pd.Index([], dtype=object))) - - empty_with_cols = DataFrame(columns=['a', 'b', 'c']) - result = empty_with_cols.apply(x.append, axis=1, reduce=False) - assert_frame_equal(result, empty_with_cols) - result = empty_with_cols.apply(x.append, axis=1, reduce=True) - assert_series_equal(result, Series( - [], index=pd.Index([], dtype=object))) - - # Ensure that x.append hasn't been called - assert x == [] + with tm.assert_produces_warning(FutureWarning, + check_stacklevel=False): + x = [] + result = self.empty.apply(x.append, axis=1, reduce=False) + assert_frame_equal(result, self.empty) + result = self.empty.apply(x.append, axis=1, reduce=True) + assert_series_equal(result, Series( + [], index=pd.Index([], dtype=object))) + + empty_with_cols = DataFrame(columns=['a', 'b', 'c']) + result = empty_with_cols.apply(x.append, axis=1, reduce=False) + assert_frame_equal(result, empty_with_cols) + result = empty_with_cols.apply(x.append, axis=1, reduce=True) + assert_series_equal(result, Series( + [], index=pd.Index([], dtype=object))) + + # Ensure that x.append hasn't been called + assert x == [] def test_apply_standard_nonunique(self): df = DataFrame( @@ -371,14 +373,16 @@ def test_apply_dict(self): B_dicts = pd.Series([dict([(0, 0), (1, 2)]), dict([(0, 1), (1, 3)])]) fn = lambda x: x.to_dict() - for df, dicts in [(A, A_dicts), (B, B_dicts)]: - reduce_true = df.apply(fn, reduce=True) - reduce_false = df.apply(fn, reduce=False) - reduce_none = df.apply(fn, reduce=None) + with tm.assert_produces_warning(FutureWarning, + check_stacklevel=False): + for df, dicts in [(A, A_dicts), (B, B_dicts)]: + reduce_true = df.apply(fn, reduce=True) + reduce_false = df.apply(fn, reduce=False) + reduce_none = df.apply(fn, reduce=None) - assert_series_equal(reduce_true, dicts) - assert_frame_equal(reduce_false, df) - assert_series_equal(reduce_none, dicts) + assert_series_equal(reduce_true, dicts) + assert_frame_equal(reduce_false, df) + assert_series_equal(reduce_none, dicts) def test_applymap(self): applied = self.frame.applymap(lambda x: x * 2)