'Slicing a python dataframe from a string date column

I am trying to slice a dataframe based on their str date values. I only want to keep records of the last 15 days.

When I try this

df_master_recent_date= df_master['Datetime_col'] > (datetime.now() - pd.to_timedelta("15day"))
     

I get error : TypeError: '>' not supported between instances of 'str' and 'datetime.datetime'

When I convert the df_master['Datetime_col'] to date

df_master_recent_date = datetime.strptime(df_master['Datetime_col'],'%Y%M%D') > (datetime.now() - pd.to_timedelta("15day"))

I get error TypeError: strptime() argument 1 must be str, not Series

At the beginning of the code I convert the entire dataframe to str with this df_master= df_master.astype(str)

Date format in the column is '21/02/2022 12:21:00'

I understand the errors, and the second error likely to be resolved with a for loop which I need to figure how to construct, however, does anyone knows how to get this working without a for loop, or with the for loop if one line is not possible. Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source