Compare two Timestamps

Hi friends,

Is it possible to compare two Timestamps based on the Hours?
I want to figure out if:

Send = pd.to_datetime(df.Send_Time).dt.strftime('%d-%m-%Y %H:%M')

is 3 Hours or more older than:

Read = pd.to_datetime(df.Read_Time).dt.strftime('%d-%m-%Y %H:%M')

I can compare with an IF-Statement if one Date is older than the other or the same, but how can i check if one Date is 3 Hours older than the other one? Do i have to extract the H from the Timestamp for comparsion or is here a better way to solve this?

Thanks for any idea.

Hi Simo

I need a clarification here.
You are converting Send_Time to datetime type, then again you are converting it to object type, which is basically a string.
So, now both Send and Read are Series objects that contain string data. And then you want to find the time difference between the two.
Do you really need to format the data in Send_Time and Read_Time to object types?


Hi @dash.debasmita

well, i’m note sure if this was clever but i will try to explain you why i did it this way.
I read that i have to convert to string within the ‘to_datetime’ thats why i did the ‘dt.strftime’.
I tried to use Timedelta but I can’t use on a string get an error message:
read > send + pd.Timedelta(hours=3)
unsupported type(s) for +: ‘Timedelta’ and ‘str’.

So thats why I removed strftime so that I don’t convert to string so i can use the ‘+’ for the calculation. Now it does the job but i can’t write the result into a new column. Somehow every try makes some new troubles :slight_smile:

Send = pd.to_datetime(df[‘Send_Time’])
Read= pd.to_datetime(df[‘Read_Time’])

df['HoursCheck']=(created - send) > pd.Timedelta(hours=3)

This now returns a True or False. But i want to add a ‘Overdue’ or ‘No Overdue’ to the Column HoursCheck.

Do you have an idea how do this? Thanks.


Hi Simo

Check whether the following works

def is_overdue(hr_diff):
    if hr_diff:
        return 'Overdue'
        return 'No Overdue'

df['HoursCheck'] = (created - send) > pd.Timedelta(hours=3)
df['HoursCheck'] = df['HoursCheck'].apply(lambda x: is_overdue(x))


1 Like

Hi @dash.debasmita thank you for your help.
It works fine. I can do now my calculations.
You helped me a lot with your Code :slight_smile:

All the best,

1 Like