Pandas dataframe conditional value assignemnt fails when there are exactly 2 rows in the dataframe
04:25 24 Feb 2026

I'm facing an issue with pandas. Assign values based on conditional fails for the case that I have exactly 2 rows and they both meet the same condition. If they meet different conditions it also works or for any other number of rows. The issue is puzzling.

The minimal example causing the error:

df = pd.DataFrame()
proba = [0.7, 0.6]
df["proba"] = proba
df['prediction'] = ['neg' if x < 0.5 else 'pos' for x in proba]
df.loc[df['proba'] <= 0.2, ['interpretation','class']] = \
        ['very high probability to be negative', 'very-high']
df.loc[(df['proba'] > 0.1) & (df['proba'] <= 0.4), ['interpretation','class']] = \
        ['Likely to be negative.', 'likely']
df
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Note that this is in jupyter notebook and the last line is needed to trigger the issue. in the real code the line afterwards causes the issue:

df.fillna({'interpretation': '\U0001F44D', 'class': 'thumbs-up'}, inplace=True)

This confused me even more as the fillna doesn't really cause the issue just "materializing" the df after the conditionals does. df.head(5) or any call to the df triggers the issue.

If you change the proba list and add a 3rd entry, it will work. if you change one entry to 0.4, it will work.

This error is due to an update of the environment to python 3.13 and pandas 3 and new numpy version.

How can I fix it so it works also for the described edge-case?

python pandas