Reputation: 95
I get the following error TypeError: unsupported operand type(s) for &: 'str' and 'DatetimeArray'
, I want to check if the value in column "Resumo" equals "Transfêrencia" and check a date comparison.
But with "np.select" I get a error because the type of data.
How can I do that?
>>>df
Resumo Scan DHR_ULT_ATUALIZACAO DT_PREVISAO
Transferência 2021-07-20 09:00:00 2021-07-16 00:00:00
Transferência 2021-07-16 08:00:00 2021-07-16 00:00:00
Transferência 2021-07-20 16:00:00 2021-07-19 00:00:00
conditions = [ df["Prazo"]=="No prazo",
df["Resumo"]=="Coleta",
df["Resumo"]=="Hub",
df["Resumo"]=="Transbordo",
(df["Resumo"]=="Transferência" & pd.to_datetime(df["DHR_ULT_ATUALIZACAO"], format=("%Y-%m-%d %H:%M:%S")) > pd.to_datetime(df["DT_PREVISAO"], format=("%Y-%m-%d %H:%M:%S")))
]
choices = [ "No prazo",
"Coleta",
"Hub",
"Falta",
"Transferência¹"
]
df["Desvios"] = np.select(conditions, choices, default = "Entregues")
Upvotes: 0
Views: 454
Reputation: 24314
It would be more easier if you convert datelike column to datetime first:
df["DHR_ULT_ATUALIZACAO"]=pd.to_datetime(df["DHR_ULT_ATUALIZACAO"], format=("%Y-%m-%d %H:%M:%S"))
df["DT_PREVISAO"]=pd.to_datetime(df["DT_PREVISAO"], format=("%Y-%m-%d %H:%M:%S"))
Wrap your last condition in a bracket:
conditions = [df["Prazo"]=="No prazo",
df["Resumo"]=="Coleta",
df["Resumo"]=="Hub",
df["Resumo"]=="Transbordo",
((df["Resumo"]=="Transferência") & (df["DHR_ULT_ATUALIZACAO"] > df["DT_PREVISAO"]))
]
#^added bracket ^added bracket
Upvotes: 1