np.where 在 pandas 中比较时间时表现不佳

np.where acting up while comparing time in pandas

我有以下代码:

np.where((final_df_SDC['Start Time']>= final_df_SDC['Repayment_Time']) & (final_df_SDC['End Time'] <= final_df_SDC['Repayment_Time']),"OCC","-")

这是给我的错误:TypeError: '>=' not supported between instances of 'datetime.time' and 'str'

但是,当我检查数据类型时

final_df_SDC['Start Time'].dtype
final_df_SDC['Repayment_Time'].dtype
final_df_SDC['End Time'].dtype

我得到:dtype('O')

那怎么不比较呢? 我尝试转换为 pd.datetime(final_df_SDC['Start Time']) 然后它说 TypeError: <class 'datetime.time'> is not convertible to datetime 我现在很困惑。如何让所有这些时间值相互比较?

示例值:08:55:18 所有三列均采用此格式。

我转换为 astype(str)

后的样本值
Start Time  End Time    Repayment_Time
5415    2021-12-01 15:12:24 2021-12-01 15:12:48 2021-12-01 17:41:20
5482    2021-12-01 15:17:05 2021-12-01 15:18:32 2021-12-01 16:33:49
5539    2021-12-01 15:19:40 2021-12-01 15:19:42 2021-12-01 20:02:20
6011    2021-12-01 15:46:18 2021-12-01 15:47:50 2021-12-01 15:51:01
6128    2021-12-01 15:54:20 2021-12-01 15:57:28 2021-12-01 16:00:51
6177    2021-12-01 15:55:44 2021-12-01 15:56:07 2021-12-01 20:00:34
6262    2021-12-01 15:58:28 2021-12-01 15:58:39 2021-12-01 12:11:2

对于所有列,使用 to_datetime 并转换为字符串:

s = pd.to_datetime(final_df_SDC['Start Time'].astype(str))
e = pd.to_datetime(final_df_SDC['End Time'].astype(str))
r = pd.to_datetime(final_df_SDC['Repayment_TimeTime'].astype(str))

to_timedelta:

s = pd.to_timedelta(final_df_SDC['Start Time'].astype(str))
e = pd.to_timedelta(final_df_SDC['End Time'].astype(str))
r = pd.to_timedelta(final_df_SDC['Repayment_TimeTime'].astype(str))

然后将 <= 更改为 >=,例如:

np.where((s <= r) & (e >= r),"OCC","-")

或使用Series.between:

np.where(r.between(s, e),"OCC","-")