Understand pandas groupby/apply behaviour
<p>Let's take the following DataFrame:</p>

<pre><code>     location   outlook   play  players  temperature
0     Hamburg     sunny   True     2.00        25.00
1      Berlin     sunny   True     2.00        25.00
2   Stuttgart       NaN   True     4.00        19.00
3         NaN       NaN    NaN      nan          nan
4   Flensburg  overcast  False     0.00        33.00
5    Hannover      rain    NaN     0.00        27.00
6  Heidelberg      rain    NaN     0.00        21.50
7   Frankfurt  overcast   True     2.00        26.00
8    Augsburg     sunny   True     2.00        13.00
9       Koeln     sunny   True     2.00        16.00
</code></pre>

<p>I run</p>

<pre><code>g = df(by=["outlook", "play"])
def gfunc(x):
    print(x)
g.apply(gfunc)
</code></pre>

<p>and this is printed</p>

<pre><code>    location   outlook   play  players  temperature
4  Flensburg  overcast  False     0.00        33.00
    location   outlook   play  players  temperature
4  Flensburg  overcast  False     0.00        33.00
    location   outlook  play  players  temperature
7  Frankfurt  overcast  True     2.00        26.00
   location outlook  play  players  temperature
0   Hamburg   sunny  True     2.00        25.00
1    Berlin   sunny  True     2.00        25.00
8  Augsburg   sunny  True     2.00        13.00
9     Koeln   sunny  True     2.00        16.00
</code></pre>

<p>I don't mind not returning anything; I just want to understand why it prints the exact same output twice and then a couple of different outputs. Shouldn't the output of printing rather be a different subgroup every time? What am I missing?</p>

<p>According to the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html" rel="nofollow">docs</a></p>

<blockquote>
  <p>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.</p>
</blockquote>

python pandas