Skip to content Skip to sidebar Skip to footer

Error Converting Object (string) To Int32: TypeError: Object Cannot Be Converted To An IntegerDtype

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') Typ

Solution 1:

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

Solution 2:

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.)


Solution 3:

Personally, I use df = df.astype({i: type_dict[i] for i in header}, errors='ignore') to deal with this problem. Note that attribute errors is to ignore all kinds of warnings. Though it is very inelegant and possible to cause other critical bugs, it does work in converting np.NAN or string of int like `100` or int like 100 to pandas.Int. Hope this could help you.


Post a Comment for "Error Converting Object (string) To Int32: TypeError: Object Cannot Be Converted To An IntegerDtype"