Skip to content

DOC: documented that .apply(func) executes func twice on the first time #6756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions doc/source/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,25 @@ The dimension of the returned result can also change:
s
s.apply(f)


.. warning::

In the current implementation apply calls func twice on the
first group to decide whether it can take a fast or slow code
path. This can lead to unexpected behavior if func has
side-effects, as they will take effect twice for the first
group.

.. ipython:: python

d = DataFrame({"a":["x", "y"], "b":[1,2]})
def identity(df):
print df
return df

d.groupby("a").apply(identity)


Other useful features
---------------------

Expand Down
8 changes: 8 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3302,6 +3302,14 @@ def apply(self, func, axis=0, broadcast=False, raw=False, reduce=None,
array/series
Additional keyword arguments will be passed as keywords to the function

Notes
-----
In the current implementation apply calls func twice on the
first column/row to decide whether it can take a fast or slow
code path. This can lead to unexpected behavior if func has
side-effects, as they will take effect twice for the first
column/row.

Examples
--------
>>> df.apply(numpy.sqrt) # returns DataFrame
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,14 @@ def apply(self, func, *args, **kwargs):

Notes
-----
See online documentation for full exposition on how to use apply
See online documentation for full exposition on how to use apply.

In the current implementation apply calls func twice on the
first group to decide whether it can take a fast or slow code
path. This can lead to unexpected behavior if func has
side-effects, as they will take effect twice for the first
group.


See also
--------
Expand Down