Skip to content

Commit c9fb752

Browse files
BUG: DataFrame.apply with result_type=reduce incorrect index (pandas-dev#35905)
Co-authored-by: jbrockmendel <[email protected]>
1 parent 54ea0cd commit c9fb752

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v1.1.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Fixed regressions
2424

2525
Bug fixes
2626
~~~~~~~~~
27-
27+
- Bug in :meth:`DataFrame.apply` with ``result_type="reduce"`` returning with incorrect index (:issue:`35683`)
2828
-
2929
-
3030

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)