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
Copy file name to clipboardExpand all lines: doc/source/basics.rst
+63
Original file line number
Diff line number
Diff line change
@@ -624,6 +624,66 @@ We can also pass infinite values to define the bins:
624
624
Function application
625
625
--------------------
626
626
627
+
There are three main cases for function, depending on what the function
628
+
is expecting. Pandas correspondingly offers a method for each case:
629
+
630
+
1. `Tablewise Function Application`_: :meth:`~DataFrame.pipe`
631
+
2. `Row or Column-wise Function Application`_: :meth:`~DataFrame.apply`
632
+
3. Elementwise_ function application: :meth:`~DataFrame.applymap`
633
+
634
+
.. _pipe:
635
+
636
+
Tablewise Function Application
637
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
638
+
639
+
.. versionadded:: 0.16.2
640
+
641
+
DataFrames and Series can of course just be passed into functions.
642
+
However, if the function needs to be called in a chain, consider using the :meth:`~DataFrame.pipe` method.
643
+
Compare the following
644
+
645
+
.. code-block:: python
646
+
647
+
# f, g, and h are functions taking and returning DataFrames
648
+
>>> f(g(h(df), arg1=1), arg2=2, arg3=3)
649
+
650
+
with the equivalent
651
+
652
+
.. code-block:: python
653
+
654
+
>>> (df.pipe(h),
655
+
.pipe(g, arg1=1),
656
+
.pipe(f, arg2=2, arg3=3)
657
+
)
658
+
659
+
Pandas encourages the second style. It flows with the rest of pandas
660
+
methods which return DataFrames or Series and are non-mutating by
661
+
default.
662
+
663
+
In the example above, the functions ``f``, ``g``, and ``h`` each expected the DataFrame as the first positional argument.
664
+
What if the function you wish to apply takes its data as, say, the second argument?
665
+
In this case, provide ``pipe`` with a tuple of ``(callable, data_keyword)``.
666
+
``.pipe`` will route the DataFrame to the argument specified in the tuple.
667
+
668
+
For example, we can fit a regression using statsmodels. Their API expects a formula first and a DataFrame as the second argument, ``data``. We pass in the function, keyword pair ``(sm.poisson, 'data')`` to ``pipe``:
Copy file name to clipboardExpand all lines: doc/source/whatsnew/v0.16.2.txt
+41
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,47 @@ Check the :ref:`API Changes <whatsnew_0162.api>` before updating.
20
20
New features
21
21
~~~~~~~~~~~~
22
22
23
+
We've introduced a new method :meth:`DataFrame.pipe`. As suggested by the name, ``pipe``
24
+
should be used to pipe data through a chain of function calls.
25
+
The goal is to avoid confusing nested function calls like
26
+
27
+
.. code-block:: python
28
+
29
+
# df is a DataFrame, f, g, and h are functions taking and returing DataFrames
30
+
f(g(h(df), arg1=1), arg2=2, arg3=3)
31
+
32
+
The logic flows from inside out, and function names are separated from their keyword arguments.
33
+
This can be rewritten as
34
+
35
+
.. code-block:: python
36
+
37
+
(df.pipe(h)
38
+
.pipe(g, arg1=1)
39
+
.pipe(f, arg2=2)
40
+
)
41
+
42
+
Now the both the code and the logic flow from top to bottom. Keyword arguments are next to
43
+
their functions. Overall the code is much more readable.
44
+
45
+
In the example above, the functions ``f``, ``g``, and ``h`` each expected the DataFrame as the first positional argument. When the funciton you wish to apply takes its data anywhere other than the
46
+
frist argument, pass a tuple of ``(funciton, keyword)`` indicating where the DataFrame should flow.For example:
0 commit comments