大概思路:
>>> hos_date = pd.DataFrame({
"hos_name" : [1,1,3,2,3,4,5,6,5,4,6,1],
"date" : ['05-01','05-02','05-05','05-30','05-24','05-18','05-02','05-05','05-30','05-24','05-05','05-30']
})
>>> t = hos_date.where(cond = (hos_date['hos_name']==1)).dropna()
>>> t
hos_name date
0 1.0 05-01
1 1.0 05-02
11 1.0 05-30
>>> dates = sorted(['2022-'+i for i in t['date']])
>>> def diff(t1,t2):
y1,m1,d1 = list(time.strptime(t1,'%Y-%m-%d'))[:3]
y2,m2,d2 = list(time.strptime(t2,'%Y-%m-%d'))[:3]
result = datetime.date(y2,m2,d2)-datetime.date(y1,m1,d1)
return result.days
>>> [diff(dates[i],dates[i+1]) for i in range(len(dates)-1)]
[1, 28]
>>>