4

I need to offset a DateTimeIndex index by a CustomBusinessDay, a trading day offset, which I instantiate like so:

import pandas_market_calendars as mcal
TCal = mcal.get_calendar('NYSE')
from pandas.tseries.offsets import CustomBusinessDay
TDay = CustomBusinessDay(calendar=TCal, holidays = TCal.holidays().holidays)

Applying this offset to a 'date' level of a MultiIndex:

df.index.set_levels(df.index.levels[0] + TDay, level=0)

yields the non-vectorized DateOffset being applied warning:

python3.8/site-packages/pandas/core/arrays/datetimes.py:692: PerformanceWarning: Non-vectorized DateOffset being applied to Series or DatetimeIndex

Is there a way to vectorize my CustomBusinessDay, or produce an analogous date offset that is vectorized? Thanks!

UPDATE: Here's the code that exemplifies the dataframe I am working with:

import numpy as np
import pandas as pd

dates = list(map(pd.to_datetime, ['2020-07-16', '2020-07-17']))
strings = ['A', 'B', 'C', 'D']
df = pd.DataFrame(
    index=pd.MultiIndex.from_product([dates, strings], names=['date', 'string']),
    data=np.random.uniform(size=(len(dates) * len(strings), 2))
)
nijshar28
  • 173
  • 7
  • 1
    could you post an sample df? – Ezer K Jul 27 '20 at 20:01
  • Unfortunately, no. But the df has float64 data, and the index is a MultiIndex with two levels where the outer level is a DateTimeIndex and an inner level has strings. – nijshar28 Jul 27 '20 at 23:14
  • @EzerK Sorry about that I think I misunderstood what you meant by posting a sample df. I just uploaded the question with code that produces a dataframe similar to the one I am working on. Thanks! – nijshar28 Jul 29 '20 at 05:13

0 Answers0