João Galdino
João Galdino

Reputation: 95

How to compare the difference between two date and get it in hours?

I want check if the difference between the date of column DHR_COL and DHR_TRANSF1 is more than 20 hours.

I did the calculation below to get this difference, but I cannot get in hours to compare with my limit (20 hours)

df["Transf1-Coleta"] = pd.to_datetime(df["DHR_TRANSF1"], format=("%Y-%m-%d %H:%M:%S"))-pd.to_datetime(df["DHR_COL"], format=("%Y-%m-%d %H:%M:%S"))

In [21]: df

Out[21]:

              DHR_TRANSF1              DHR_COL           Transf1-Coleta
0          2021-07-14 15:00:00    2021-07-14 03:25:00    0 days 11:35:00
4          2021-07-14 23:59:00    2021-07-14 13:30:00    0 days 10:29:00
5          2021-07-15 00:16:00    2021-07-19 00:00:00   -4 days +00:16:00
8          2021-07-15 05:30:00    2021-07-14 17:00:00    0 days 12:30:00
9          2021-07-14 09:29:41    2021-07-14 17:00:00   -1 days +16:29:41

I thought about doing something like that, but it didn't work.How can I made it?

pd.to_datetime(df["Transf1-Coleta"] > "20:00:00") 

Upvotes: 0

Views: 68

Answers (1)

Chris Wesseling
Chris Wesseling

Reputation: 6378

These are the rows where the difference is bigger than 20 hours:

df[df["Transf1-Coleta"] > pd.Timedelta(hours=20)]
# or closer to your original try:
df[df["Transf1-Coleta"] > pd.Timedelta("20:00:00")]

Upvotes: 1

Related Questions