I have the following code:
np.where((final_df_SDC['Start Time']>= final_df_SDC['Repayment_Time']) & (final_df_SDC['End Time'] <= final_df_SDC['Repayment_Time']),"OCC","-")
which is giving me error : TypeError: '>=' not supported between instances of 'datetime.time' and 'str'
However, when I am checking dtypes
final_df_SDC['Start Time'].dtype
final_df_SDC['Repayment_Time'].dtype
final_df_SDC['End Time'].dtype
I get : dtype('O')
Then how is it not comparing?
I tried converting to pd.datetime(final_df_SDC['Start Time'])
then it said TypeError: <class 'datetime.time'> is not convertible to datetime
I am highly confused now. How do I let all these time values compare with each other?
Sample values : 08:55:18
in this format for all three columns.
SAMPLE VALUES after I converted to 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
CodePudding user response:
For all columns use to_datetime
with convert to strings:
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))
Or 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))
Then change <=
with >=
like:
np.where((s <= r) & (e >= r),"OCC","-")
Or use Series.between
:
np.where(r.between(s, e),"OCC","-")