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
* commit 'v0.16.2-42-g383865f': (72 commits)
BUG: provide categorical concat always on axis 0, pandas-dev#10430 numpy 1.10 makes this an error for 1-d on axis != 0
DOC: update missing.rst with ref to groupby.rst
BUG: Timedeltas with no specified units (and frac) should raise, pandas-dev#10426
BUG: using .loc[:,column] fails when the object is a multi-index, pandas-dev#10408
Removed scikit-timeseries migration docs from FAQ
BUG: GH10395 bug in DataFrame.interpolate with axis=1 and inplace=True
BUG: GH10392 bug where Table.select_column does not preserve column name
TST: Use unicode literals in string test
PERF: fix _get_level_indexer to accept an intermediate indexer result
PERF: bench for pandas-dev#10287
BUG: drop_duplicates drops name(s).
ENH: Enable ExcelWriter to construct in-memory sheets
BLD: remove support for 3.2, pandas-dev#9118
PERF: timedelta and datetime64 ops improvements
PERF: parse timedelta strings in cython pandas-dev#6755
closes bug in reset_index when index contains NaT
Check for size=0 before setting item Fixespandas-dev#10193
closes bug in apply when function returns categorical
BUG: frequencies.get_freq_code raises an error against offset with n != 1
CI: run doc-tests always
...
Copy file name to clipboardExpand all lines: doc/source/basics.rst
+74
Original file line number
Diff line number
Diff line change
@@ -624,6 +624,77 @@ We can also pass infinite values to define the bins:
624
624
Function application
625
625
--------------------
626
626
627
+
To apply your own or another library's functions to pandas objects,
628
+
you should be aware of the three methods below. The appropriate
629
+
method to use depends on whether your function expects to operate
630
+
on an entire ``DataFrame`` or ``Series``, row- or column-wise, or elementwise.
631
+
632
+
1. `Tablewise Function Application`_: :meth:`~DataFrame.pipe`
633
+
2. `Row or Column-wise Function Application`_: :meth:`~DataFrame.apply`
634
+
3. Elementwise_ function application: :meth:`~DataFrame.applymap`
635
+
636
+
.. _basics.pipe:
637
+
638
+
Tablewise Function Application
639
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
640
+
641
+
.. versionadded:: 0.16.2
642
+
643
+
``DataFrames`` and ``Series`` can of course just be passed into functions.
644
+
However, if the function needs to be called in a chain, consider using the :meth:`~DataFrame.pipe` method.
645
+
Compare the following
646
+
647
+
.. code-block:: python
648
+
649
+
# f, g, and h are functions taking and returning ``DataFrames``
650
+
>>> f(g(h(df), arg1=1), arg2=2, arg3=3)
651
+
652
+
with the equivalent
653
+
654
+
.. code-block:: python
655
+
656
+
>>> (df.pipe(h)
657
+
.pipe(g, arg1=1)
658
+
.pipe(f, arg2=2, arg3=3)
659
+
)
660
+
661
+
Pandas encourages the second style, which is known as method chaining.
662
+
``pipe`` makes it easy to use your own or another library's functions
663
+
in method chains, alongside pandas' methods.
664
+
665
+
In the example above, the functions ``f``, ``g``, and ``h`` each expected the ``DataFrame`` as the first positional argument.
666
+
What if the function you wish to apply takes its data as, say, the second argument?
667
+
In this case, provide ``pipe`` with a tuple of ``(callable, data_keyword)``.
668
+
``.pipe`` will route the ``DataFrame`` to the argument specified in the tuple.
669
+
670
+
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``:
0 commit comments