Skip to content

Commit 82f9a0e

Browse files
authored
BUG: DataFrame.apply with result_type=reduce incorrect index (#35777)
* BUG: DataFrame.apply with result_type=reduce incorrect index * move whatsnew to 1.1.1 * move whatsnew
1 parent 3ff1b5f commit 82f9a0e

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Fixed regressions
2525
Bug fixes
2626
~~~~~~~~~
2727
- Bug in :meth:`DataFrame.eval` with ``object`` dtype column binary operations (:issue:`35794`)
28+
- Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`)
2829
-
2930
-
3031

pandas/core/apply.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ def wrap_results_for_axis(
340340

341341
if self.result_type == "reduce":
342342
# e.g. test_apply_dict GH#8735
343-
return self.obj._constructor_sliced(results)
343+
res = self.obj._constructor_sliced(results)
344+
res.index = res_index
345+
return res
346+
344347
elif self.result_type is None and all(
345348
isinstance(x, dict) for x in results.values()
346349
):

pandas/tests/frame/apply/test_frame_apply.py

+9
Original file line numberDiff line numberDiff line change
@@ -1541,3 +1541,12 @@ def func(row):
15411541

15421542
tm.assert_frame_equal(result, expected)
15431543
tm.assert_frame_equal(df, result)
1544+
1545+
1546+
def test_apply_empty_list_reduce():
1547+
# GH#35683 get columns correct
1548+
df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]], columns=["a", "b"])
1549+
1550+
result = df.apply(lambda x: [], result_type="reduce")
1551+
expected = pd.Series({"a": [], "b": []}, dtype=object)
1552+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)