-
-
Notifications
You must be signed in to change notification settings - Fork 141
ENH: add dict
to return type of func
argument of DataFrame#apply
#470
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
Dr-Irv
merged 7 commits into
pandas-dev:main
from
skatsuta:dataframe-apply-func-return-dict
Dec 19, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
77060c1
ENH: add dict to return type of func argument of DataFrame#apply when…
skatsuta eec0284
tests/test_frame: replace Literal[None] with just None
skatsuta 8187af6
Add support for apply func that returns Mapping with axis=0 and resul…
skatsuta 05940be
Replace dict with Mapping in return type of f for DataFrame#apply
skatsuta b101d77
Add support for apply func that returns Mapping with result_type="red…
skatsuta e6160fc
Add support for apply func that returns Mapping with axis=1 and resul…
skatsuta 370a012
Add support for apply func that returns Mapping with result_type="bro…
skatsuta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -477,6 +477,9 @@ def returns_listlike_of_2(x: pd.Series) -> tuple[int, int]: | |
def returns_listlike_of_3(x: pd.Series) -> tuple[int, int, int]: | ||
return (7, 8, 9) | ||
|
||
def returns_dict(x: pd.Series) -> dict[str, int]: | ||
return {"col4": 7, "col5": 8} | ||
|
||
# Misc checks | ||
check(assert_type(df.apply(np.exp), pd.DataFrame), pd.DataFrame) | ||
check(assert_type(df.apply(str), "pd.Series[str]"), pd.Series, str) | ||
|
@@ -489,22 +492,15 @@ def gethead(s: pd.Series, y: int) -> pd.Series: | |
|
||
# Check various return types for default result_type (None) with default axis (0) | ||
check(assert_type(df.apply(returns_scalar), "pd.Series[int]"), pd.Series, int) | ||
check( | ||
assert_type(df.apply(returns_series), pd.DataFrame), | ||
pd.DataFrame, | ||
) | ||
check( | ||
assert_type(df.apply(returns_listlike_of_3), pd.DataFrame), | ||
pd.DataFrame, | ||
) | ||
check(assert_type(df.apply(returns_series), pd.DataFrame), pd.DataFrame) | ||
check(assert_type(df.apply(returns_listlike_of_3), pd.DataFrame), pd.DataFrame) | ||
check(assert_type(df.apply(returns_dict), pd.Series), pd.Series) | ||
|
||
# Check various return types for result_type="expand" with default axis (0) | ||
check( | ||
assert_type( | ||
# Note that technically it does not make sense to pass a result_type of "expand" to a scalar return | ||
df.apply(returns_scalar, result_type="expand"), | ||
"pd.Series[int]", | ||
), | ||
# Note that technically it does not make sense | ||
# to pass a result_type of "expand" to a scalar return | ||
assert_type(df.apply(returns_scalar, result_type="expand"), "pd.Series[int]"), | ||
pd.Series, | ||
int, | ||
) | ||
|
@@ -518,71 +514,47 @@ def gethead(s: pd.Series, y: int) -> pd.Series: | |
), | ||
pd.DataFrame, | ||
) | ||
check( | ||
assert_type(df.apply(returns_dict, result_type="expand"), pd.DataFrame), | ||
pd.DataFrame, | ||
) | ||
|
||
# Check various return types for result_type="reduce" with default axis (0) | ||
check( | ||
assert_type( | ||
# Note that technically it does not make sense to pass a result_type of "reduce" to a scalar return | ||
df.apply(returns_scalar, result_type="reduce"), | ||
"pd.Series[int]", | ||
), | ||
# Note that technically it does not make sense | ||
# to pass a result_type of "reduce" to a scalar return | ||
assert_type(df.apply(returns_scalar, result_type="reduce"), "pd.Series[int]"), | ||
pd.Series, | ||
int, | ||
) | ||
check( | ||
assert_type( | ||
# Note that technically it does not make sense to pass a result_type of "reduce" to a series return | ||
df.apply(returns_series, result_type="reduce"), | ||
pd.Series, | ||
), | ||
# Note that technically it does not make sense | ||
# to pass a result_type of "reduce" to a series return | ||
assert_type(df.apply(returns_series, result_type="reduce"), pd.Series), | ||
pd.Series, # This technically returns a pd.Series[pd.Series], but typing does not support that | ||
) | ||
check( | ||
assert_type(df.apply(returns_listlike_of_3, result_type="reduce"), pd.Series), | ||
pd.Series, | ||
) | ||
|
||
# Check various return types for result_type="broadcast" with default axis (0) | ||
check( | ||
assert_type( | ||
# Note that technically it does not make sense to pass a result_type of "broadcast" to a scalar return | ||
df.apply(returns_scalar, result_type="broadcast"), | ||
pd.DataFrame, | ||
), | ||
pd.DataFrame, | ||
) | ||
check( | ||
assert_type(df.apply(returns_series, result_type="broadcast"), pd.DataFrame), | ||
pd.DataFrame, | ||
) | ||
check( | ||
assert_type( | ||
# Can only broadcast a list-like of 2 elements, not 3, because there are 2 rows | ||
df.apply(returns_listlike_of_2, result_type="broadcast"), | ||
pd.DataFrame, | ||
), | ||
pd.DataFrame, | ||
Comment on lines
-547
to
-564
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These assertions are moved to the bottom so that test cases for |
||
assert_type(df.apply(returns_dict, result_type="reduce"), pd.Series), pd.Series | ||
) | ||
|
||
# Check various return types for default result_type (None) with axis=1 | ||
check( | ||
assert_type(df.apply(returns_scalar, axis=1), "pd.Series[int]"), pd.Series, int | ||
) | ||
check( | ||
assert_type(df.apply(returns_series, axis=1), pd.DataFrame), | ||
pd.DataFrame, | ||
) | ||
check( | ||
assert_type(df.apply(returns_listlike_of_3, axis=1), pd.Series), | ||
pd.Series, | ||
) | ||
check(assert_type(df.apply(returns_series, axis=1), pd.DataFrame), pd.DataFrame) | ||
check(assert_type(df.apply(returns_listlike_of_3, axis=1), pd.Series), pd.Series) | ||
check(assert_type(df.apply(returns_dict, axis=1), pd.Series), pd.Series) | ||
|
||
# Check various return types for result_type="expand" with axis=1 | ||
check( | ||
# Note that technically it does not make sense | ||
# to pass a result_type of "expand" to a scalar return | ||
assert_type( | ||
# Note that technically it does not make sense to pass a result_type of "expand" to a scalar return | ||
df.apply(returns_scalar, axis=1, result_type="expand"), | ||
"pd.Series[int]", | ||
df.apply(returns_scalar, axis=1, result_type="expand"), "pd.Series[int]" | ||
), | ||
pd.Series, | ||
int, | ||
|
@@ -599,22 +571,26 @@ def gethead(s: pd.Series, y: int) -> pd.Series: | |
), | ||
pd.DataFrame, | ||
) | ||
check( | ||
assert_type(df.apply(returns_dict, axis=1, result_type="expand"), pd.DataFrame), | ||
pd.DataFrame, | ||
) | ||
|
||
# Check various return types for result_type="reduce" with axis=1 | ||
check( | ||
# Note that technically it does not make sense | ||
# to pass a result_type of "reduce" to a scalar return | ||
assert_type( | ||
# Note that technically it does not make sense to pass a result_type of "reduce" to a scalar return | ||
df.apply(returns_scalar, axis=1, result_type="reduce"), | ||
"pd.Series[int]", | ||
df.apply(returns_scalar, axis=1, result_type="reduce"), "pd.Series[int]" | ||
), | ||
pd.Series, | ||
int, | ||
) | ||
check( | ||
# Note that technically it does not make sense | ||
# to pass a result_type of "reduce" to a series return | ||
assert_type( | ||
# Note that technically it does not make sense to pass a result_type of "reduce" to a series return | ||
df.apply(returns_series, axis=1, result_type="reduce"), | ||
pd.DataFrame, | ||
df.apply(returns_series, axis=1, result_type="reduce"), pd.DataFrame | ||
), | ||
pd.DataFrame, | ||
) | ||
|
@@ -624,13 +600,34 @@ def gethead(s: pd.Series, y: int) -> pd.Series: | |
), | ||
pd.Series, | ||
) | ||
check( | ||
assert_type(df.apply(returns_dict, axis=1, result_type="reduce"), pd.Series), | ||
pd.Series, | ||
) | ||
|
||
# Check various return types for result_type="broadcast" with axis=1 | ||
# Check various return types for result_type="broadcast" with axis=0 and axis=1 | ||
check( | ||
# Note that technically it does not make sense | ||
# to pass a result_type of "broadcast" to a scalar return | ||
assert_type(df.apply(returns_scalar, result_type="broadcast"), pd.DataFrame), | ||
pd.DataFrame, | ||
) | ||
check( | ||
assert_type(df.apply(returns_series, result_type="broadcast"), pd.DataFrame), | ||
pd.DataFrame, | ||
) | ||
check( | ||
# Can only broadcast a list-like of 2 elements, not 3, because there are 2 rows | ||
assert_type( | ||
# Note that technicaly it does not make sense to pass a result_type of "broadcast" to a scalar return | ||
df.apply(returns_scalar, axis=1, result_type="broadcast"), | ||
pd.DataFrame, | ||
df.apply(returns_listlike_of_2, result_type="broadcast"), pd.DataFrame | ||
), | ||
pd.DataFrame, | ||
) | ||
check( | ||
# Note that technicaly it does not make sense | ||
# to pass a result_type of "broadcast" to a scalar return | ||
assert_type( | ||
df.apply(returns_scalar, axis=1, result_type="broadcast"), pd.DataFrame | ||
), | ||
pd.DataFrame, | ||
) | ||
|
@@ -642,14 +639,29 @@ def gethead(s: pd.Series, y: int) -> pd.Series: | |
) | ||
check( | ||
assert_type( | ||
# Can only broadcast a list-like of 3 elements, not 2, as there are 3 columns | ||
# Can only broadcast a list-like of 3 elements, not 2, | ||
# as there are 3 columns | ||
df.apply(returns_listlike_of_3, axis=1, result_type="broadcast"), | ||
pd.DataFrame, | ||
), | ||
pd.DataFrame, | ||
) | ||
# Since dicts will be assigned to elements of np.ndarray inside broadcasting, | ||
# we need to use a DataFrame with object dtype to make the assignment possible. | ||
df2 = pd.DataFrame({"col1": ["a", "b"], "col2": ["c", "d"]}) | ||
check( | ||
assert_type(df2.apply(returns_dict, result_type="broadcast"), pd.DataFrame), | ||
pd.DataFrame, | ||
) | ||
check( | ||
assert_type( | ||
df2.apply(returns_dict, axis=1, result_type="broadcast"), pd.DataFrame | ||
), | ||
pd.DataFrame, | ||
) | ||
|
||
# Test various other positional/keyword argument combinations to ensure all overloads are supported | ||
# Test various other positional/keyword argument combinations | ||
# to ensure all overloads are supported | ||
check( | ||
assert_type(df.apply(returns_scalar, axis=0), "pd.Series[int]"), pd.Series, int | ||
) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not necessarily have to be
Literal[None]
, justNone
is OK, and so are others.