<p dir="auto">Take the following dataframe:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="df = pd.DataFrame({'ID':[1,2,1,1,2,3,4,4,4],
                   'Litmus':[False,False,True,True,False,False,True,True,True]})"><pre class="notranslate"><code class="notranslate">df = pd.DataFrame({'ID':[1,2,1,1,2,3,4,4,4],
                   'Litmus':[False,False,True,True,False,False,True,True,True]})
</code></pre></div>
<p dir="auto">I want to count the conditional number of True values, that is, I want to count the number of True values for each ID.</p>
<p dir="auto">This works fine:<br>
<code class="notranslate">df.groupby('ID')['Litmus'].sum()</code></p>
<p dir="auto">However, this does not:<br>
<code class="notranslate">df[4:7].groupby('ID')['Litmus'].sum()</code></p>
<p dir="auto">To make it more interesting, this again works fine:<br>
<code class="notranslate">df[4:8].groupby('ID')['Litmus'].sum()</code></p>
<p dir="auto">Bizarre behavior.</p>
<h4 dir="auto">Code Sample, a copy-pastable example if possible</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import pandas as pd

dat = pd.DataFrame({'a': [1, 1, 2, 2], 'b': [0, 1, 2, 3], 'c': [None, None, 1, 1]})

gp = dat.groupby('a')

case_one = gp['b'].aggregate(lambda x: (x != 0).all())

case_two = gp['c'].aggregate(lambda x: x.isnull().all())
"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>

<span class="pl-s1">dat</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'a'</span>: [<span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">2</span>], <span class="pl-s">'b'</span>: [<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>], <span class="pl-s">'c'</span>: [<span class="pl-c1">None</span>, <span class="pl-c1">None</span>, <span class="pl-c1">1</span>, <span class="pl-c1">1</span>]})

<span class="pl-s1">gp</span> <span class="pl-c1">=</span> <span class="pl-s1">dat</span>.<span class="pl-en">groupby</span>(<span class="pl-s">'a'</span>)

<span class="pl-s1">case_one</span> <span class="pl-c1">=</span> <span class="pl-s1">gp</span>[<span class="pl-s">'b'</span>].<span class="pl-en">aggregate</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: (<span class="pl-s1">x</span> <span class="pl-c1">!=</span> <span class="pl-c1">0</span>).<span class="pl-en">all</span>())

<span class="pl-s1">case_two</span> <span class="pl-c1">=</span> <span class="pl-s1">gp</span>[<span class="pl-s">'c'</span>].<span class="pl-en">aggregate</span>(<span class="pl-k">lambda</span> <span class="pl-s1">x</span>: <span class="pl-s1">x</span>.<span class="pl-en">isnull</span>().<span class="pl-en">all</span>())</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">By comparing <code class="notranslate">case_one</code> and <code class="notranslate">case_two</code>, it seems that <code class="notranslate">aggregate</code> function turns boolean values sometimes to integers 1 or 0.</p>
<p dir="auto">For <code class="notranslate">case_one</code> we get,</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="a
1    False
2     True
Name: b, dtype: bool"><pre class="notranslate"><code class="notranslate">a
1    False
2     True
Name: b, dtype: bool
</code></pre></div>
<p dir="auto">However for <code class="notranslate">case_two</code> we get</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="a
1    1.0
2    0.0
Name: c, dtype: float64"><pre class="notranslate"><code class="notranslate">a
1    1.0
2    0.0
Name: c, dtype: float64
</code></pre></div>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">For <code class="notranslate">case_two</code> we should get</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="a
1    True
2     False
Name: b, dtype: bool"><pre class="notranslate"><code class="notranslate">a
1    True
2     False
Name: b, dtype: bool
</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-53-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_AU.UTF-8
LOCALE: en_AU.UTF-8
<p dir="auto">pandas: 0.19.1<br>
nose: 1.3.7<br>
pip: 8.1.2<br>
setuptools: 23.0.0<br>
Cython: None<br>
numpy: 1.11.0<br>
scipy: 0.17.0<br>
statsmodels: None<br>
xarray: None<br>
IPython: 4.2.0<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.5.3<br>
pytz: 2016.4<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
matplotlib: None<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.8<br>
boto: None<br>
pandas_datareader: None</p>
</details>
<p dir="auto">This same issue appears in a different environment as well.</p>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-53-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_AU.UTF-8
<p dir="auto">pandas: 0.18.1<br>
nose: 1.3.7<br>
pip: 9.0.1<br>
setuptools: 23.0.0<br>
Cython: None<br>
numpy: 1.11.2<br>
scipy: 0.18.1<br>
statsmodels: None<br>
xarray: None<br>
IPython: 5.0.0<br>
sphinx: None<br>
patsy: None<br>
dateutil: 2.5.3<br>
pytz: 2016.6.1<br>
blosc: None<br>
bottleneck: None<br>
tables: None<br>
numexpr: None<br>
matplotlib: 1.5.1<br>
openpyxl: None<br>
xlrd: None<br>
xlwt: None<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.1.3<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.8<br>
boto: None<br>
pandas_datareader: None</p>
</details>