Deleting pandas string value from all rows in a column and convert it to DateTime

I would like to delete "- 01" , "- 02", "- 03",etc. in the Hours column and then concatenate date and Hours column as a DateTime. Could you please help me?

Thanks in advance

enter image description here


df['new_date'] = df['date'].astype(str) + ' ' + df['Volume'].astype(str).replace(r'\s-\s\d+$','', regex=True)
df['new_date'] = pd.to_datetime(df['new_date'])

This creates a new column. You could also replace the date column and drop the hours if you'd prefer.

Output:

0   2022-01-03 00:00:00
1   2022-01-04 01:00:00
2   2022-01-05 02:00:00
3   2022-01-06 03:00:00
4   2022-01-07 04:00:00
5   2022-01-10 05:00:00
6   2022-01-11 06:00:00
7   2022-01-12 07:00:00
8   2022-01-13 08:00:00
9   2022-01-14 09:00:00
Name: new_date, dtype: datetime64[ns]

Use str.cat .to join the two columns. str.partition to partition should help you eliminate unwanted text from hours. Code below

df= df.assign(newdate=df['date'].str.cat(df['Hours'].str.partition('-')[0], sep=" "))

 

    date      Hours        newdate
0  01/01/2020  01-02  01/01/2020 01
1  01/01/2020  01-03  01/01/2020 01
2  01/01/2020  01-02  01/01/2020 01