Skip to content

Commit

Permalink
docs(python): add big warnings about using apply (#9906)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Jul 15, 2023
1 parent 66ce209 commit fd871eb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5682,6 +5682,10 @@ def apply(
"""
Apply a custom/user-defined function (UDF) over the rows of the DataFrame.
.. warning::
This method is much slower than the native expressions API.
Only use it if you cannot implement your logic otherwise.
The UDF will receive each row as a tuple of values: ``udf(row)``.
Implementing logic using a Python function is almost always _significantly_
Expand Down
4 changes: 4 additions & 0 deletions py-polars/polars/dataframe/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ def apply(self, function: Callable[[DataFrame], DataFrame]) -> DataFrame:
"""
Apply a custom/user-defined function (UDF) over the groups as a sub-DataFrame.
.. warning::
This method is much slower than the native expressions API.
Only use it if you cannot implement your logic otherwise.
Implementing logic using a Python function is almost always _significantly_
slower and more memory intensive than implementing the same logic using
the native expression API because:
Expand Down
4 changes: 4 additions & 0 deletions py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3613,6 +3613,10 @@ def apply(
"""
Apply a custom/user-defined function (UDF) in a GroupBy or Projection context.
.. warning::
This method is much slower than the native expressions API.
Only use it if you cannot implement your logic otherwise.
Depending on the context it has the following behavior:
* Selection
Expand Down
4 changes: 4 additions & 0 deletions py-polars/polars/functions/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,10 @@ def apply(
"""
Apply a custom/user-defined function (UDF) in a GroupBy context.
.. warning::
This method is much slower than the native expressions API.
Only use it if you cannot implement your logic otherwise.
Depending on the context it has the following behavior:
* Select
Expand Down
4 changes: 4 additions & 0 deletions py-polars/polars/lazyframe/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ def apply(
"""
Apply a custom/user-defined function (UDF) over the groups as a new DataFrame.
.. warning::
This method is much slower than the native expressions API.
Only use it if you cannot implement your logic otherwise.
Using this is considered an anti-pattern. This will be very slow because:
- it forces the engine to materialize the whole `DataFrames` for the groups.
Expand Down
4 changes: 4 additions & 0 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4364,6 +4364,10 @@ def apply(
"""
Apply a custom/user-defined function (UDF) over elements in this Series.
.. warning::
This method is much slower than the native expressions API.
Only use it if you cannot implement your logic otherwise.
If the function returns a different datatype, the return_dtype arg should
be set, otherwise the method will fail.
Expand Down

0 comments on commit fd871eb

Please sign in to comment.