2 回答

TA貢獻1772條經驗 獲得超5個贊
這是我所擁有的最好的。我嘗試了@RichieV 的 pd.Series.between() 和以下方法,速度更快:
dates_list = df.index
all_dates = pd.date_range(dates_list[0], dates_list[1])
holidays = [d.date() for d in all_dates if d not in dates_list]
cal = np.busdaycalendar(holidays=holidays)
x = np.busday_count('2019-01-01', '2019-01-03', busdaycal=cal)

TA貢獻1895條經驗 獲得超7個贊
這是一種方法:
import pandas as pd
my_cal = pd.Series(
data=1,
index=pd.date_range(start='2020-01-01', periods=100, freq='D'))
# set your own 'holidays' to zero here
# cumulative sum won't count your custom 'holidays'
my_cal = my_cal.cumsum()
# use like this (this could be wrapped in a function)
days_between = my_cal['2020-01-03'] - my_cal['2020-01-01']
print(days_between)
添加回答
舉報