在DataFrame中获取上一个工作日

时间:2018-10-10 14:06:27

标签: python python-3.x pandas datetime calendar

我有一个带有两列的DataFrame,一个日期和一个类别。我想根据以下规则创建一个新的日期列:如果category为B,则该值应为最接近该日期的营业日(仅来自过去或日期本身),否则为该日期的值列本身。

我将工作日定义为不在周末或不在以下最小示例中定义的列表holidays中的任何一天。

请考虑以下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

如何获得第三列,其值是下面列出的值?

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

我创建了一个函数,如果有帮助的话,该函数可以查找使用列表时的最后一个工作日。

# 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

5 个答案:

答案 0 :(得分:3)

您已经很接近了:

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
        ]
    )

然后将其应用于整个数据框:

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

答案 1 :(得分:3)

通过使用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

答案 2 :(得分:2)

您可以在pd.merge_asof的子集中使用category == 'B'来设置所有非假日工作日,并为所有其他类别分配日期。设置allow_exact_matches=False以确保您与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')

输出:

         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

答案 3 :(得分:2)

Pandas支持通过Custom Business Days提供您自己的假期。

此解决方案的好处是它无缝支持相邻的假期;例如某些地区的节礼日和圣诞节。

# 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

编辑:根据您的评论,“我只是意识到我并没有确切地问自己想要什么。我想找到上一个工作日”,您可以使用:

df['day'] -= bday

答案 4 :(得分:1)

您只需确定工作日并根据类别选择最接近的工作日即可。

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])

输出

    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