Skip to content

Commit e9c1047

Browse files
anton-dAnton Daitche
authored and
Anton Daitche
committed
DOC: documented that .apply(func) executes func twice on the first time
Related issues are #2656, #2936 and #6753.
1 parent 2e18d1a commit e9c1047

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

doc/source/groupby.rst

+19
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,25 @@ The dimension of the returned result can also change:
663663
s
664664
s.apply(f)
665665
666+
667+
.. warning::
668+
669+
In the current implementation apply calls func twice on the
670+
first group to decide whether it can take a fast or slow code
671+
path. This can lead to unexpected behavior if func has
672+
side-effects, as they will take effect twice for the first
673+
group.
674+
675+
.. ipython:: python
676+
677+
d = DataFrame({"a":["x", "y"], "b":[1,2]})
678+
def identity(df):
679+
print df
680+
return df
681+
682+
d.groupby("a").apply(identity)
683+
684+
666685
Other useful features
667686
---------------------
668687

pandas/core/frame.py

+8
Original file line numberDiff line numberDiff line change
@@ -3302,6 +3302,14 @@ def apply(self, func, axis=0, broadcast=False, raw=False, reduce=None,
33023302
array/series
33033303
Additional keyword arguments will be passed as keywords to the function
33043304
3305+
Notes
3306+
-----
3307+
In the current implementation apply calls func twice on the
3308+
first column/row to decide whether it can take a fast or slow
3309+
code path. This can lead to unexpected behavior if func has
3310+
side-effects, as they will take effect twice for the first
3311+
column/row.
3312+
33053313
Examples
33063314
--------
33073315
>>> df.apply(numpy.sqrt) # returns DataFrame

pandas/core/groupby.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,14 @@ def apply(self, func, *args, **kwargs):
547547
548548
Notes
549549
-----
550-
See online documentation for full exposition on how to use apply
550+
See online documentation for full exposition on how to use apply.
551+
552+
In the current implementation apply calls func twice on the
553+
first group to decide whether it can take a fast or slow code
554+
path. This can lead to unexpected behavior if func has
555+
side-effects, as they will take effect twice for the first
556+
group.
557+
551558
552559
See also
553560
--------

0 commit comments

Comments
 (0)