<h4 dir="auto">Code Sample</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

#working data
names = ['d1', 'd2', 'd3', 'd4', 'd5']
formats = ['u1', '&lt;f8', 'u1', 'u1', 'u1']
dtype = dict(names=names, formats=formats)
data = {'d1':[1,11], 'd2':[2,12], 'd3':[3,13], 'd4':[4,14], 'd5':[5,15]}

#create a pandas dataframe with uint8 variables except for a double in d2 slot.
df_create = np.rec.fromarrays(data.values(), dtype=dtype, names=data.keys())
df_create = pd.DataFrame(df_create)
df_create.loc[:,'d2'] *= 0.12345

#create a pandas dataframe with all variables uint8
df_mod = pd.DataFrame.from_dict(data, dtype=np.dtype('u1'))
#convert d2 to double and modify
df_mod.loc[:,'d2'] = df_mod.loc[:,'d2'].astype(np.dtype('float64'))
df_mod.loc[:,'d2'] *= 0.12345


print('type of df_create.loc[0,\'d1\']: {}'.format(type(df_create.loc[0,'d1'])))
print('type of df_create.loc[0,\'d2\']: {}'.format(type(df_create.loc[0,'d2'])))
print('type of df_create.iloc[0,0]: {}'.format(type(df_create.iloc[0,2])))
print('type of df_create.iloc[0,1]: {}'.format(type(df_create.iloc[0,1])))
print('type of df_create.ix[0,0]: {}'.format(type(df_create.iloc[0,2])))
print('type of df_create.ix[0,1]: {}'.format(type(df_create.iloc[0,1])))
print('')
print('type of df_mod.loc[0,\'d1\']: {}'.format(type(df_mod.loc[0,'d1'])))
print('type of df_mod.loc[0,\'d2\']: {}'.format(type(df_mod.loc[0,'d2'])))
print('type of df_mod.iloc[0,0]: {}'.format(type(df_mod.iloc[0,2])))
print('type of df_mod.iloc[0,1]: {}'.format(type(df_mod.iloc[0,1])))
print('type of df_mod.ix[0,0]: {}'.format(type(df_mod.iloc[0,2])))
print('type of df_mod.ix[0,1]: {}'.format(type(df_mod.iloc[0,1])))
print('')
print('All dtypes for dataframe df_mod:')
print(df_mod.dtypes)
print('')
pd.show_versions()"><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-c">#working data</span>
<span class="pl-s1">names</span> <span class="pl-c1">=</span> [<span class="pl-s">'d1'</span>, <span class="pl-s">'d2'</span>, <span class="pl-s">'d3'</span>, <span class="pl-s">'d4'</span>, <span class="pl-s">'d5'</span>]
<span class="pl-s1">formats</span> <span class="pl-c1">=</span> [<span class="pl-s">'u1'</span>, <span class="pl-s">'&lt;f8'</span>, <span class="pl-s">'u1'</span>, <span class="pl-s">'u1'</span>, <span class="pl-s">'u1'</span>]
<span class="pl-s1">dtype</span> <span class="pl-c1">=</span> <span class="pl-en">dict</span>(<span class="pl-s1">names</span><span class="pl-c1">=</span><span class="pl-s1">names</span>, <span class="pl-s1">formats</span><span class="pl-c1">=</span><span class="pl-s1">formats</span>)
<span class="pl-s1">data</span> <span class="pl-c1">=</span> {<span class="pl-s">'d1'</span>:[<span class="pl-c1">1</span>,<span class="pl-c1">11</span>], <span class="pl-s">'d2'</span>:[<span class="pl-c1">2</span>,<span class="pl-c1">12</span>], <span class="pl-s">'d3'</span>:[<span class="pl-c1">3</span>,<span class="pl-c1">13</span>], <span class="pl-s">'d4'</span>:[<span class="pl-c1">4</span>,<span class="pl-c1">14</span>], <span class="pl-s">'d5'</span>:[<span class="pl-c1">5</span>,<span class="pl-c1">15</span>]}

<span class="pl-c">#create a pandas dataframe with uint8 variables except for a double in d2 slot.</span>
<span class="pl-s1">df_create</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-s1">rec</span>.<span class="pl-en">fromarrays</span>(<span class="pl-s1">data</span>.<span class="pl-en">values</span>(), <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">dtype</span>, <span class="pl-s1">names</span><span class="pl-c1">=</span><span class="pl-s1">data</span>.<span class="pl-en">keys</span>())
<span class="pl-s1">df_create</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">df_create</span>)
<span class="pl-s1">df_create</span>.<span class="pl-s1">loc</span>[:,<span class="pl-s">'d2'</span>] <span class="pl-c1">*=</span> <span class="pl-c1">0.12345</span>

