Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ ExtensionArray

Other
^^^^^
-
- 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)