Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix DataFrame.apply(..., raw=True) not calling with raw array #32425

Merged
merged 4 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ Reshaping
- :meth:`DataFrame.pivot` can now take lists for ``index`` and ``columns`` arguments (:issue:`21425`)
- Bug in :func:`concat` where the resulting indices are not copied when ``copy=True`` (:issue:`29879`)
- :meth:`DataFrame.replace` and :meth:`Series.replace` will raise a ``TypeError`` if ``to_replace`` is not an expected type. Previously the ``replace`` would fail silently (:issue:`18634`)
- Bug in :meth:`DataFrame.apply` where callback was called with :class:`Series` parameter even though ``raw=True`` requested. (:issue:`32423`)


Sparse
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def get_result(self):
return self.apply_empty_result()

# raw
elif self.raw and not self.obj._is_mixed_type:
elif self.raw:
return self.apply_raw()

return self.apply_standard()
Expand Down
13 changes: 12 additions & 1 deletion pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,14 @@ def test_apply_broadcast_error(self, int_frame_const_col):
with pytest.raises(ValueError):
df.apply(lambda x: Series([1, 2]), axis=1, result_type="broadcast")

def test_apply_raw(self, float_frame):
def test_apply_raw(self, float_frame, mixed_type_frame):
kernc marked this conversation as resolved.
Show resolved Hide resolved
def _assert_raw(x):
assert isinstance(x, np.ndarray)
assert x.ndim == 1

float_frame.apply(_assert_raw, raw=True)
float_frame.apply(_assert_raw, axis=1, raw=True)

result0 = float_frame.apply(np.mean, raw=True)
result1 = float_frame.apply(np.mean, axis=1, raw=True)

Expand All @@ -250,6 +257,10 @@ def test_apply_raw(self, float_frame):
expected = float_frame * 2
tm.assert_frame_equal(result, expected)

# Mixed dtype (GH-32423)
mixed_type_frame.apply(_assert_raw, raw=True)
mixed_type_frame.apply(_assert_raw, axis=1, raw=True)

def test_apply_axis1(self, float_frame):
d = float_frame.index[0]
tapplied = float_frame.apply(np.mean, axis=1)
Expand Down