When to apply(pd.to_numeric) and when to astype(np.float64) in python?
<p>I have a pandas DataFrame object named <code>xiv</code> which has a column of <code>int64</code> Volume measurements.  </p>

<pre><code>In[]: xiv['Volume'].head(5)
Out[]: 

0    252000
1    484000
2     62000
3    168000
4    232000
Name: Volume, dtype: int64
</code></pre>

<p>I have read other posts (like <a href="http://stackoverflow.com/questions/21287624/pandas-dataframe-column-type-conversion?rq=1">this</a> and <a href="http://stackoverflow.com/questions/15891038/pandas-change-data-type-of-columns">this</a>) that suggest the following solutions.  But when I use either approach, it doesn't appear to change the <code>dtype</code> of the underlying data:</p>

<pre><code>In[]: xiv['Volume'] = pd.to_numeric(xiv['Volume'])

In[]: xiv['Volume'].dtypes
Out[]: 
dtype('int64')
</code></pre>

<p>Or...</p>

<pre><code>In[]: xiv['Volume'] = pd.to_numeric(xiv['Volume'])
Out[]: ###omitted for brevity###

In[]: xiv['Volume'].dtypes
Out[]: 
dtype('int64')

In[]: xiv['Volume'] = xiv['Volume'].apply(pd.to_numeric)

In[]: xiv['Volume'].dtypes
Out[]: 
dtype('int64')
</code></pre>

<p>I've also tried making a separate pandas <code>Series</code> and using the methods listed above on that Series and reassigning to the <code>x['Volume']</code> obect, which is a <code>pandas.core.series.Series</code> object.</p>

<p>I have, however, found a solution to this problem using the <code>numpy</code> package's <code>float64</code> type - <strong><em>this works but I don't know why it's different</em></strong>.</p>

<pre><code>In[]: xiv['Volume'] = xiv['Volume'].astype(np.float64)

In[]: xiv['Volume'].dtypes
Out[]: 
dtype('float64') 
</code></pre>

<p>Can someone explain how to accomplish with the <code>pandas</code> library what the <code>numpy</code> library seems to do easily with its <code>float64</code> class; that is, convert the column in the <code>xiv</code> DataFrame to a <code>float64</code> in place.</p>

<p>If you already have numeric dtypes (<code>int8|16|32|64</code>,<code>float64</code>,<code>boolean</code>) you can convert it to another "numeric" dtype using <strong>Pandas</strong> <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.astype.html" rel="nofollow">.astype()</a> method.</p>

<p>Demo:</p>

<pre><code>In [90]: df = pd.DataFrame(np.random.randint(10**5,10**7,(5,3)),columns=list('abc'), dtype=np.int64)

In [91]: df
Out[91]:
         a        b        c
0  9059440  9590567  2076918
1  5861102  4566089  1947323
2  6636568   162770  2487991
3  6794572  5236903  5628779
4   470121  4044395  4546794

In [92]: df.dtypes
Out[92]:
a    int64
b    int64
c    int64
dtype: object

In [93]: df['a'] = df['a'].astype(float)

In [94]: df.dtypes
Out[94]:
a    float64
b      int64
c      int64
dtype: object
</code></pre>

<p>It won't work for <code>object</code> (string) dtypes, that <strong>can't</strong> be converted to numbers:</p>

<pre><code>In [95]: df.ix[1, 'b'] = 'XXXXXX'

In [96]: df
Out[96]:
           a        b        c
0  9059440.0  9590567  2076918
1  5861102.0   XXXXXX  1947323
2  6636568.0   162770  2487991
3  6794572.0  5236903  5628779
4   470121.0  4044395  4546794

In [97]: df.dtypes
Out[97]:
a    float64
b     object
c      int64
dtype: object

In [98]: df['b'].astype(float)
...
skipped
...
ValueError: could not convert string to float: 'XXXXXX'
</code></pre>

<p>So here we want to use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_numeric.html" rel="nofollow">pd.to_numeric()</a> method:</p>

<pre><code>In [99]: df.b = pd.to_numeric(df['b'], errors='coerse')

In [100]: df
Out[100]:
           a          b        c
0  9059440.0  9590567.0  2076918
1  5861102.0        NaN  1947323
2  6636568.0   162770.0  2487991
3  6794572.0  5236903.0  5628779
4   470121.0  4044395.0  4546794

In [101]: df.dtypes
Out[101]:
a    float64
b    float64
c      int64
dtype: object
</code></pre>

python pandas numpy dataframe types