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)
添加回答
举报