Skip to content

Commit

Permalink
DEPR .apply(reduce=)
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Dec 8, 2017
1 parent 2f9fc7c commit 7105339
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
12 changes: 10 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
50 changes: 27 additions & 23 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7105339

Please sign in to comment.