AliasSyed
AliasSyed

Reputation: 97

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

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.

Upvotes: 1

Views: 4120

Answers (1)

kelvt
kelvt

Reputation: 1038

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)

Upvotes: 1

Related Questions