Are there any workarounds for pandas casting INT to Float when NaN is present?
<p>Trying to get my column to be formatted as INT as the 1.0 2.0 3.0 is causing issues with how I am using the data.  The first thing I tried was <code>df['Severity'] = pd.to_numeric(df['Severity'], errors='coerce')</code>.  While this looked like it worked initially, it reverted back to appearing as float when I wrote to csv.  Next I tried using <code>df['Severity'] = df['Severity'].astype(int)</code> followed by another failed attempt using <code>df['Severity'] = df['Severity'].astype(int, errors='coerce')</code> because it seemed a logical solution to me.</p>

<p>I did some digging into pandas' docs and found this regarding how pandas handles NAs:</p>

<pre><code>Typeclass   Promotion dtype for storing NAs
floating    no change
object  no change
integer cast to float64
boolean cast to object
</code></pre>

<p>What I find strange though, is that when I run df.info(), I get <code>Severity                        452646 non-null object</code></p>

<p>Sample Data:</p>

<pre><code>Age,Severity
1,1
2,2
3,3
4,NaN
5,4
6,4
7,5
8,7
9,6
10,5
</code></pre>

<p>Any help would be greatly appreciated :)</p>

<p>It's up to you how to handle missing values there is no correct way as it's up to you. You can either drop them using <code>dropna</code> or replace/fill them using <code>replace/fillna</code>, note that there is no way to represent <code>NaN</code> using integers: <a href="https://en.wikipedia.org/wiki/NaN#Integer_NaN" rel="nofollow">https://en.wikipedia.org/wiki/NaN#Integer_NaN</a>.</p>

<p>The reason you get <code>object</code> as the <code>dtype</code> is because you now have a mixture of integers and floats. Depending on the operation then the entire Series maybe upcast to <code>float</code> but in your case you have mixed dtypes.</p>

python pandas int