我必须计算数据框中两个日期字段之间的日期差。计算时必须遵循以下条件。
1) Include only count working days doing subraction i.e exclude saturday and sunday while doing the calculations.
2) If Date 1 is null populate the difference as Null
3)If Date 2 is null populate the difference as Null
**Sample Data**
Date1 Date2
1/10/2019 17:24 15/2/2019 17:41
8/1/2019 12:04 11/1/2019 10:02
16/1/2019 11:30
21/1/2019 16:30
23/1/2019 20:35 30/1/2019 12:35
7/1/2019 17:58 8/1/2019 14:18
24/1/2019 11:50 25/1/2019 8:00
1/3/2019 13:00
7/1/2019 12:55 8/1/2019 15:03
我尝试了以下代码
data['date_diff'] = ((pd.to_datetime(data['date2']) - pd.to_datetime(data['date1'])) .dt
但是当值为空时会中断。如何解决此问题
答案 0 :(得分:0)
可以肯定,还有一些更优雅的方法,但是您可以考虑从中使用
import pandas as pd
import numpy as np
date1 = "1/10/2019 17:24,8/1/2019 12:04,,21/1/2019 16:30, 23/1/2019 20:35,,7/1/2019 17:58,24/1/2019 11:50,,7/1/2019 12:55"
date2 = "15/2/2019 17:41,11/1/2019 10:02,16/1/2019 11:30,,30/1/2019 12:35,,8/1/2019 14:18,25/1/2019 8:00,1/3/2019 13:00,8/1/2019 15:03"
df = pd.DataFrame({"date1":date1.split(','),
"date2":date2.split(',')})
for col in df.columns:
df[col] = pd.to_datetime(df[col])
def fun(row):
if row.isnull().any():
return np.nan
ts = pd.DataFrame(pd.date_range(row["date1"],row["date2"]), columns=["date"])
ts["dow"] = ts["date"].dt.weekday
return (ts["dow"]<5).sum()
df["diff"] = df.apply(lambda x: fun(x), axis=1)