如何解决熊猫-如何根据自定义日历计算黑白天数两个日期
我的问题很简单,所以我希望有一个简单的解决方案。我想计算两个日期之间的天数,而不是使用完整的日历天或工作日或带假日日历的工作日,而是想以日期列表的形式提供“我的日历”。
所以说我的日期是['2019-01-01','2010-01-03','2019-01-04','2019-01-10']。我希望“ 2019-01-01”和“ 2019-01-03”之间的日期返回1。在“ 2019-01-03”和“ 2019-01-10”之间的日期应该返回2。
谢谢!
# This produces standard calendar days between-
dates_list = df.index
x = 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.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',busdaycal=cal)
解决方法
这是我所拥有的最好的。我尝试了@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)
,
这是一种方法:
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。