Skip to content

Commit bdf1292

Browse files
alimcmaster1WillAyd
authored andcommitted
Fix pipe docs (pandas-dev#29368)
1 parent 391df15 commit bdf1292

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

doc/source/getting_started/basics.rst

+34-11
Original file line numberDiff line numberDiff line change
@@ -753,28 +753,51 @@ on an entire ``DataFrame`` or ``Series``, row- or column-wise, or elementwise.
753753
Tablewise function application
754754
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
755755

756-
``DataFrames`` and ``Series`` can of course just be passed into functions.
756+
``DataFrames`` and ``Series`` can be passed into functions.
757757
However, if the function needs to be called in a chain, consider using the :meth:`~DataFrame.pipe` method.
758-
Compare the following
759758

760-
.. code-block:: python
759+
First some setup:
760+
761+
.. ipython:: python
761762
762-
# f, g, and h are functions taking and returning ``DataFrames``
763-
>>> f(g(h(df), arg1=1), arg2=2, arg3=3)
763+
def extract_city_name(df):
764+
"""
765+
Chicago, IL -> Chicago for city_name column
766+
"""
767+
df['city_name'] = df['city_and_code'].str.split(",").str.get(0)
768+
return df
764769
765-
with the equivalent
770+
def add_country_name(df, country_name=None):
771+
"""
772+
Chicago -> Chicago-US for city_name column
773+
"""
774+
col = 'city_name'
775+
df['city_and_country'] = df[col] + country_name
776+
return df
766777
767-
.. code-block:: python
778+
df_p = pd.DataFrame({'city_and_code': ['Chicago, IL']})
779+
780+
781+
``extract_city_name`` and ``add_country_name`` are functions taking and returning ``DataFrames``.
782+
783+
Now compare the following:
784+
785+
.. ipython:: python
786+
787+
add_country_name(extract_city_name(df_p), country_name='US')
788+
789+
Is equivalent to:
790+
791+
.. ipython:: python
768792
769-
>>> (df.pipe(h)
770-
... .pipe(g, arg1=1)
771-
... .pipe(f, arg2=2, arg3=3))
793+
(df_p.pipe(extract_city_name)
794+
.pipe(add_country_name, country_name="US"))
772795
773796
Pandas encourages the second style, which is known as method chaining.
774797
``pipe`` makes it easy to use your own or another library's functions
775798
in method chains, alongside pandas' methods.
776799

777-
In the example above, the functions ``f``, ``g``, and ``h`` each expected the ``DataFrame`` as the first positional argument.
800+
In the example above, the functions ``extract_city_name`` and ``add_country_name`` each expected a ``DataFrame`` as the first positional argument.
778801
What if the function you wish to apply takes its data as, say, the second argument?
779802
In this case, provide ``pipe`` with a tuple of ``(callable, data_keyword)``.
780803
``.pipe`` will route the ``DataFrame`` to the argument specified in the tuple.

setup.cfg

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ ignore = E402, # module level import not at top of file
4747
E711, # comparison to none should be 'if cond is none:'
4848

4949
exclude =
50-
doc/source/getting_started/basics.rst
5150
doc/source/development/contributing_docstring.rst
5251

5352

0 commit comments

Comments
 (0)