Cerramos los Ojos
Cerramos los Ojos

Reputation: 133

Get previous business day in a DataFrame

I have a DataFrame with two columns, a date and a category. I want to create a new date column according to the rule: if category is B then the value should the business day closest to the date (only from the past or the day itself), else it's the value of the date column itself.

I define business day as any day which isn't on a weekend, nor present in the list holidays defined in the minimal example below.

Please consider the following DataFrame df:

import datetime as dt
import pandas as pd
from IPython.display import display

holidays = [dt.datetime(2018, 10, 11)]
df = pd.DataFrame({"day": ["2018-10-10", "2018-10-11", "2018-10-12",
                       "2018-10-13", "2018-10-14", "2018-10-15"
                      ],
               "category":["A", "B", "C", "B", "C", "A"]
              }
)

df["day"] = pd.to_datetime(df.day, format="%Y-%m-%d")
display(df)

         day category
0 2018-10-10        A
1 2018-10-11        B
2 2018-10-12        C
3 2018-10-13        B
4 2018-10-14        C
5 2018-10-15        A

How do I get a third column whose values are the ones listed below?

2018-10-10
2018-10-10
2018-10-12
2018-10-12
2018-10-14
2018-10-15

I have a created a function that finds the last business day when working with lists, if that's any help.

# creates a list whose elements are all days in the years 2017, 2018 and 2019
days = [dt.datetime(2017, 1 , 1) + dt.timedelta(k) for k in range(365*3)]


def lastt_bus_day(date):
    return max(
        [d for d in days if d.weekday() not in [5, 6]
                            and d not in holidays
                            and d <= date
        ]
    )

for d in df.day:
    print(last_bus_day(d))
#prints
2018-10-10 00:00:00
2018-10-10 00:00:00
2018-10-12 00:00:00
2018-10-12 00:00:00
2018-10-12 00:00:00
2018-10-15 00:00:00

Upvotes: 13

Views: 4980

Answers (5)

jpp
jpp

Reputation: 164623

Pandas supports providing your own holidays via Custom Business Days.

The benefit of this solution is it supports adjacent holidays seamlessly; for example, Boxing Day & Christmas in some regions.

# define custom business days
weekmask = 'Mon Tue Wed Thu Fri'
holidays = ['2018-10-11']

bday = pd.tseries.offsets.CustomBusinessDay(holidays=holidays, weekmask=weekmask)

# construct mask to identify when days must be sutracted
m1 = df['category'] == 'B'
m2 = df['day'].dt.weekday.isin([5, 6]) | df['day'].isin(holidays)

# apply conditional logic
df['day'] = np.where(m1 & m2, df['day'] - bday, df['day'])

print(df)

  category        day
0        A 2018-10-10
1        B 2018-10-10
2        C 2018-10-12
3        B 2018-10-12
4        C 2018-10-14
5        A 2018-10-15

Edit: On the basis of your comment, "I just realised I didn't ask exactly what I wanted. I want to find the previous business day", you can simply use:

df['day'] -= bday

Upvotes: 3

ALollz
ALollz

Reputation: 59519

You can use pd.merge_asof on the subset where category == 'B' with all non-holiday business days and assign the date for all other categories. Set allow_exact_matches=False to ensure you don't match with the same day for B.

import pandas as pd

mask = df.category == 'B'

# DataFrame of all non-holiday days
df_days = pd.DataFrame(days, columns=['day'])
df_days = df_days.loc[(df_days.day.dt.weekday<5) & ~df_days.day.isin(holidays)]

dfb = pd.merge_asof(
        df.loc[mask], 
        df_days.assign(new_day=df_days.day), 
        on='day', 
        direction='backward',
        allow_exact_matches=False)

dfnb = df.assign(new_day = df.day)[~mask]

pd.concat([dfnb, dfb], ignore_index=True).sort_values('day')

Output:

         day category    new_day
0 2018-10-10        A 2018-10-10
4 2018-10-11        B 2018-10-10
1 2018-10-12        C 2018-10-12
5 2018-10-13        B 2018-10-12
2 2018-10-14        C 2018-10-14
3 2018-10-15        A 2018-10-15

Upvotes: 2

CJR
CJR

Reputation: 3985

You're pretty close already:

holidays = [dt.date(2018, 10, 11)]
days = [dt.date(2017, 1 , 1) + dt.timedelta(k) for k in range(365*3)]
def lastt_bus_day(date, format='%Y-%m-%d'):
    if not isinstance(date, dt.date):
        date = dt.datetime.strptime(date, format).date()
    return max(
        [d for d in days if d.weekday() not in [5, 6]
                            and d not in holidays
                            and d <= date
        ]
    )

Then just apply this across the dataframe:

df['business_day'] = df['day']
df['business_day'].loc[df['category'] == 'B'] = df.loc[df['category'] == 'B', 'day'].apply(lastt_bus_day)

Upvotes: 3

iDrwish
iDrwish

Reputation: 3103

You can do this simply by figuring out the business days and choosing the closest one to it based on your category.

df['day2'] = df.day
bd = pd.date_range(min(df.day), max(df.day), freq='b')
bd = bd[~bd.isin(holidays)]
df.loc[df.category=='B', 'day2'] = df.loc[df.category=='B', 'day'].apply(lambda x: bd[bd.searchsorted(x)-1])

Output

    category    day day2
0   A   2018-10-10  2018-10-10
1   B   2018-10-11  2018-10-10
2   C   2018-10-12  2018-10-12
3   B   2018-10-13  2018-10-12
4   C   2018-10-14  2018-10-14
5   A   2018-10-15  2018-10-15

Upvotes: 1

BENY
BENY

Reputation: 323226

By using pandas BDay

df.day.update(df.loc[(df.category=='B')&((df.day.dt.weekday.isin([5,6])|(df.day.isin(holidays )))),'day']-pd.tseries.offsets.BDay(1))
df
Out[22]: 
  category        day
0        A 2018-10-10
1        B 2018-10-10
2        C 2018-10-12
3        B 2018-10-12
4        C 2018-10-14
5        A 2018-10-15

Upvotes: 3

Related Questions