You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -249,7 +249,7 @@ Documentation can be found at :meth:`~DataFrame.pipe`.
249
249
Performance
250
250
-----------
251
251
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
253
253
performance issues, especially when written in pure Python. To improve efficiency,
254
254
consider using built-in ``NumPy`` or ``pandas`` functions instead of UDFs
255
255
for common operations.
@@ -302,3 +302,35 @@ especially for computationally heavy tasks.
302
302
.. note::
303
303
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>`_
304
304
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
+
defadd_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
0 commit comments