Skip to content

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

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 7 commits into from
Aug 22, 2020
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)