Here is my data frame:
INV_NUM LOCATION CREATED_DATE DATE_OPENED
1 North 10/10/2020 10/11/2020
2 North 10/11/2020 10/11/2020
3 South 10/12/2020 10/01/2020
4 West 10/13/2020 WIP
I want to create an output that makes a new column called "STATUS". If the "CREATED_DATE" is before "DATE_OPENED" return "Closed", if it is on or after "DATE_OPENED" return "Open", if "DATE_OPENED" is 'WIP' then return "Closed".
Here is the code I wrote to do that:
Define a function:
def date_test(row):
if row['DATE OPENED'] == 'WIP':
return 'Closed'
if row['CREATED_DATE'] < row['DATE OPENED']:
return 'Closed'
if row['CREATED_DATE'] >= row['DATE OPENED']:
return 'Open'
else:
return 0000
Change the type to datetime:
df['DATE OPENED'] = pd.to_datetime(df['DATE OPENED'], errors='ignore')
df['CREATED_DATE'] = pd.to_datetime(df['CREATED_DATE'], errors='raise')
Test the function:
df['STATUS'] = df.apply(lambda row: date_test(row), axis=1)
When I run that code here is the error I get:
TypeError: '<' not supported between instances of 'Timestamp' and 'str'
I also found that the dtype for column "DATE_OPENED" remained as 'non-null object' after running to_datetime.
I think its the 'WIP' throwing it off but it should ignore that one and continue changing the others and the function looks for the 'WIP' string first.