How to check if <NA> type variable is <NA> or not from a pandas dataframe? np.nan() not working

Viewed 2802

I have a dataframe with a column whose values look something like:

     YEAR_TORONTO
0    <NA>
1    2016
2    <NA>
3    1999

I need to check each element of this dataframe individually via a for loop for other reasons outside this segment of code, so I'm looking for solutions that comply with my implementation.

Essentially the code I have at the moment to check the presence of is:

if np.isnan(df.get("YEAR_TORONTO")[row]):

This leads to me getting the following error for the <NA> values:

boolean value of NA is ambiguous

Any idea of what I can do to fix this error? Help much appreciated

1 Answers

As sammywemmy said, pd.isna() should work.

>>> d = pd.Series([1,2,pd.NA,3])
>>> d
0       1
1       2
2    <NA>
3       3
dtype: object
>>> d.isna()
0    False
1    False
2     True
3    False
dtype: bool
Related