I have this df
data1_txt = """
date
7/8/2021
7/6/2021
6/29/2021
"""
I need to get the previous trading day. I manage to do it with pandas_market_calendars python package. Here is my whole code
import io
import pandas_market_calendars as mcal
from pandas.tseries.offsets import CustomBusinessDay
import datetime
import pandas as pd
data1_txt = """
date
7/8/2021
7/6/2021
6/29/2021
"""
df = pd.read_fwf(io.StringIO(data1_txt))
df['date'] = pd.to_datetime(df['date'])
nyse = mcal.get_calendar('NYSE')
holidays = nyse.holidays()
holidays = list(holidays.holidays)
US_BUSINESS_DAY = CustomBusinessDay(holidays=holidays)
df['date_prev'] = df['date'] - 1 * US_BUSINESS_DAY
The code does the job. But the process is very slow for a large dataset. Is it possible to somehow increase the code speed?
P.S. When I run the code python gives me this warning:
PerformanceWarning: Non-vectorized DateOffset being applied to Series or DatetimeIndex
warnings.warn(