<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="In [62]: df = pd.DataFrame([(float(x) for x in range(0, 10)), (float(x) for x in range(10,20))])

In [63]: df
Out[63]:
      0     1     2     3     4     5     6     7     8     9
0   0.0   1.0   2.0   3.0   4.0   5.0   6.0   7.0   8.0   9.0
1  10.0  11.0  12.0  13.0  14.0  15.0  16.0  17.0  18.0  19.0

In [64]: df[0]
Out[64]:
0     0.0
1    10.0
Name: 0, dtype: float64

In [65]: df[0].astype(int)
Out[65]:
0     0
1    10
Name: 0, dtype: int64

In [66]: df[0] = df[0].astype(int)

In [67]: df
Out[67]:
    0     1     2     3     4     5     6     7     8     9
0   0   1.0   2.0   3.0   4.0   5.0   6.0   7.0   8.0   9.0
1  10  11.0  12.0  13.0  14.0  15.0  16.0  17.0  18.0  19.0

In [68]: df.iloc[0]
Out[68]:
0    0.0
1    1.0
2    2.0
3    3.0
4    4.0
5    5.0
6    6.0
7    7.0
8    8.0
9    9.0
Name: 0, dtype: float64
"><pre class="notranslate"><span class="pl-v">In</span> [<span class="pl-c1">62</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-en">float</span>(<span class="pl-s1">x</span>) <span class="pl-k">for</span> <span class="pl-s1">x</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">0</span>, <span class="pl-c1">10</span>)), (<span class="pl-en">float</span>(<span class="pl-s1">x</span>) <span class="pl-k">for</span> <span class="pl-s1">x</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-c1">10</span>,<span class="pl-c1">20</span>))])

