Erro "FutureWarning ... dtype incompatible with float64, please explicitly cast to a compatible dtype first"

1.7k Views Asked by At

I updated Python to 3.11.6 from 3.9 and starting to get this error:

**FutureWarning:

Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas. Value '1000000' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.**

The line where it is warning is:

df.fillna('1000000', inplace=True)

Read stackoverflow hits like Solving incompatible dtype warning... and DeprecationWarning: The default dtype for empty Series... and many more.

But still not getting grasp of the issue and how to fix it. The code still works, it is just producing this warning message. Hoping someone can help me looking at that single line code above instead of a reproduceable code.

Thanks.

The code still works, it is just producing this warning message. Trying to see how can I get rid of this warning.

1

There are 1 best solutions below

0
kelvt On

You want a different fillna strategy for different column types. '1000000' is a string while 1000000 is an int and 1000000.0 is a float. Placing strings in a float64 column will produce your error. You may iterate over the columns, get the column type and fill NAs appropriately.

for col in df:
    dt = df[col].dtype 
    if (dt == int) or (dt == float):
        df[col].fillna(1000000, inplace=True)
    else:
        df[col].fillna("1000000", inplace=True)