Skip to content

Backport PR #36610 on branch 1.1.x (REGR: DataFrame.apply() with raw option and func returning string) #36631

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Fixed regressions
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`)
- Fixed regression in :meth:`read_excel` with ``engine="odf"`` caused ``UnboundLocalError`` in some cases where cells had nested child nodes (:issue:`36122`, :issue:`35802`)
- Fixed regression in :class:`DataFrame` and :class:`Series` comparisons between numeric arrays and strings (:issue:`35700`, :issue:`36377`)
- Fixed regression in :meth:`DataFrame.apply` with ``raw=True`` and user-function returning string (:issue:`35940`)
- Fixed regression when setting empty :class:`DataFrame` column to a :class:`Series` in preserving name of index in frame (:issue:`36527`)
- Fixed regression in :class:`Period` incorrect value for ordinal over the maximum timestamp (:issue:`36430`)

Expand Down
18 changes: 17 additions & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,23 @@ def apply_empty_result(self):

def apply_raw(self):
""" apply to the values as a numpy array """
result = np.apply_along_axis(self.f, self.axis, self.values)

def wrap_function(func):
"""
Wrap user supplied function to work around numpy issue.

see https://github.com/numpy/numpy/issues/8352
"""

def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, str):
result = np.array(result, dtype=object)
return result

return wrapper

result = np.apply_along_axis(wrap_function(self.f), self.axis, self.values)

# TODO: mixed type case
if result.ndim == 2:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1561,3 +1561,11 @@ def test_apply_no_suffix_index():
)

tm.assert_frame_equal(result, expected)


def test_apply_raw_returns_string():
# https://github.com/pandas-dev/pandas/issues/35940
df = pd.DataFrame({"A": ["aa", "bbb"]})
result = df.apply(lambda x: x[0], axis=1, raw=True)
expected = pd.Series(["aa", "bbb"])
tm.assert_series_equal(result, expected)