Pandas: convert dtype 'object' to int
<p>I've read an SQL query into Pandas and the values are coming in as dtype 'object', although they are strings, dates and integers. I am able to convert the date 'object' to a Pandas datetime dtype, but I'm getting an error when trying to convert the string and integers.</p>

<p>Here is an example:</p>

<pre><code>&gt;&gt;&gt; import pandas as pd
&gt;&gt;&gt; df = pd.read_sql_query('select * from my_table', conn)
&gt;&gt;&gt; df
    id    date          purchase
 1  abc1  2016-05-22    1
 2  abc2  2016-05-29    0
 3  abc3  2016-05-22    2
 4  abc4  2016-05-22    0

&gt;&gt;&gt; df.dtypes
 id          object
 date        object
 purchase    object
 dtype: object
</code></pre>

<p>Converting the df['date'] to a datetime works...</p>

<pre><code>&gt;&gt;&gt; pd.to_datetime(df['date'])
 1  2016-05-22
 2  2016-05-29
 3  2016-05-22
 4  2016-05-22
 Name: date, dtype: datetime64[ns] 
</code></pre>

<p>But I get an error when trying to convert the df['purchase'] to an integer. </p>

<pre><code>&gt;&gt;&gt; df['purchase'].astype(int)
 ....
 pandas/lib.pyx in pandas.lib.astype_intsafe (pandas/lib.c:16667)()
 pandas/src/util.pxd in util.set_value_at (pandas/lib.c:67540)()

 TypeError: long() argument must be a string or a number, not 'java.lang.Long'
</code></pre>

<p>NOTE: I get a similar error when I tried .astype('float')</p>

<p>And when trying to convert to a string, nothing seems to happen...</p>

<pre><code>&gt;&gt;&gt; df['id'].apply(str)
 1 abc1
 2 abc2
 3 abc3
 4 abc4
 Name: id, dtype: object
</code></pre>

<p>Documenting the answer that worked for me based on the comment by @piRSquared. </p>

<p>I needed to convert to a string first, then an integer.</p>

<pre><code>&gt;&gt;&gt; df['purchase'].astype(str).astype(int)
</code></pre>

python pandas numpy