Skip to content

Commit f212352

Browse files
kerncSeeminSyed
authored andcommitted
BUG: Fix DataFrame.apply(..., raw=True) not calling with raw array (pandas-dev#32425)
1 parent 24a7b3e commit f212352

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ Reshaping
335335
- Bug in :func:`concat` where the resulting indices are not copied when ``copy=True`` (:issue:`29879`)
336336
- :meth:`Series.append` will now raise a ``TypeError`` when passed a DataFrame or a sequence containing Dataframe (:issue:`31413`)
337337
- :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`)
338+
- Bug in :meth:`DataFrame.apply` where callback was called with :class:`Series` parameter even though ``raw=True`` requested. (:issue:`32423`)
338339

339340

340341
Sparse

pandas/core/apply.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def get_result(self):
179179
return self.apply_empty_result()
180180

181181
# raw
182-
elif self.raw and not self.obj._is_mixed_type:
182+
elif self.raw:
183183
return self.apply_raw()
184184

185185
return self.apply_standard()

pandas/tests/frame/test_apply.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,14 @@ def test_apply_broadcast_error(self, int_frame_const_col):
235235
with pytest.raises(ValueError):
236236
df.apply(lambda x: Series([1, 2]), axis=1, result_type="broadcast")
237237

238-
def test_apply_raw(self, float_frame):
238+
def test_apply_raw(self, float_frame, mixed_type_frame):
239+
def _assert_raw(x):
240+
assert isinstance(x, np.ndarray)
241+
assert x.ndim == 1
242+
243+
float_frame.apply(_assert_raw, raw=True)
244+
float_frame.apply(_assert_raw, axis=1, raw=True)
245+
239246
result0 = float_frame.apply(np.mean, raw=True)
240247
result1 = float_frame.apply(np.mean, axis=1, raw=True)
241248

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

260+
# Mixed dtype (GH-32423)
261+
mixed_type_frame.apply(_assert_raw, raw=True)
262+
mixed_type_frame.apply(_assert_raw, axis=1, raw=True)
263+
253264
def test_apply_axis1(self, float_frame):
254265
d = float_frame.index[0]
255266
tapplied = float_frame.apply(np.mean, axis=1)

0 commit comments

Comments
 (0)