<h4 dir="auto">Code Sample:</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; df = pd.DataFrame({'A': 'a a b'.split(), 'B': [1,2,3], 'C': [4,6, 5]})
&gt;&gt;&gt; g = df.groupby('A')
&gt;&gt;&gt; g.apply(lambda x: x / x.sum())"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</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-s">'a a b'</span>.<span class="pl-en">split</span>(), <span class="pl-s">'B'</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">4</span>,<span class="pl-c1">6</span>, <span class="pl-c1">5</span>]})
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">g</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-en">groupby</span>(<span class="pl-s">'A'</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">g</span>.<span class="pl-en">apply</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-s1">x</span>.<span class="pl-en">sum</span>())</pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">Applying a function to a grouped data frame fails. The code above is the example code from the official pandas documentation: <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.apply.html" rel="nofollow">https://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.apply.html</a></p>
<p dir="auto">Output to the above code:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="/usr/local/lib/python2.7/dist-packages/pandas/core/computation/check.py:17: UserWarning: The installed version of numexpr 2.4.3 is not supported in pandas and will be not be used
The minimum supported version is 2.4.6

  ver=ver, min_ver=_MIN_NUMEXPR_VERSION), UserWarning)
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py&quot;, line 805, in apply
    return self._python_apply_general(f)
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py&quot;, line 809, in _python_apply_general
    self.axis)
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py&quot;, line 1969, in apply
    res = f(group)
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;lambda&gt;
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/ops.py&quot;, line 1262, in f
    return self._combine_series(other, na_op, fill_value, axis, level)
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py&quot;, line 3944, in _combine_series
    try_cast=try_cast)
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py&quot;, line 3958, in _combine_series_infer
    try_cast=try_cast)
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py&quot;, line 3981, in _combine_match_columns
    try_cast=try_cast)
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py&quot;, line 3435, in eval
    return self.apply('eval', **kwargs)
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py&quot;, line 3329, in apply
    applied = getattr(b, f)(**kwargs)
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py&quot;, line 1377, in eval
    result = get_result(other)
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py&quot;, line 1346, in get_result
    result = func(values, other)
  File &quot;/usr/local/lib/python2.7/dist-packages/pandas/core/ops.py&quot;, line 1216, in na_op
    yrav.fill(yrav.item())
ValueError: can only convert an array of size 1 to a Python scalar"><pre class="notranslate"><span class="pl-c1">/</span><span class="pl-s1">usr</span><span class="pl-c1">/</span><span class="pl-s1">local</span><span class="pl-c1">/</span><span class="pl-s1">lib</span><span class="pl-c1">/</span><span class="pl-s1">python2</span>.<span class="pl-c1">7</span><span class="pl-c1">/</span><span class="pl-s1">dist</span><span class="pl-c1">-</span><span class="pl-s1">packages</span><span class="pl-c1">/</span><span class="pl-s1">pandas</span><span class="pl-c1">/</span><span class="pl-s1">core</span><span class="pl-c1">/</span><span class="pl-s1">computation</span><span class="pl-c1">/</span><span class="pl-s1">check</span>.<span class="pl-s1">py</span>:<span class="pl-c1">17</span>: <span class="pl-v">UserWarning</span>: <span class="pl-v">The</span> <span class="pl-s1">installed</span> <span class="pl-s1">version</span> <span class="pl-s1">of</span> <span class="pl-s1">numexpr</span> <span class="pl-c1">2.4</span><span class="pl-c1">.3</span> <span class="pl-c1">is</span> <span class="pl-c1">not</span> <span class="pl-s1">supported</span> <span class="pl-c1">in</span> <span class="pl-s1">pandas</span> <span class="pl-c1">and</span> <span class="pl-s1">will</span> <span class="pl-s1">be</span> <span class="pl-c1">not</span> <span class="pl-s1">be</span> <span class="pl-s1">used</span>
<span class="pl-v">The</span> <span class="pl-s1">minimum</span> <span class="pl-s1">supported</span> <span class="pl-s1">version</span> <span class="pl-c1">is</span> <span class="pl-c1">2.4</span>.<span class="pl-c1">6</span>

  <span class="pl-s1">ver</span><span class="pl-c1">=</span><span class="pl-s1">ver</span>, <span class="pl-s1">min_ver</span><span class="pl-c1">=</span><span class="pl-s1">_MIN_NUMEXPR_VERSION</span>), <span class="pl-v">UserWarning</span>)
