Skip to content

Backport PR #35777: BUG: DataFrame.apply with result_type=reduce incorrect index #35905

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
merged 1 commit into from
Aug 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~

- Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`)
-
-

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ def wrap_results_for_axis(

if self.result_type == "reduce":
# e.g. test_apply_dict GH#8735
return self.obj._constructor_sliced(results)
res = self.obj._constructor_sliced(results)
res.index = res_index
return res

elif self.result_type is None and all(
isinstance(x, dict) for x in results.values()
):
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1541,3 +1541,12 @@ def func(row):

tm.assert_frame_equal(result, expected)
tm.assert_frame_equal(df, result)


def test_apply_empty_list_reduce():
# GH#35683 get columns correct
df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], columns=["a", "b"])

result = df.apply(lambda x: [], result_type="reduce")
expected = pd.Series({"a": [], "b": []}, dtype=object)
tm.assert_series_equal(result, expected)