<span class="pl-v">In</span> [<span class="pl-c1">63</span>]: <span class="pl-s1">df</span>
<span class="pl-v">Out</span>[<span class="pl-c1">63</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-c1">4</span>     <span class="pl-c1">5</span>     <span class="pl-c1">6</span>     <span class="pl-c1">7</span>     <span class="pl-c1">8</span>     <span class="pl-c1">9</span>
<span class="pl-c1">0</span>   <span class="pl-c1">0.0</span>   <span class="pl-c1">1.0</span>   <span class="pl-c1">2.0</span>   <span class="pl-c1">3.0</span>   <span class="pl-c1">4.0</span>   <span class="pl-c1">5.0</span>   <span class="pl-c1">6.0</span>   <span class="pl-c1">7.0</span>   <span class="pl-c1">8.0</span>   <span class="pl-c1">9.0</span>
<span class="pl-c1">1</span>  <span class="pl-c1">10.0</span>  <span class="pl-c1">11.0</span>  <span class="pl-c1">12.0</span>  <span class="pl-c1">13.0</span>  <span class="pl-c1">14.0</span>  <span class="pl-c1">15.0</span>  <span class="pl-c1">16.0</span>  <span class="pl-c1">17.0</span>  <span class="pl-c1">18.0</span>  <span class="pl-c1">19.0</span>

<span class="pl-v">In</span> [<span class="pl-c1">64</span>]: <span class="pl-s1">df</span>[<span class="pl-c1">0</span>]
<span class="pl-v">Out</span>[<span class="pl-c1">64</span>]:
<span class="pl-c1">0</span>     <span class="pl-c1">0.0</span>
<span class="pl-c1">1</span>    <span class="pl-c1">10.0</span>
<span class="pl-v">Name</span>: <span class="pl-c1">0</span>, <span class="pl-s1">dtype</span>: <span class="pl-s1">float64</span>

<span class="pl-v">In</span> [<span class="pl-c1">65</span>]: <span class="pl-s1">df</span>[<span class="pl-c1">0</span>].<span class="pl-en">astype</span>(<span class="pl-s1">int</span>)
<span class="pl-v">Out</span>[<span class="pl-c1">65</span>]:
<span class="pl-c1">0</span>     <span class="pl-c1">0</span>
<span class="pl-c1">1</span>    <span class="pl-c1">10</span>
<span class="pl-v">Name</span>: <span class="pl-c1">0</span>, <span class="pl-s1">dtype</span>: <span class="pl-s1">int64</span>

<span class="pl-v">In</span> [<span class="pl-c1">66</span>]: <span class="pl-s1">df</span>[<span class="pl-c1">0</span>] <span class="pl-c1">=</span> <span class="pl-s1">df</span>[<span class="pl-c1">0</span>].<span class="pl-en">astype</span>(<span class="pl-s1">int</span>)

<span class="pl-v">In</span> [<span class="pl-c1">67</span>]: <span class="pl-s1">df</span>
<span class="pl-v">Out</span>[<span class="pl-c1">67</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-c1">4</span>     <span class="pl-c1">5</span>     <span class="pl-c1">6</span>     <span class="pl-c1">7</span>     <span class="pl-c1">8</span>     <span class="pl-c1">9</span>
<span class="pl-c1">0</span>   <span class="pl-c1">0</span>   <span class="pl-c1">1.0</span>   <span class="pl-c1">2.0</span>   <span class="pl-c1">3.0</span>   <span class="pl-c1">4.0</span>   <span class="pl-c1">5.0</span>   <span class="pl-c1">6.0</span>   <span class="pl-c1">7.0</span>   <span class="pl-c1">8.0</span>   <span class="pl-c1">9.0</span>
<span class="pl-c1">1</span>  <span class="pl-c1">10</span>  <span class="pl-c1">11.0</span>  <span class="pl-c1">12.0</span>  <span class="pl-c1">13.0</span>  <span class="pl-c1">14.0</span>  <span class="pl-c1">15.0</span>  <span class="pl-c1">16.0</span>  <span class="pl-c1">17.0</span>  <span class="pl-c1">18.0</span>  <span class="pl-c1">19.0</span>

<span class="pl-v">In</span> [<span class="pl-c1">68</span>]: <span class="pl-s1">df</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>]
<span class="pl-v">Out</span>[<span class="pl-c1">68</span>]:
<span class="pl-c1">0</span>    <span class="pl-c1">0.0</span>
<span class="pl-c1">1</span>    <span class="pl-c1">1.0</span>
<span class="pl-c1">2</span>    <span class="pl-c1">2.0</span>
<span class="pl-c1">3</span>    <span class="pl-c1">3.0</span>
<span class="pl-c1">4</span>    <span class="pl-c1">4.0</span>
<span class="pl-c1">5</span>    <span class="pl-c1">5.0</span>
<span class="pl-c1">6</span>    <span class="pl-c1">6.0</span>
<span class="pl-c1">7</span>    <span class="pl-c1">7.0</span>
<span class="pl-c1">8</span>    <span class="pl-c1">8.0</span>
<span class="pl-c1">9</span>    <span class="pl-c1">9.0</span>
<span class="pl-v">Name</span>: <span class="pl-c1">0</span>, <span class="pl-s1">dtype</span>: <span class="pl-s1">float64</span></pre></div>
<h4 dir="auto">Problem description</h4>
<p dir="auto">After I reassign the 0th column as int, I expect it to be int, and it appears to be that way. But when I do a <code class="notranslate">.iloc</code> on the dataframe, it seems to be returning back into being a float somehow!</p>
<p dir="auto">This is the narrowed-down version of a more insidious problem where instead of doing an <code class="notranslate">iloc[]</code> I was running a <code class="notranslate">.apply(f)</code> on a dataframe and the dtypes of the resulting dataframe were all messed up even when the function <code class="notranslate">f</code> wasn't doing anything discernible with the types, so I narrowed it down to this.</p>
<p dir="auto">Current workaround is to re-cast all the types in <code class="notranslate">f</code>, but that can get frustrating real quick depending on the number of columns.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">I expect the row to be a mixed dtype object, with the dtype of each cell matching that of the column:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [68]: df.iloc[0]
Out[68]:
0    0
1    1.0
2    2.0
3    3.0
4    4.0
5    5.0
6    6.0
7    7.0
8    8.0
9    9.0
Name: 0, dtype: object"><pre class="notranslate"><code class="notranslate">In [68]: df.iloc[0]
Out[68]:
0    0
1    1.0
2    2.0
3    3.0
4    4.0
5    5.0
6    6.0
7    7.0
8    8.0
9    9.0
Name: 0, dtype: object
</code></pre></div>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
------------------
commit: None
python: 3.6.0.final.0
python-bits: 64
OS: Darwin
OS-release: 15.6.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
<p dir="auto">pandas: 0.20.1<br>
pytest: 3.0.7<br>
pip: 9.0.1<br>
setuptools: 35.0.2<br>
Cython: 0.25.2<br>
numpy: 1.12.1<br>
scipy: 0.19.0<br>
xarray: None<br>
IPython: 6.0.0<br>
sphinx: None<br>
patsy: 0.4.1<br>
dateutil: 2.6.0<br>
pytz: 2017.2<br>
blosc: 1.5.1<br>
bottleneck: 1.2.0<br>
tables: 3.3.0<br>
numexpr: 2.6.2<br>
feather: None<br>
matplotlib: 2.0.2<br>
openpyxl: None<br>
xlrd: 1.0.0<br>
xlwt: 1.2.0<br>
xlsxwriter: None<br>
lxml: None<br>
bs4: None<br>
html5lib: 0.999999999<br>
sqlalchemy: None<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.9.6<br>
s3fs: 0.0.9<br>
pandas_gbq: None<br>
pandas_datareader: None</p>
</details>
<p dir="auto">I'm am trying to call the to_dict function on the following DataFrame:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import pandas as pd

data = {&quot;a&quot;: [1,2,3,4,5], &quot;b&quot;: [90,80,40,60,30]}

df = pd.DataFrame(data)"><pre class="notranslate"><code class="notranslate">import pandas as pd

data = {"a": [1,2,3,4,5], "b": [90,80,40,60,30]}

df = pd.DataFrame(data)
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="df
   a   b
0  1  90
1  2  80
2  3  40
3  4  60
4  5  30"><pre class="notranslate"><code class="notranslate">df
   a   b
0  1  90
1  2  80
2  3  40
3  4  60
4  5  30
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="df.reset_index().to_dict(&quot;r&quot;)
[{'a': 1, 'b': 90, 'index': 0},
 {'a': 2, 'b': 80, 'index': 1},
 {'a': 3, 'b': 40, 'index': 2},
 {'a': 4, 'b': 60, 'index': 3},
 {'a': 5, 'b': 30, 'index': 4}]
"><pre class="notranslate"><code class="notranslate">df.reset_index().to_dict("r")
[{'a': 1, 'b': 90, 'index': 0},
 {'a': 2, 'b': 80, 'index': 1},
 {'a': 3, 'b': 40, 'index': 2},
 {'a': 4, 'b': 60, 'index': 3},
 {'a': 5, 'b': 30, 'index': 4}]

</code></pre></div>
<p dir="auto">However my problem occurs if I perform a float operation on the dataframe, which mutates the index into a float:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="(df*1.0).reset_index().to_dict(&quot;r&quot;)
[{'a': 1.0, 'b': 90.0, 'index': 0.0},  
{'a': 2.0, 'b': 80.0, 'index': 1.0},  
{'a': 3.0, 'b': 40.0, 'index': 2.0},  
{'a': 4.0, 'b': 60.0, 'index': 3.0},  
{'a': 5.0, 'b': 30.0, 'index': 4.0}]"><pre class="notranslate"><code class="notranslate">(df*1.0).reset_index().to_dict("r")
[{'a': 1.0, 'b': 90.0, 'index': 0.0},  
{'a': 2.0, 'b': 80.0, 'index': 1.0},  
{'a': 3.0, 'b': 40.0, 'index': 2.0},  
{'a': 4.0, 'b': 60.0, 'index': 3.0},  
{'a': 5.0, 'b': 30.0, 'index': 4.0}]
</code></pre></div>
<p dir="auto">Can anyone explain the above behaviour or recommend a workaround, or verify whether or not this could be a pandas bug? None of the other outtypes in the to_dict method mutates the index as shown above.</p>
<p dir="auto">I've replicated this on both pandas 0.14 and 0.18 (latest)</p>
<p dir="auto">Many thanks!</p>
<p dir="auto">link to stackoverflow: <a href="http://stackoverflow.com/questions/36548151/pandas-to-dict-changes-index-type-with-outtype-records" rel="nofollow">http://stackoverflow.com/questions/36548151/pandas-to-dict-changes-index-type-with-outtype-records</a></p>