Pandas INT column datatype changes depending on how the data is called (BUG?)
<p>I have a dataframe <code>XY</code> with two columns, one is an integer column, the other one a float column.</p>

<p>The integer column is called <code>Count</code> (with a capital C to avoid problems) and its unique values are these:</p>

<pre><code>XY["Count"].unique()
array([ 38,   7,   1,  13,   3,  28,   5,   6,   4,  11,   9,   2,   8,
        22,  12,  15,  20,  17,  18,  10,  40,  14,  16,  24,  25,  39,
        81,  19,  21, 110,  88,  23,  29,  46,  26,  32,  36,  43,  96,
        34,  47,  61,  35,  56,  41,  33,  30,  53,  27,  54,  37,  65,
        31,  52,  42,  93,  76,  57,  49,  71,  48,  77,  50,  84,  44,
        69,  75,  58,  60,  55,  72, 536,  67,  80,  64,  82, 101,  99,
       104,  68,  45,  66,  87,  90,  78,  74,  62,  73,  63,  86, 108,
       105, 197, 118, 209,  70,  59,  98,  97,  94,  79,  89,  51,  83,
        85, 221, 322, 164, 116, 103, 107, 102, 143,  91,  95,  92, 120,
       188, 148, 106, 179, 124, 165, 122, 113, 119, 169, 109, 138, 123,
       121, 125, 129, 177, 137, 206, 127, 115, 111, 131, 117, 128, 100,
       126, 163, 133, 186, 114, 203, 135, 141, 227, 162], dtype=int64)
</code></pre>

<p>As you can see it's only integers and <code>numpy</code> interprets it correctly as <code>int64</code>.</p>

<p>Now lets look at extracting a single value:</p>

<pre><code>XY["Count"][0]
38
XY["Count"][0].dtype
numpy.int32

XY.ix[0,"Count"]
38
XY.ix[0,"Count"].dtype
numpy.int32
</code></pre>

<p>So direct indexing and <code>ix</code> with column label give back <code>int32</code>.</p>

<pre><code>XY.loc[0,"Count"]
38.0
XY.loc[0,"Count"].dtype
numpy.float64

XY.ix[0,0]
38.0
XY.ix[0,0].dtype
numpy.float64

XY.iloc[0,0]
38.0
XY.iloc[0,0].dtype
numpy.float64
</code></pre>

<p>But <code>loc</code>, <code>iloc</code>, and index-based <code>ix</code> report the format as <code>float64</code>.</p>

<p>Now when I'm directly extracting the values from the internal <code>numpy</code> array, it's also a <code>float64</code>. Remember that my second column is a float column.</p>

<pre><code>XY.values[0,0]
38.0
XY.values[0,0].dtype
numpy.float64
</code></pre>

<p>I don't know if I'm missing something, but this is really inconsistent and causes problems since I need to specifically return the data in integer format. I presume it's a bug.</p>

<p>EDIT 1:</p>

<p>When testing with a dataframe that has ONLY the integer column every method returned <code>int32</code>, so it seems the problem comes from the second column and inconsistent internal data conversion.</p>

<p>I think you get back a float because each row of your dataframe contains a mix of integer and float types. Upon selecting a row index with <code>ix</code> or <code>loc</code>, integers are being converted to floats. To get around this, you could use loc to select from just the required column and not the whole dataframe: </p>

<pre><code>XY['Count'].loc[4]
</code></pre>

python pandas numpy