Skip to content

Commit 77060c1

Browse files
committed
ENH: add dict to return type of func argument of DataFrame#apply when result_type="expand"
1 parent 7545d16 commit 77060c1

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pandas-stubs/core/frame.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ class DataFrame(NDFrame, OpsMixin):
11261126
@overload
11271127
def apply(
11281128
self,
1129-
f: Callable[..., ListLikeExceptSeriesAndStr | Series],
1129+
f: Callable[..., ListLikeExceptSeriesAndStr | Series | dict],
11301130
axis: AxisType = ...,
11311131
raw: _bool = ...,
11321132
args=...,

tests/test_frame.py

+11
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ def returns_listlike_of_2(x: pd.Series) -> tuple[int, int]:
477477
def returns_listlike_of_3(x: pd.Series) -> tuple[int, int, int]:
478478
return (7, 8, 9)
479479

480+
def returns_dict(x: pd.Series) -> dict[str, int]:
481+
return {"col4": 7, "col5": 8}
482+
480483
# Misc checks
481484
check(assert_type(df.apply(np.exp), pd.DataFrame), pd.DataFrame)
482485
check(assert_type(df.apply(str), "pd.Series[str]"), pd.Series, str)
@@ -518,6 +521,10 @@ def gethead(s: pd.Series, y: int) -> pd.Series:
518521
),
519522
pd.DataFrame,
520523
)
524+
check(
525+
assert_type(df.apply(returns_dict, result_type="expand"), pd.DataFrame),
526+
pd.DataFrame,
527+
)
521528

522529
# Check various return types for result_type="reduce" with default axis (0)
523530
check(
@@ -599,6 +606,10 @@ def gethead(s: pd.Series, y: int) -> pd.Series:
599606
),
600607
pd.DataFrame,
601608
)
609+
check(
610+
assert_type(df.apply(returns_dict, axis=1, result_type="expand"), pd.DataFrame),
611+
pd.DataFrame,
612+
)
602613

603614
# Check various return types for result_type="reduce" with axis=1
604615
check(

0 commit comments

Comments
 (0)