API: Clarify difference between agg and apply for Series / DataFrame #49673
Labels
API Design
Apply
Apply, Aggregate, Transform, Map
DataFrame
DataFrame data structure
Deprecate
Functionality to remove in pandas
Needs Discussion
Requires discussion from core team before further action
Series
Series data structure
Currently the use of
Series.apply
/Series.agg
andDataFrame.apply
/DataFrame.agg
is confusing. In particular, sometimes the user callsapply
and gets the results ofagg
or vice-versa:apply
with list- or dict-like arguments callsagg
.DataFrame.agg
with a UDF callsDataFrame.apply
.Series.agg
with a UDF callsSeries.apply
, and if this fails, attempts to pass the Series to the UDF.If we are to change the current behavior, it will need to go through deprecation. This will be a bit tricky with the way the code paths switch between
agg
andapply
, but I believe it can be done (see #49672 (comment)).In order to clarify the difference between agg and apply for users, I propose the following for a single argument:
DataFrame.apply
will apply the function to each Series, the result shape will be inferred from the output.DataFrame.applymap
will apply the function to each cell.Series.apply
will apply the function to each row.DataFrame.agg
will act on each Series that makes up the DataFrame, the result will always be a Series. Currently the result shape is inferred from the output.Series.agg
will act on the Series, and the result will be whatever the return is. Currentlyapply
is tried first and only when that fails willagg
act on the Series.And for multiples:
agg
will callagg
for each argument andapply
will callapply
. Currentlyapply
will callagg
in this case.I've put up #49672 to show the implementation and the impact on our tests. Some examples:
The text was updated successfully, but these errors were encountered: