📜  Python - 获取最近的前一个工作日

📅  最后修改于: 2022-05-13 01:55:35.069000             🧑  作者: Mango

Python - 获取最近的前一个工作日

给定一个日期,任务是编写一个Python程序,从给定日期获取最近的前一个工作日。

例子:

方法一:使用timedelta() + weekday()

在这里,我们执行以下任务:在星期一的情况下减去 3,在星期日的情况下减去 2,在所有其他日期的情况下减去 1。 timedelta() 执行减法任务,条件语句检查工作日。

Python3
# Python3 code to demonstrate working of
# Last business day
# using timedelta() + conditional statements + weekday()
from datetime import datetime, timedelta
  
# initializing dates
test_date = datetime(2020, 1, 31)
               
# printing original date
print("The original date is : " + str(test_date))
  
# getting difference
diff = 1
if test_date.weekday() == 0:
    diff = 3
elif test_date.weekday() == 6:
    diff = 2
else :
    diff = 1
      
# subtracting diff 
res = test_date - timedelta(days=diff)
  
# printing result
print("Last business day : " + str(res))


Python3
# Python3 code to demonstrate working of
# Last business day
# using max() + % operator + timedelta() 
from datetime import datetime, timedelta
  
# initializing dates
test_date = datetime(2020, 1, 31)
               
# printing original date
print("The original date is : " + str(test_date))
  
# getting difference
# using max() to get differences 
diff = max(1, (test_date.weekday() + 6) % 7 - 3)
      
# subtracting diff 
res = test_date - timedelta(days=diff)
  
# printing result
print("Last business day : " + str(res))


Python3
# Python3 code to demonstrate working of
# Last business day
# using pd.tseries.offsets.BusinessDay(n)
import pandas as pd
from datetime import datetime
  
# initializing dates
test_date = datetime(2020, 2, 3)
  
# printing original date
print("The original date is : " + str(test_date))
  
# Creating Timestamp
ts = pd.Timestamp(str(test_date))
  
# Create an offset of 1 Business days
offset = pd.tseries.offsets.BusinessDay(n=1)
  
# getting result by subtracting offset
res = test_date - offset
  
# printing result
print("Last business day : " + str(res))


输出:

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00

方法 2:使用max() + %运算符+ timedelta()  

以类似的方式执行任务,唯一的区别是计算差异变化以获得 max() 和 % 运算符。



蟒蛇3

# Python3 code to demonstrate working of
# Last business day
# using max() + % operator + timedelta() 
from datetime import datetime, timedelta
  
# initializing dates
test_date = datetime(2020, 1, 31)
               
# printing original date
print("The original date is : " + str(test_date))
  
# getting difference
# using max() to get differences 
diff = max(1, (test_date.weekday() + 6) % 7 - 3)
      
# subtracting diff 
res = test_date - timedelta(days=diff)
  
# printing result
print("Last business day : " + str(res))

输出:

The original date is : 2020-01-31 00:00:00
Last business day : 2020-01-30 00:00:00

方法 3:使用pd.tseries.offsets.BusinessDay(n)

在此,我们创建一个工作日偏移量 1 天,并从初始化的日期中减去。这将根据需要返回前一个工作日。

蟒蛇3

# Python3 code to demonstrate working of
# Last business day
# using pd.tseries.offsets.BusinessDay(n)
import pandas as pd
from datetime import datetime
  
# initializing dates
test_date = datetime(2020, 2, 3)
  
# printing original date
print("The original date is : " + str(test_date))
  
# Creating Timestamp
ts = pd.Timestamp(str(test_date))
  
# Create an offset of 1 Business days
offset = pd.tseries.offsets.BusinessDay(n=1)
  
# getting result by subtracting offset
res = test_date - offset
  
# printing result
print("Last business day : " + str(res))

输出 :

The original date is : 2020-02-03 00:00:00
Last business day : 2020-01-31 00:00:00