python - 日期减法不适用于日期范围功能
问题描述
当 T1 有有效值时,我尝试减去两个日期时间,T2 获得差异。差异是通过仅考虑日期之间的工作日而不考虑周六和周日来计算的。
代码仅适用于某些行。这怎么能解决。
T1 T2 Diff
0 2017-12-04 05:48:15 2018-01-05 12:15:22 NaN
1 2017-07-10 08:23:11 2018-01-05 15:28:22 NaN
2 2017-09-11 05:10:37 2018-01-29 15:02:07 NaN
3 2017-12-21 04:51:12 2018-01-29 16:06:43 NaN
4 2017-10-13 10:11:00 2018-02-22 16:19:04 NaN
5 2017-09-28 21:44:31 2018-01-29 12:42:02 NaN
6 2018-01-23 20:00:58 2018-01-29 14:40:33 NaN
7 2017-11-28 15:39:38 2018-01-31 11:57:04 NaN
8 2017-12-21 12:44:00 2018-01-31 13:12:37 30.0
9 2017-11-09 05:52:29 2018-01-22 11:42:01 53.0
10 2018-02-12 04:21:08 NaT NaN
df[['T1','T2','diff']].dtypes
T1 datetime64[ns]
T2 datetime64[ns]
diff float64
df['T1'] = pd.to_datetime(df['T1'])
df['T2'] = pd.to_datetime(df['t2'])
def fun(row):
if row.isnull().any():
return np.nan
ts = pd.DataFrame(pd.date_range(row["T1"],row["T2"]), columns=["date"])
ts["dow"] = ts["date"].dt.weekday
return (ts["dow"]<5).sum()
df["diff"] = df.apply(lambda x: fun(x), axis=1)
解决方案
与其尝试检查行中的空值,不如在使用空值进行计算时使用 try/except 来捕获错误。
这对我有用,我认为,你想要的方式。
import pandas as pd
import numpy as np
df = pd.read_csv("/home/rightmire/Downloads/test.csv", sep=",")
# df = df[["m1","m2"]]
print(df)
# print(df[['m1','m2']].dtypes)
df['m1'] = pd.to_datetime(df['m1'])
df['m2'] = pd.to_datetime(df['m2'])
print(df[['m1','m2']].dtypes)
#for index, row in df.iterrows():
def fun(row):
try:
ts = pd.DataFrame(pd.date_range(row["m1"],row["m2"]), columns=["date"])
# print(ts)
ts["dow"] = ts["date"].dt.weekday
result = (ts["dow"]<5).sum()
# print("Result = ", result)
return result
except Exception as e:
# print("ERROR:{}".format(str(e)))
result = np.nan
# print("Result = ", result)
return result
df["diff"] = df.apply(lambda x: fun(x), axis=1)
print(df["diff"])
感兴趣的输出:
dtype: object
0 275.0
1 147.0
2 58.0
3 28.0
4 95.0
5 87.0
6 4.0
7 46.0
8 30.0
9 96.0
10 NaN
11 27.0
12 170.0
13 158.0
14 79.0
Name: diff, dtype: float64
推荐阅读
- android - 将 targetSdkVersion 更新为 27 后操作 ACTION_POWER_DISCONNECTED 不起作用
- css - Magnific PopUp - 自定义标题 Css
- python-3.x - QPainter drawEllipse() 函数的撤消功能
- java - 如何使用相同的密钥更新两个首选项的摘要?
- html - 如何使用提交按钮发送电子邮件,触发发布请求
- c# - 在绘图上使用 graphics.ScaleTransform
- javascript - 将带偏移的纹理从 Three.js 场景导出到 GLTF
- css - 无法在 CSS 中添加 bgimage
- ios - 在 uitableview 重新加载时显示活动指示器
- node.js - 从 Firebase Cloud Functions 访问 Auth