Going fast! #DataquestChallenge Premium Annual Offer:
500 get 50% & the next 1000 get 40% off.
GET OFFER CODE

Check for NaN in IF-Statement

Hi together,

i have the following problem i’m stuck with and i hope somebody can help me.
I have a Dataframe with some TimeStamps i want to calculate the open time for some issues.
So far so good and everything works fine but i have one exception i have to handle somehow.
If the column Solution Time is a NaN in a row this issue is not solved yet and should not be calculated.
The calculation should only be executed if there is a NaN in a row.

This is what i tried but it doesn’t work:

if (math.isnull(df['Solution Timestamp'])):
    #Calculation

Any ideas how i can check with a IF-Statement for NaNs and only execute the calculation if NaN == True?

Thanks for any input/help

Regards,
Simo

Hi, @Simo

Given that you are talking about a data frame, you can use the built-in pandas method

if (pd.isna(df['Solution Timestamp'])):
    #Calculation

You can read more using the Pandas documentation

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.isna.html?highlight=isna#pandas.isna

I really find it strange to see the variable df in an if expression. I would assume it is one line from your DF. But my answer is based on your question

Regards, Max