Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error converting object (string) to Int32: TypeError: object cannot be converted to an IntegerDtype

Tags:

python

pandas

I get following error while trying to convert object (string) column in Pandas to Int32 which is integer type that allows for NA values.

df.column = df.column.astype('Int32')

TypeError: object cannot be converted to an IntegerDtype

I'm using pandas version: 0.25.3

like image 642
Hrvoje Avatar asked Feb 02 '20 08:02

Hrvoje


2 Answers

As of v0.24, you can use: df['col'] = df['col'].astype(pd.Int32Dtype())

Edit: I should have mentioned that this falls under the Nullable integer documentation. The docs specify other nullable integer types as well (i.e. Int64Dtype, Int8Dtype, UInt64Dtype, etc.)

like image 41
sng Avatar answered Nov 09 '22 10:11

sng


It's known bug, as explained here.

Workaround is to convert column first to float and than to Int32.

Make sure you strip your column from whitespaces before you do conversion:

df.column = df.column.str.strip()

Than do conversion:

df.column = df.column.astype('float')  # first convert to float before int
df.column = df.column.astype('Int32')

or simpler:

 df.column = df.column.astype('float').astype('Int32') # or Int64
like image 78
Hrvoje Avatar answered Nov 09 '22 09:11

Hrvoje