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\'
Cannot comment so posting this as an answer, which is somewhat in between @piRSquared/@cyril's solution and @cs95's:
As noted by @cs95, if your data contains NaNs or Nones, converting to string type will throw an error when trying to convert to int afterwards.
However, if your data consists of (numerical) strings, using convert_dtypes
will convert it to string type unless you use pd.to_numeric
as suggested by @cs95 (potentially combined with df.apply()
).
In the case that your data consists only of numerical strings (including NaNs or Nones but without any non-numeric "junk"), a possibly simpler alternative would be to convert first to float and then to one of the nullable-integer extension dtypes provided by pandas (already present in version 0.24) (see also this answer):
df['purchase'].astype(float).astype('Int64')
Note that there has been recent discussion on this on github (currently an -unresolved- closed issue though) and that in the case of very long 64-bit integers you may have to convert explicitly to float128
to avoid approximations during the conversions.