Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I fix TypeError: cannot convert the series to <class 'float'> when column in df has nan?

I have a dataframe

dfooc
Name AddressId
XYZ  nan
ABC  <memory at 0x7f145136ca10>
HIJ  nan

How do I convert this AddressId column to float type?
it is currently –

Name: AddressId, Length: 346498, dtype: object

I tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dfooc['AddressId'] = int(float(dfooc['AddressId']))
raise TypeError(f"cannot convert the series to {converter}")

TypeError: cannot convert the series to <class 'float'>

I am converting this to float because if i let this be as it is, im unable to get this columns data into SQL server and im guessing its because SQL server doesnt like ‘<memory at 0x7f145136ca10>’

>Solution :

You can do :

dfooc['AddressId'] = dfooc['AddressId'].astype('float', errors = 'ignore')

errors control raising of exceptions on invalid data for provided dtype according to the documentation, set to ignore, it will ssuppress exceptions and on error return original object (i.e. nan).

Or you can fillna first and then convert the series to float :

dfooc['AddressId'].fillna(0, inplace = True)
dfooc['AddressId'] = dfooc['AddressId'].astype('float')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading