亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

熊貓-如何根據自定義日歷計算兩個日期的黑白天數

熊貓-如何根據自定義日歷計算兩個日期的黑白天數

鳳凰求蠱 2023-05-16 15:51:48
我的問題很簡單,所以我希望有一個簡單的解決方案。我想計算兩個日期之間的天數,而不是使用完整的日歷日或工作日或帶假日日歷的工作日,我想以日期列表的形式提供我自己的“日歷”。假設我的日期是 ['2019-01-01', '2010-01-03', '2019-01-04', '2019-01-10']。我希望“2019-01-01”和“2019-01-03”之間的日期返回 1?!?019-01-03”和“2019-01-10”之間的日期應返回 2。謝謝!# This produces standard calendar days between-dates_list = df.indexx = dates_list[1] - dates_list[0]# This produces days according to numpy businessdaycal:cal = np.busdaycalendar()x = np.busday_count('2019-01-01', '2019-01-03', busdaycal=cal)# This works, but requires multiple steps so prob inefficient:dates_list = df.indexall_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)
查看完整描述

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)


查看完整回答
反對 回復 2023-05-16
?
人到中年有點甜

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)


查看完整回答
反對 回復 2023-05-16
  • 2 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號