pandas not converting an object dtype to float64 even after error free execution of df.astype('float64')

Question:

I have tried to convert an object dtype column to float64 using .astype('float64')
It ran without raising any error, but when I check the dtype using .dtype or .dtypes it is showing that converted column again as object.

 real_estate['Age at time of purchase'].astype('float64')
164    67.0
153    61.0
133    56.0
132    56.0
179     NaN
       ... 
110    49.0
89     44.0
45     37.0
131    55.0
116    51.0
Name: Age at time of purchase, Length: 195, dtype: float64

real_estate.dtypes

Name                        object
Surname                     object
Age at time of purchase     object
Interval                    object
Y                          float64
M                          float64
D                          float64
Gender                      object
Country                     object
State                       object
dtype: object

Why is it not converting and why isn’t it giving any error?

also,
real_estate['Age at time of purchase'].dtype
this is giving me something that I haven’t expected. dtype('O')
What does dtype('O') mean?

Asked By: Jihjohn

||

Answers:

you can try like this

real_estate['Age at time of purchase']=real_estate['Age at time of purchase'].astype('float64')
Answered By: Ehtisham

In one of my datasets, pandas was inferring a column as object instead of float64. Turned out one of the rows had 2,662 instead of 2.662 because of which pandas did not determine it as float64.

So, my recommendation will be to inspect the data to see if any rows have offending value.

Answered By: Kanch
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.