首页 > 解决方案 > 日期减法不适用于日期范围功能

问题描述

当 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)

标签: pythonpandasdatetime

解决方案


与其尝试检查行中的空值,不如在使用空值进行计算时使用 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

推荐阅读