I want to create a function that flags the rows based on certain conditions.
It's not working and I think it's caused by the format of the columns.
The function is:
tolerance=5
def pmm_m2_rag(data):
if data['m2'] == data['TP_M2'] and data['m6p'] + pd.to_timedelta(tolerance,unit='D') <= data['latedate']:
return 'GREEN'
elif data['m2']!= data['TP_M2'] and data['m6p'] + pd.to_timedelta(tolerance,unit='D') < data['latedate']:
return 'AMBER'
elif data['m2']!= None and data['m6p'] + pd.to_timedelta(tolerance,unit='D') > data['latedate']:
return 'RED'
The dataframe is :
m2 TP_M2 m6p latedate
0 2019-11-28 2019-10-29 2020-02-21 2020-02-25
1 2019-11-28 2019-10-29 2020-02-21 2020-02-25
2 2019-11-28 2019-11-28 2020-02-09 2020-02-17
3 2019-11-28 2019-11-28 2020-02-29 2020-02-17
The datatype is:
m2 object
TP_M2 object
m6p object
latedate object
dtype: object
Expected output:
m2 TP_M2 m6p latedate RAG
0 2019-11-28 2019-10-29 2020-02-21 2020-02-25 AMBER
1 2019-11-28 2019-10-29 2020-02-21 2020-02-25 AMBER
2 2019-11-28 2019-11-28 2020-02-09 2020-02-17 GREEN
3 2019-11-28 2019-11-28 2020-02-29 2020-02-17 RED
datais a complete dataframe, right? The error message tells you, that it is nonsense to evaluate a dataframe in an if-statement. What you most likely want is to applypmm_m2_ragto each row in the dataframe.