Pandas Dataframe automatic typecasting
<p>I am working with a pandas dataframe and need several columns (x &amp; y in the example below) to be an integer and one column to be a float (l).  It appears that assigning a new row with a float in it recasts the whole dataframe as a float.  Why is this and how do I prevent it?</p>

<pre><code>data = pd.DataFrame(data=[[3103, 1189, 1]],index = None, columns = ['y', 'x', 'l'], dtype = int)
print data.y
data.ix[1] = (3, 3, 3.4)
print data.y
</code></pre>

<p>Which produces: </p>

<pre><code>0    3103
Name: y, dtype: int32
0    3103
1       3
Name: y, dtype: float64
</code></pre>

<p>You can recast all of the other columns after each addition using:</p>

<pre><code>data['y'] = data['y'].astype(int)
</code></pre>

<p>Not the most efficient solution if you need to add a lot of columns on the fly. Alternatively you could create the entire data frame using Series in advance and type the whole thing a creation time instead if that's an option.</p>

python pandas dataframe casting