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

2

7

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

Hale answered 27/11, 2020 at 21:33 Comment(2)
pd.isna()? pandas.pydata.org/pandas-docs/stable/reference/api/…Herbert
pd.NA is not really np.nan.Augusto
J
7

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
Junko answered 27/11, 2020 at 21:51 Comment(0)
T
0

Just to add to the answer from benji. to get back the filtered series you do:-

d[d.isna()]

Output
2    <NA>
dtype: object
Thebes answered 23/4, 2024 at 18:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.