Thursday, 3 June 2021

Pandas: calculate time elapsed between timestamp and current time, but only business hours and with timezone

I am trying to use Pandas to calculate the number of business seconds elapsed. I have a column in a Pandas dataframe that has a bunch of timestamps in the NY timezone. Here is the code I have so far:

import pandas as pd
import datetime

times = pd.DataFrame([datetime.datetime.now(timezone('America/New_York')),datetime.datetime.now(timezone('America/New_York'))],columns=['timestamp'])
time.sleep(2)
times['difference'] = (datetime.datetime.now(timezone('America/New_York')) - times)
times['difference'] = times['difference'].dt.seconds

This works as intended and gives the answer as 2 in the 'difference' column. But now I would like to only include business hours (say 9am to 5pm). So that the output between 5pm yesterday and 9am this morning is zero. I have read the Pandas documentation on time offsets and have looked for similar questions, but haven't found any examples that work.



from Pandas: calculate time elapsed between timestamp and current time, but only business hours and with timezone

No comments:

Post a Comment