user1922364
user1922364

Reputation: 573

Iterating through a Dataframe and Checking the Date

I have the following dataframe of orders delivery:

   Customer     Date    
1   Jay         2018-04-16
2   Anthony     2018-04-10
3   Anne        2018-05-01

I want to make another column which says which orders are late

   Customer     Date         Status
1   Jay         2018-04-16    OK
2   Anthony     2018-04-10    Late
3   Anne        2018-05-01    Ok

I´ve already turned the field Date into a Datetime format (datetime64[ns])

Here is my code:

def Status(x):
    if x < dt.datetime.today()
        return 'late'
    else:
        return 'ok'
df['Status'] = df['Date'].apply(Status)

I am getting the following error

File "<ipython-input-100-c389a139a796>", line 2
    if x < dt.datetime.today()
                              ^
SyntaxError: invalid syntax

What should I do?

Upvotes: 1

Views: 30

Answers (1)

jezrael
jezrael

Reputation: 862581

Better is use numpy.where:

df['Status'] = np.where( df['Date']< dt.datetime.today(), 'late','ok')
print (df)
  Customer       Date Status
1      Jay 2018-04-16     ok
2  Anthony 2018-04-10   late
3     Anne 2018-05-01     ok

Problem is you missing last ::

if x < dt.datetime.today():

Upvotes: 2

Related Questions