<span class="pl-v">Traceback</span> (<span class="pl-s1">most</span> <span class="pl-s1">recent</span> <span class="pl-s1">call</span> <span class="pl-s1">last</span>):
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">module</span><span class="pl-c1">&gt;</span>
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">805</span>, <span class="pl-s1">in</span> <span class="pl-s1">apply</span>
    <span class="pl-k">return</span> <span class="pl-s1">self</span>.<span class="pl-en">_python_apply_general</span>(<span class="pl-s1">f</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">809</span>, <span class="pl-s1">in</span> <span class="pl-s1">_python_apply_general</span>
    <span class="pl-s1">self</span>.<span class="pl-s1">axis</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/groupby.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1969</span>, <span class="pl-s1">in</span> <span class="pl-s1">apply</span>
    <span class="pl-s1">res</span> <span class="pl-c1">=</span> <span class="pl-en">f</span>(<span class="pl-s1">group</span>)
  <span class="pl-v">File</span> <span class="pl-s">"&lt;stdin&gt;"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1</span>, <span class="pl-s1">in</span> <span class="pl-c1">&lt;</span><span class="pl-s1">lambda</span><span class="pl-c1">&gt;</span>
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/ops.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1262</span>, <span class="pl-s1">in</span> <span class="pl-s1">f</span>
    <span class="pl-k">return</span> <span class="pl-s1">self</span>.<span class="pl-en">_combine_series</span>(<span class="pl-s1">other</span>, <span class="pl-s1">na_op</span>, <span class="pl-s1">fill_value</span>, <span class="pl-s1">axis</span>, <span class="pl-s1">level</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">3944</span>, <span class="pl-s1">in</span> <span class="pl-s1">_combine_series</span>
    <span class="pl-s1">try_cast</span><span class="pl-c1">=</span><span class="pl-s1">try_cast</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">3958</span>, <span class="pl-s1">in</span> <span class="pl-s1">_combine_series_infer</span>
    <span class="pl-s1">try_cast</span><span class="pl-c1">=</span><span class="pl-s1">try_cast</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">3981</span>, <span class="pl-s1">in</span> <span class="pl-s1">_combine_match_columns</span>
    <span class="pl-s1">try_cast</span><span class="pl-c1">=</span><span class="pl-s1">try_cast</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">3435</span>, <span class="pl-s1">in</span> <span class="pl-s1">eval</span>
    <span class="pl-k">return</span> <span class="pl-s1">self</span>.<span class="pl-en">apply</span>(<span class="pl-s">'eval'</span>, <span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">3329</span>, <span class="pl-s1">in</span> <span class="pl-s1">apply</span>
    <span class="pl-s1">applied</span> <span class="pl-c1">=</span> <span class="pl-en">getattr</span>(<span class="pl-s1">b</span>, <span class="pl-s1">f</span>)(<span class="pl-c1">**</span><span class="pl-s1">kwargs</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1377</span>, <span class="pl-s1">in</span> <span class="pl-s1">eval</span>
    <span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-en">get_result</span>(<span class="pl-s1">other</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1346</span>, <span class="pl-s1">in</span> <span class="pl-s1">get_result</span>
    <span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-en">func</span>(<span class="pl-s1">values</span>, <span class="pl-s1">other</span>)
  <span class="pl-v">File</span> <span class="pl-s">"/usr/local/lib/python2.7/dist-packages/pandas/core/ops.py"</span>, <span class="pl-s1">line</span> <span class="pl-c1">1216</span>, <span class="pl-s1">in</span> <span class="pl-s1">na_op</span>
    <span class="pl-s1">yrav</span>.<span class="pl-en">fill</span>(<span class="pl-s1">yrav</span>.<span class="pl-en">item</span>())
<span class="pl-v">ValueError</span>: <span class="pl-s1">can</span> <span class="pl-s1">only</span> <span class="pl-s1">convert</span> <span class="pl-s1">an</span> <span class="pl-s1">array</span> <span class="pl-s1">of</span> <span class="pl-s1">size</span> <span class="pl-c1">1</span> <span class="pl-s1">to</span> <span class="pl-s1">a</span> <span class="pl-v">Python</span> <span class="pl-s1">scalar</span></pre></div>
<p dir="auto">The error can be 'fixed' by applying another command to the grouped object first:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; g.sum()
   B   C
A       
a  3  10
b  3   5

&gt;&gt;&gt; g.apply(lambda x: x / x.sum())
          B    C
0  0.333333  0.4
1  0.666667  0.6
2  1.000000  1.0"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">g</span>.<span class="pl-en">sum</span>()
   <span class="pl-v">B</span>   <span class="pl-v">C</span>
<span class="pl-v">A</span>       
<span class="pl-s1">a</span>  <span class="pl-c1">3</span>  <span class="pl-c1">10</span>
<span class="pl-s1">b</span>  <span class="pl-c1">3</span>   <span class="pl-c1">5</span>

<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">g</span>.<span class="pl-en">apply</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-s1">x</span>.<span class="pl-en">sum</span>())
          <span class="pl-v">B</span>    <span class="pl-v">C</span>
<span class="pl-c1">0</span>  <span class="pl-c1">0.333333</span>  <span class="pl-c1">0.4</span>
<span class="pl-c1">1</span>  <span class="pl-c1">0.666667</span>  <span class="pl-c1">0.6</span>
<span class="pl-c1">2</span>  <span class="pl-c1">1.000000</span>  <span class="pl-c1">1.0</span></pre></div>
<h4 dir="auto">Expected Output</h4>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; g.apply(lambda x: x / x.sum())
          B    C
0  0.333333  0.4
1  0.666667  0.6
2  1.000000  1.0"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">g</span>.<span class="pl-en">apply</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-s1">x</span>.<span class="pl-en">sum</span>())
          <span class="pl-v">B</span>    <span class="pl-v">C</span>
<span class="pl-c1">0</span>  <span class="pl-c1">0.333333</span>  <span class="pl-c1">0.4</span>
<span class="pl-c1">1</span>  <span class="pl-c1">0.666667</span>  <span class="pl-c1">0.6</span>
<span class="pl-c1">2</span>  <span class="pl-c1">1.000000</span>  <span class="pl-c1">1.0</span></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
&gt;&gt;&gt; pd.show_versions()
<h2 dir="auto">INSTALLED VERSIONS</h2>
<p dir="auto">commit: None<br>
python: 2.7.12.final.0<br>
python-bits: 64<br>
OS: Linux<br>
OS-release: 4.4.0-122-generic<br>
machine: x86_64<br>
processor: x86_64<br>
byteorder: little<br>
LC_ALL: en_US.utf8<br>
LANG: en_US.UTF-8<br>
LOCALE: None.None</p>
<p dir="auto">pandas: 0.22.0<br>
pytest: 2.8.7<br>
pip: 9.0.1<br>
setuptools: 20.7.0<br>
Cython: 0.23.4<br>
numpy: 1.11.0<br>
scipy: 0.17.0<br>
pyarrow: None<br>
xarray: None<br>
IPython: 5.5.0<br>
sphinx: None<br>
patsy: 0.4.1<br>
dateutil: 2.4.2<br>
pytz: 2014.10<br>
blosc: None<br>
bottleneck: None<br>
tables: 3.2.2<br>
numexpr: 2.4.3<br>
feather: None<br>
matplotlib: 1.5.1<br>
openpyxl: 2.3.0<br>
xlrd: 0.9.4<br>
xlwt: 0.7.5<br>
xlsxwriter: None<br>
lxml: 3.5.0<br>
bs4: None<br>
html5lib: 1.0.1<br>
sqlalchemy: 1.0.11<br>
pymysql: 0.7.2.None<br>
psycopg2: 2.6.1 (dt dec mx pq3 ext lo64)<br>
jinja2: 2.10<br>
s3fs: None<br>
fastparquet: None<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
<blockquote>
<blockquote>
<blockquote>
</blockquote>
</blockquote>
</blockquote>
</details>
<p dir="auto">I wonder if it wouldn't be better/less surprising if Index.tolist did that conversion. FWIW, I was bitten by it via xlwings which tries to send an index values to Excel via COM by using index.tolist() and since the COM layer only handles basic Python types, it breaks. The patch seems trivial (I can submit a PR if you like), but I don't know whether you'd accept that, nor if it would have any implications.</p>
<div class="highlight highlight-source-diff notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="- return list(self.values)
+ return self.values.tolist()"><pre class="notranslate"><span class="pl-md"><span class="pl-md">-</span> return list(self.values)</span>
<span class="pl-mi1"><span class="pl-mi1">+</span> return self.values.tolist()</span></pre></div>
<p dir="auto">FWIW, it seems to be related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="103130382" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/10904" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/10904/hovercard" href="https://github.com/pandas-dev/pandas/issues/10904">#10904</a>.</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
import numpy as np

df = pd.DataFrame(np.zeros((3, 4)), index=np.arange(3))
l = df.index.tolist()
print(type(l[0]))
# &lt;class 'numpy.int64'&gt;"><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-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>

<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">np</span>.<span class="pl-en">zeros</span>((<span class="pl-c1">3</span>, <span class="pl-c1">4</span>)), <span class="pl-s1">index</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">3</span>))
<span class="pl-s1">l</span> <span class="pl-c1">=</span> <span class="pl-s1">df</span>.<span class="pl-s1">index</span>.<span class="pl-en">tolist</span>()
<span class="pl-en">print</span>(<span class="pl-en">type</span>(<span class="pl-s1">l</span>[<span class="pl-c1">0</span>]))
<span class="pl-c"># &lt;class 'numpy.int64'&gt;</span></pre></div>
<h4 dir="auto">Expected Output</h4>
<p dir="auto"><code class="notranslate">&lt;class 'int'&gt;</code></p>
<h4 dir="auto">output of <code class="notranslate">pd.show_versions()</code></h4>
<p dir="auto">python: 3.5.1<br>
pandas: 0.18.0<br>
numpy: 1.10.4</p>