António Mendes
António Mendes

Reputation: 183

Pandas conditional statement with NaT

So, I have a dataframe with many variables. The index is uid and the other variables are all dates.

I am trying to create flag variables when a certain value is NaT but I can't find the correct statement.

I want something like this:

auxData['flagInvited'] = np.where(auxData['invited'] == NaT, 0, 1)

How can I do this easily?

Upvotes: 2

Views: 3116

Answers (1)

jezrael
jezrael

Reputation: 862901

I think you need notnull for convert to boolean mask and then cast to int - True is 1 and False is 0:

auxData['flagInvited'] = auxData['invited'].notnull().astype(int)

Sample:

auxData = pd.DataFrame({'invited':[np.nan, '2017-01-01','2017-03-03']})
auxData.invited = pd.to_datetime(auxData.invited, dayfirst=True)
print (auxData)
     invited
0        NaT
1 2017-01-01
2 2017-03-03

print (auxData['invited'].notnull())
0    False
1     True
2     True
Name: invited, dtype: bool

auxData['flagInvited'] = auxData['invited'].notnull().astype(int)
print (auxData)
     invited  flagInvited
0        NaT            0
1 2017-01-01            1
2 2017-03-03            1

Upvotes: 5

Related Questions