-
-
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
ENH: add dict
to return type of func
argument of DataFrame#apply
#470
Conversation
… result_type="expand"
c929706
to
77060c1
Compare
DataFrame#apply
when result_type="expand"
dict
to return type of func
argument of DataFrame#apply
when result_type="expand"
I recently modified these The changes you made look good to me, I only have one question. During the last round of changes to I cannot test right now, but does |
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.
As suggested by @gandhis1 here: #470 (comment)
you should modify all of the overloads to return dict
, and create tests for all of the various combinations of axis
and result_type
as is already present in the tests.
If some combination of axis
and result_type
doesn't work with dict
as the return type, then you leave dict
out as a possibility for the return type of the Callable
in the f
argument.
@gandhis1 You are the one who added those comprehensive overloads! Thanks for the hard work! @Dr-Irv Thanks for your suggestion! Will add support for func returning dict to other overloads and also additional comprehensive test cases! |
dict
to return type of func
argument of DataFrame#apply
when result_type="expand"
dict
to return type of func
argument of DataFrame#apply
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.
@Dr-Irv I've added Mapping
return type to all possible overloads, and comprehensive test cases for all axis
and result_type
parameter combinations, which are 2 x 4 = 8 assertions in total (2 axes and 4 result types).
I've also replaced Literal[None]
with just None
, and reformatted and reordered some existing assertions for readability and clarity, but none of them are removed.
Would appreciate if you could take a look when you have time, thanks!
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, |
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.
These assertions are moved to the bottom so that test cases for result_type="broadcast"
are grouped together.
result_type: Literal[None] = ..., | ||
result_type: None = ..., |
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]
, just None
is OK, and so are others.
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.
thanks @skatsuta
assert_type()
to assert the type of any return valueProblem
It is perfectly valid to pass a function with a return type of
dict
as thefunc
argument toDataFrame#apply
whenresult_type="expand"
, but the current type stub does not support it.For example, the following code
results in the following errors when type-checked by Pyright:
Solution
Add
dict
to the return type offunc
argument ofDataFrame#apply
whenresult_type="expand"
.Also, add tests for passing a function that returns a dict to
DataFrame#apply
in the cases whereaxis
is 0 and 1, respectively.