<span class="pl-c">#create a pandas dataframe with all variables uint8</span>
<span class="pl-s1">df_mod</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>.<span class="pl-en">from_dict</span>(<span class="pl-s1">data</span>, <span class="pl-s1">dtype</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-en">dtype</span>(<span class="pl-s">'u1'</span>))
<span class="pl-c">#convert d2 to double and modify</span>
<span class="pl-s1">df_mod</span>.<span class="pl-s1">loc</span>[:,<span class="pl-s">'d2'</span>] <span class="pl-c1">=</span> <span class="pl-s1">df_mod</span>.<span class="pl-s1">loc</span>[:,<span class="pl-s">'d2'</span>].<span class="pl-en">astype</span>(<span class="pl-s1">np</span>.<span class="pl-en">dtype</span>(<span class="pl-s">'float64'</span>))
<span class="pl-s1">df_mod</span>.<span class="pl-s1">loc</span>[:,<span class="pl-s">'d2'</span>] <span class="pl-c1">*=</span> <span class="pl-c1">0.12345</span>


<span class="pl-en">print</span>(<span class="pl-s">'type of df_create.loc[0,<span class="pl-cce">\'</span>d1<span class="pl-cce">\'</span>]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_create</span>.<span class="pl-s1">loc</span>[<span class="pl-c1">0</span>,<span class="pl-s">'d1'</span>])))
<span class="pl-en">print</span>(<span class="pl-s">'type of df_create.loc[0,<span class="pl-cce">\'</span>d2<span class="pl-cce">\'</span>]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_create</span>.<span class="pl-s1">loc</span>[<span class="pl-c1">0</span>,<span class="pl-s">'d2'</span>])))
<span class="pl-en">print</span>(<span class="pl-s">'type of df_create.iloc[0,0]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_create</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>,<span class="pl-c1">2</span>])))
<span class="pl-en">print</span>(<span class="pl-s">'type of df_create.iloc[0,1]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_create</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>,<span class="pl-c1">1</span>])))
<span class="pl-en">print</span>(<span class="pl-s">'type of df_create.ix[0,0]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_create</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>,<span class="pl-c1">2</span>])))
<span class="pl-en">print</span>(<span class="pl-s">'type of df_create.ix[0,1]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_create</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>,<span class="pl-c1">1</span>])))
<span class="pl-en">print</span>(<span class="pl-s">''</span>)
<span class="pl-en">print</span>(<span class="pl-s">'type of df_mod.loc[0,<span class="pl-cce">\'</span>d1<span class="pl-cce">\'</span>]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_mod</span>.<span class="pl-s1">loc</span>[<span class="pl-c1">0</span>,<span class="pl-s">'d1'</span>])))
<span class="pl-en">print</span>(<span class="pl-s">'type of df_mod.loc[0,<span class="pl-cce">\'</span>d2<span class="pl-cce">\'</span>]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_mod</span>.<span class="pl-s1">loc</span>[<span class="pl-c1">0</span>,<span class="pl-s">'d2'</span>])))
<span class="pl-en">print</span>(<span class="pl-s">'type of df_mod.iloc[0,0]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_mod</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>,<span class="pl-c1">2</span>])))
<span class="pl-en">print</span>(<span class="pl-s">'type of df_mod.iloc[0,1]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_mod</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>,<span class="pl-c1">1</span>])))
<span class="pl-en">print</span>(<span class="pl-s">'type of df_mod.ix[0,0]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_mod</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>,<span class="pl-c1">2</span>])))
<span class="pl-en">print</span>(<span class="pl-s">'type of df_mod.ix[0,1]: {}'</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">df_mod</span>.<span class="pl-s1">iloc</span>[<span class="pl-c1">0</span>,<span class="pl-c1">1</span>])))
<span class="pl-en">print</span>(<span class="pl-s">''</span>)
<span class="pl-en">print</span>(<span class="pl-s">'All dtypes for dataframe df_mod:'</span>)
<span class="pl-en">print</span>(<span class="pl-s1">df_mod</span>.<span class="pl-s1">dtypes</span>)
<span class="pl-en">print</span>(<span class="pl-s">''</span>)
<span class="pl-s1">pd</span>.<span class="pl-en">show_versions</span>()</pre></div>
<p dir="auto">produces the following output:<br>
type of df_create.loc[0,'d1']: &lt;class 'numpy.float64'&gt;<br>
type of df_create.loc[0,'d2']: &lt;class 'numpy.float64'&gt;<br>
type of df_create.iloc[0,0]: &lt;class 'numpy.float64'&gt;<br>
type of df_create.iloc[0,1]: &lt;class 'numpy.float64'&gt;<br>
type of df_create.ix[0,0]: &lt;class 'numpy.float64'&gt;<br>
type of df_create.ix[0,1]: &lt;class 'numpy.float64'&gt;</p>
<p dir="auto">type of df_mod.loc[0,'d1']: &lt;class 'numpy.float64'&gt;<br>
type of df_mod.loc[0,'d2']: &lt;class 'numpy.float64'&gt;<br>
type of df_mod.iloc[0,0]: &lt;class 'numpy.float64'&gt;<br>
type of df_mod.iloc[0,1]: &lt;class 'numpy.float64'&gt;<br>
type of df_mod.ix[0,0]: &lt;class 'numpy.float64'&gt;<br>
type of df_mod.ix[0,1]: &lt;class 'numpy.float64'&gt;</p>
<p dir="auto">All dtypes for dataframe df_mod:<br>
d1      uint8<br>
d2    float64<br>
d3      uint8<br>
d4      uint8<br>
d5      uint8<br>
dtype: object</p>
<h4 dir="auto">Problem description</h4>
<p dir="auto">As far as I can see, the indexing mechanism is converting non-float64s to float64s. According to the dtypes, the internal representation of the data remains uint8, but when exposed by some indexing mechanism, the uint8 data is converted to float64.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">type of df_create.loc[0,'d1']: &lt;class 'numpy.uint8'&gt;<br>
type of df_create.loc[0,'d2']: &lt;class 'numpy.float64'&gt;<br>
type of df_create.iloc[0,0]: &lt;class 'numpy.uint8'&gt;<br>
type of df_create.iloc[0,1]: &lt;class 'numpy.float64'&gt;<br>
type of df_create.ix[0,0]: &lt;class 'numpy.uint8'&gt;<br>
type of df_create.ix[0,1]: &lt;class 'numpy.float64'&gt;</p>
<p dir="auto">type of df_mod.loc[0,'d1']: &lt;class 'numpy.uint8'&gt;<br>
type of df_mod.loc[0,'d2']: &lt;class 'numpy.float64'&gt;<br>
type of df_mod.iloc[0,0]: &lt;class 'numpy.uint8'&gt;<br>
type of df_mod.iloc[0,1]: &lt;class 'numpy.float64'&gt;<br>
type of df_mod.ix[0,0]: &lt;class 'numpy.uint8'&gt;<br>
type of df_mod.ix[0,1]: &lt;class 'numpy.float64'&gt;</p>
<h4 dir="auto">Output of <code class="notranslate">pd.show_versions()</code></h4>
<details>
INSTALLED VERSIONS
<p dir="auto">commit: None<br>
python: 3.5.2.final.0<br>
python-bits: 64<br>
OS: Windows<br>
OS-release: 7<br>
machine: AMD64<br>
processor: Intel64 Family 6 Model 30 Stepping 5, GenuineIntel<br>
byteorder: little<br>
LC_ALL: None<br>
LANG: None<br>
LOCALE: None.None</p>
<p dir="auto">pandas: 0.19.2<br>
nose: 1.3.7<br>
pip: 9.0.1<br>
setuptools: 27.2.0<br>
Cython: 0.23.4<br>
numpy: 1.12.0<br>
scipy: 0.18.0<br>
statsmodels: 0.6.1<br>
xarray: 0.8.2<br>
IPython: 4.0.1<br>
sphinx: 1.3.1<br>
patsy: 0.4.0<br>
dateutil: 2.6.0<br>
pytz: 2016.10<br>
blosc: None<br>
bottleneck: 1.1.0<br>
tables: 3.2.2<br>
numexpr: 2.6.1<br>
matplotlib: 1.5.1<br>
openpyxl: 2.4.0<br>
xlrd: 0.9.4<br>
xlwt: 1.0.0<br>
xlsxwriter: 0.7.7<br>
lxml: 3.4.4<br>
bs4: 4.4.1<br>
html5lib: None<br>
httplib2: None<br>
apiclient: None<br>
sqlalchemy: 1.0.9<br>
pymysql: None<br>
psycopg2: None<br>
jinja2: 2.8<br>
boto: 2.38.0<br>
pandas_datareader: None</p>
</details>
<p dir="auto">xref <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="176344670" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/14205" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/14205/hovercard" href="https://github.com/pandas-dev/pandas/issues/14205">#14205</a></p>
<p dir="auto">The <code class="notranslate">.loc</code> method of <code class="notranslate">DataFrame</code> with different dtypes yields coerced type <strong>even if the resulting slice does only contain elements from one type</strong>. This happens only when selecting a single row.<br>
I can guess that this might be intended because the implementation of <code class="notranslate">loc</code> seems to first lookup the row <em>as a single Series</em>, doing the coercion and <em>then</em> applying the second (column) indexer.</p>
<p dir="auto">However, when the column indexer narrows down the selection such that the upcasting would not have been necessary in the first place, it can be very surprising and may even cause bugs (on user-side) if it goes unnoticed. (Like, "I was sure that those column was <code class="notranslate">int64</code>").</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import pandas as pd

&gt;&gt;&gt; d = pd.DataFrame(dict(a=[1.23]))
&gt;&gt;&gt; d[&quot;b&quot;] = 666  # adding column with int

&gt;&gt;&gt; d.info()  # info as expected (column b is int64 - fine)
&lt;class 'pandas.core.frame.DataFrame'&gt;
Int64Index: 1 entries, 0 to 0
Data columns (total 2 columns):
a    1 non-null float64
b    1 non-null int64
dtypes: float64(1), int64(1)
memory usage: 24.0 bytes

&gt;&gt;&gt; d.loc[0,&quot;b&quot;]  # UNEXPECTED: returning a single float
666.0

&gt;&gt;&gt; d.ix[0, &quot;b&quot;]  # OK: returns a single int
666

&gt;&gt;&gt; d.loc[[0], &quot;b&quot;]  # OK
0    666
Name: b, dtype: int64"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <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-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">d</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-en">dict</span>(<span class="pl-s1">a</span><span class="pl-c1">=</span>[<span class="pl-c1">1.23</span>]))
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">d</span>[<span class="pl-s">"b"</span>] <span class="pl-c1">=</span> <span class="pl-c1">666</span>  <span class="pl-c"># adding column with int</span>

<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">d</span>.<span class="pl-en">info</span>()  <span class="pl-c"># info as expected (column b is int64 - fine)</span>
<span class="pl-c1">&lt;</span><span class="pl-k">class</span> <span class="pl-s">'pandas.core.frame.DataFrame'</span><span class="pl-c1">&gt;</span>
<span class="pl-v">Int64Index</span>: <span class="pl-c1">1</span> <span class="pl-s1">entries</span>, <span class="pl-c1">0</span> <span class="pl-s1">to</span> <span class="pl-c1">0</span>
<span class="pl-v">Data</span> <span class="pl-en">columns</span> (<span class="pl-s1">total</span> <span class="pl-c1">2</span> <span class="pl-s1">columns</span>):
<span class="pl-s1">a</span>    <span class="pl-c1">1</span> <span class="pl-s1">non</span><span class="pl-c1">-</span><span class="pl-s1">null</span> <span class="pl-s1">float64</span>
<span class="pl-s1">b</span>    <span class="pl-c1">1</span> <span class="pl-s1">non</span><span class="pl-c1">-</span><span class="pl-s1">null</span> <span class="pl-s1">int64</span>
<span class="pl-s1">dtypes</span>: <span class="pl-en">float64</span>(<span class="pl-c1">1</span>), <span class="pl-en">int64</span>(<span class="pl-c1">1</span>)
<span class="pl-s1">memory</span> <span class="pl-s1">usage</span>: <span class="pl-c1">24.0</span> <span class="pl-s1">bytes</span>

<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">d</span>.<span class="pl-s1">loc</span>[<span class="pl-c1">0</span>,<span class="pl-s">"b"</span>]  <span class="pl-c"># UNEXPECTED: returning a single float</span>
<span class="pl-c1">666.0</span>

<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">d</span>.<span class="pl-s1">ix</span>[<span class="pl-c1">0</span>, <span class="pl-s">"b"</span>]  <span class="pl-c"># OK: returns a single int</span>
<span class="pl-c1">666</span>

<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">d</span>.<span class="pl-s1">loc</span>[[<span class="pl-c1">0</span>], <span class="pl-s">"b"</span>]  <span class="pl-c"># OK</span>
<span class="pl-c1">0</span>    <span class="pl-c1">666</span>
<span class="pl-v">Name</span>: <span class="pl-s1">b</span>, <span class="pl-s1">dtype</span>: <span class="pl-s1">int64</span></pre></div>
<p dir="auto">Feel free to close if the behavior s intended. Maybe this this a "bug" or an suggested API change. I dunno.</p>
<p dir="auto">Perhaps related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="92902147" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/10503" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/10503/hovercard" href="https://github.com/pandas-dev/pandas/issues/10503">#10503</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="58176167" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/9519" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/9519/hovercard" href="https://github.com/pandas-dev/pandas/issues/9519">#9519</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="54570660" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/9269" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/9269/hovercard" href="https://github.com/pandas-dev/pandas/issues/9269">#9269</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="116811012" data-permission-text="Title is private" data-url="https://github.com/pandas-dev/pandas/issues/11594" data-hovercard-type="issue" data-hovercard-url="/pandas-dev/pandas/issues/11594/hovercard" href="https://github.com/pandas-dev/pandas/issues/11594">#11594</a> ?</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="INSTALLED VERSIONS
------------------
commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Darwin
OS-release: 15.0.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8

pandas: 0.17.0
[...]"><pre class="notranslate"><code class="notranslate">INSTALLED VERSIONS
------------------
commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Darwin
OS-release: 15.0.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8

pandas: 0.17.0
[...]
</code></pre></div>