.loc data frame causes value Error Cannot convert non-finite values (NA or inf) to integer

Hmm..im not sure why but here is a work around

df.loc[df['M'].notnull(), 'MN'] = pd.to_datetime('01.01.'+(df['M'].dropna().astype(int)-1).astype(str), format='%d.%m.%Y')

        M                   MN
0  2020.0  2019-01-01 00:00:00
1  2020.0  2019-01-01 00:00:00
2  2020.0  2019-01-01 00:00:00
3     NaN                  NaN

One guess would be, when calling pd.to_datetime pandas pulls from the original df instead of the sliced one.

In other workds pandas computes the operation then slices based on the loc. This explanation makes sense since df.loc[df['M'].notnull(), 'MN'] = df['M'] works as expected

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top