0

I would like to round the time_delta that is declared as a string to the full hour and minute

    date        stores              count   time_delta
0   2020-06-30  purchaseDateStoreA  334     02:07:26.681396
1   2020-06-30  purchaseDateStoreB  423     01:07:26.681396
2   2020-07-01  purchaseDateStoreA  294     03:46:27.282249

Desired Output

    date        stores              count   time_delta
0   2020-06-30  purchaseDateStoreA  334     02:07
1   2020-06-30  purchaseDateStoreB  423     01:07
2   2020-07-01  purchaseDateStoreA  294     03:46

This is how I computed time_delta:

df['purchaseDateStoreA'] = pd.to_datetime(df['timestamp_storeA'])
df['purchaseDateStoreB'] = pd.to_datetime(df['timestamp_storeB'])

df['time_delta'] = df['purchaseDateStoreB'] - df['purchaseDateStoreA']

user12625679
  • 676
  • 8
  • 23
  • possibly a duplicate of: https://stackoverflow.com/questions/3463930/how-to-round-the-minute-of-a-datetime-object – HolgT Jul 08 '20 at 14:19

1 Answers1

0

How about:

df['time_delta'] = pd.to_timedelta(df['time_delta']).dt.floor('Min')

Output:

         date              stores  count time_delta
0  2020-06-30  purchaseDateStoreA    334   02:07:00
1  2020-06-30  purchaseDateStoreB    423   01:07:00
2  2020-07-01  purchaseDateStoreA    294   03:46:00
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74