79557147

Date: 2025-04-05 15:13:49
Score: 1
Natty:
Report link

pd.isnull() behaves differently depending on the type of input:

pd.isnull('nan') is False because 'nan' is just a string.

pd.isnull(float('nan')) is True because it’s a real NaN float.

In a DataFrame, if a column contains 'nan' as a string, pd.isnull() will treat it as a regular value. If you convert the column to Float64, pandas will try to coerce 'nan' to np.nan, which is treated as a missing value.

Example :

import pandas as pd

import numpy as np

df = pd.DataFrame([['1', pd.NA, 'nan']], columns=['a', 'b', 'c'])

print(pd.isnull(df['c'])) # False

df['c'] = df['c'].astype('Float64')

print(pd.isnull(df['c'])) # True

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (1):
Posted by: Talha