Skip to content

Commit 467bc93

Browse files
committed
updated table of definitions and added .pipe discussion under performance section
1 parent 8d41537 commit 467bc93

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

doc/source/user_guide/user_defined_functions.rst

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,19 @@ User-Defined Functions can be applied across various pandas methods:
9090
+----------------------------+------------------------+--------------------------+---------------------------------------------------------------------------+
9191
| Method | Function Input | Function Output | Description |
9292
+============================+========================+==========================+===========================================================================+
93-
| :meth:`map` | Scalar | Scalar | Maps each element to the element returned by the function element-wise |
93+
| :meth:`map` | Scalar | Scalar | Apply a function to each element |
9494
+----------------------------+------------------------+--------------------------+---------------------------------------------------------------------------+
9595
| :meth:`apply` (axis=0) | Column (Series) | Column (Series) | Apply a function to each column |
9696
+----------------------------+------------------------+--------------------------+---------------------------------------------------------------------------+
9797
| :meth:`apply` (axis=1) | Row (Series) | Row (Series) | Apply a function to each row |
9898
+----------------------------+------------------------+--------------------------+---------------------------------------------------------------------------+
9999
| :meth:`agg` | Series/DataFrame | Scalar or Series | Aggregate and summarizes values, e.g., sum or custom reducer |
100100
+----------------------------+------------------------+--------------------------+---------------------------------------------------------------------------+
101-
| :meth:`transform` | Series/DataFrame | Same shape as input | Transform values while preserving shape |
101+
| :meth:`transform` | Series/DataFrame | Same shape as input | Apply a function while preserving shape; raises error if shape changes |
102102
+----------------------------+------------------------+--------------------------+---------------------------------------------------------------------------+
103-
| :meth:`filter` | Series/DataFrame | Series/DataFrame | Filter data using a boolean array |
103+
| :meth:`filter` | - | - | Return rows that satisfy a boolean condition |
104104
+----------------------------+------------------------+--------------------------+---------------------------------------------------------------------------+
105-
| :meth:`pipe` | Series/DataFrame | Series/DataFrame | Chain UDFs together to apply to Series or Dataframe |
105+
| :meth:`pipe` | Series/DataFrame | Series/DataFrame | Chain functions together to apply to Series or Dataframe |
106106
+----------------------------+------------------------+--------------------------+---------------------------------------------------------------------------+
107107

108108
.. note::
@@ -249,7 +249,7 @@ Documentation can be found at :meth:`~DataFrame.pipe`.
249249
Performance
250250
-----------
251251

252-
While UDFs provide flexibility, their use is currently discouraged as they can introduce
252+
While UDFs provide flexibility, their use is generally discouraged as they can introduce
253253
performance issues, especially when written in pure Python. To improve efficiency,
254254
consider using built-in ``NumPy`` or ``pandas`` functions instead of UDFs
255255
for common operations.
@@ -302,3 +302,35 @@ especially for computationally heavy tasks.
302302
.. note::
303303
You may also refer to the user guide on `Enhancing performance <https://pandas.pydata.org/pandas-docs/dev/user_guide/enhancingperf.html#numba-jit-compilation>`_
304304
for a more detailed guide to using **Numba**.
305+
306+
Using :meth:`DataFrame.pipe` for Composable Logic
307+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
308+
309+
Another useful pattern for improving readability and composability—especially when mixing
310+
vectorized logic with UDFs—is to use the :meth:`DataFrame.pipe` method.
311+
312+
The ``.pipe`` method doesn't improve performance directly, but it enables cleaner
313+
method chaining by passing the entire object into a function. This is especially helpful
314+
when chaining custom transformations:
315+
316+
.. code-block:: python
317+
318+
def add_ratio_column(df):
319+
df["ratio"] = 100 * (df["one"] / df["two"])
320+
return df
321+
322+
df = (
323+
df
324+
.query("one > 0")
325+
.pipe(add_ratio_column)
326+
.dropna()
327+
)
328+
329+
This is functionally equivalent to calling ``add_ratio_column(df)``, but keeps your code
330+
clean and composable. The function you pass to ``.pipe`` can use vectorized operations,
331+
row-wise UDFs, or any other logic—``.pipe`` is agnostic.
332+
333+
.. note::
334+
While :meth:`DataFrame.pipe` does not improve performance on its own,
335+
it promotes clean, modular design and allows both vectorized and UDF-based logic
336+
to be composed in method chains.

0 commit comments

Comments
 (0)