Subtracting days from a date, excluding weekends and holidays

39 Views Asked by At

I have the following dataframe, "df":

    Date        Ticker
0   2023-07-27  TSLA
1   2023-07-26  AAPL
2   2023-07-17  SPY

I am trying to subtract days from the dates in the "Date" column in order to make API calls for data from previous days. The idea is to do things like get the previous day's close and volume, get volume data for the past 10 days in order to calculate the average volume over this period, etc. I am using the Polygon.io API.

I tried to accomplish this using timedelta, but this doesn't allow me to account for weekends and holidays. If the day before a given date falls on a weekend or a holiday, I get no response for that query.

df['Date'] = pd.to_datetime(df['Date'])

tickers = df['Ticker']
dates = df['Date'] - timedelta(days=1)

previous_day_data = []
for t, d in zip(tickers, dates):
    previous_day_data.append(client.get_aggregate_bars(t, d, d, adjusted=False, full_range=True, multiplier='1', timespan='day'))

How can I achieve the results I am looking for?

0

There are 0 best solutions below