繁体   English   中英

Pandas:如果日期在周日或周六,如何将周五作为工作日返回?

[英]Pandas : How to return Friday as business day if date is falling on Sunday or Saturday?

如果我的日期是星期六或星期日,我会尝试返回工作日日期。 以下是代码,但似乎不完整。 请建议。

import holidays
import datetime

enter_date = '2020-10-30'
enter_date = pd.to_datetime(enter_date)
before_date = enter_date - datetime.timedelta(days=5)
print(before_date)

使用pd.DatetimeIndex.dayofweek属性(请参阅文档)。 它将每周的每一天从星期一到星期日映射到range(7)

例如:

import pandas as pd


saturday = pd.to_datetime('2020-12-12')
sunday = pd.to_datetime('2020-12-13')

def func(date):
    if date.dayofweek == 5: 
        date = date - pd.Timedelta('1 day')  # SATURDAY
    elif date.dayofweek == 6: 
        date = date - pd.Timedelta('2 day')  # SUNDAY
    return date


print(func(saturday))  # 2020-12-11 00:00:00
print(func(sunday))  # 2020-12-11 00:00:00

我认为您正在寻找一种解决假期和周末问题的解决方案。

看看这是否能解决您的问题。

没有 pandas 的解决方案(用于循环和日期时间)

from datetime import datetime, timedelta
import calendar, holidays

#weekday() starts with 0 and ends with 6.
#0 is Mon, 4 is Fri, 5 is Sat, 6 is Sun
#check if weekday is Sat or Sun (> 4)
#if Sat or Sun, subtract a day until you get to Fri
#if day is a holiday, subtract a day until you get to a working day

for enter_date in ['2020-12-09','2020-12-12','2020-12-25', '2020-12-27', '2021-01-18']:
    enter_date = datetime.strptime(enter_date, '%Y-%m-%d')
    print ('\nGiven date :', enter_date.date(), 'is a',calendar.day_name[enter_date.weekday()])

    while enter_date in holidays.US() or enter_date.weekday() > 4:
        enter_date = enter_date - timedelta(days=1)

    print ('The business working day is',enter_date.date(), 'and is a', calendar.day_name[enter_date.weekday()])

其output如下:

Given date : 2020-12-09 is a Wednesday
The business working day is 2020-12-09 and is a Wednesday

Given date : 2020-12-12 is a Saturday
The business working day is 2020-12-11 and is a Friday

Given date : 2020-12-25 is a Friday
The business working day is 2020-12-24 and is a Thursday

Given date : 2020-12-27 is a Sunday
The business working day is 2020-12-24 and is a Thursday

Given date : 2021-01-18 is a Monday
The business working day is 2021-01-15 and is a Friday

请注意:

  • 2020 年 9 月 12 日是星期三,没有进行任何更改
  • 2020 年 12 月 12 日是星期六,已更改为 2020 年 11 月 12 日星期五
  • 2020 年 12 月 25 日是星期五和节假日。 它已更改为 2020 年 12 月 24 日星期四
  • 2020 年 12 月 27 日是星期日,2020 年 12 月 25 日是星期五,但假期。 它已更改为 2020 年 12 月 24 日星期四
  • 2021 年 1 月 18 日是星期一和节假日。 它已更改为 2021 年 1 月 15 日星期五

解决方案包括 pandas

这是一个包含 pandas 的解决方案:

from datetime import datetime, timedelta
import calendar, holidays
import pandas as pd

def business_day(enter_date):
    while enter_date in holidays.US() or enter_date.weekday() > 4:
        enter_date = enter_date - timedelta(days=1)

    return enter_date

df = pd.DataFrame({'enter_date':['2020-12-09','2020-12-12','2020-12-25', '2020-12-27','2021-01-18']})
df['enter_date'] = pd.to_datetime(df['enter_date'])
df['business_date'] = df['enter_date'].apply(lambda x: business_day(x))
print (df)

output 将是:

  enter_date business_date
0 2020-12-09    2020-12-09
1 2020-12-12    2020-12-11
2 2020-12-25    2020-12-24
3 2020-12-27    2020-12-24
4 2021-01-18    2021-01-15

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM