Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

np.where acting up while comparing time in pandas

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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","-")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading