Home > Net >  How to change int to datetime in pandas?
How to change int to datetime in pandas?

Time:11-25

I am getting the following erorr TypeError: '<' not supported between instances of 'int' and 'datetime.date'

my code is np.where((Manual_Dialling['Text_Date'] > Manual_Dialling['set_date']) & (Manual_Dialling['Text_Date'] < Manual_Dialling['set_date_2']), 1, 0)

I tried converting

Manual_Dialling['Text_Date'] = Manual_Dialling['Text_Date'].apply(lambda x: pd.to_datetime(str(x), format='%Y%m%d'))

It gave me error as : ValueError: time data '2021-10-08 00:00:00' does not match format '%Y%m%d' (match)

How do I compare the three dates mentioned in np.where ?

CodePudding user response:

Convert all columns to datetimes and then create mask by Series.between:

Manual_Dialling['Text_Date'] = pd.to_datetime(Manual_Dialling['Text_Date'])
Manual_Dialling['set_date'] = pd.to_datetime(Manual_Dialling['set_date'].astype(str))
Manual_Dialling['set_date_2'] = pd.to_datetime(Manual_Dialling['set_date_2'].astype(str))

m = Manual_Dialling['Text_Date'].between(Manual_Dialling['set_date'],
                                         Manual_Dialling['set_date_2'], inclusive=False)

out = np.where(m, 1, 0)
out = m.astype(int)

CodePudding user response:

Maybe you can read pandas pandas.to_datetime. https://pandas.pydata.org/docs/reference/api/pandas.to_datetime.html#pandas-to-datetime

  • Related