Search code examples
pythonpandasstringtext-mining

Combining two columns to create a datetime object


I would need to create a datetime by combining two columns in a dataframe. My original dataset has these columns:

Date                Time
05/29/2020         00:12
05/29/2020         00:32
05/28/2020         00.59

I would like to create a new column which combines Date + Time, i.e. which creates a datetime object:

Date_time
05/22/2020 00:12
05/22/2020 00:32
05/28/2020 00:59

I used

df['Time'] = df['Time'].astype('datetime64[ns]') 

but the output gave me today's date plus values from column Time 2020-05-29 00:22:00. How could I get the right date by using the columns Date and Time above?


Solution

  • You can try

    df['Date_time']=pd.to_datetime(df.Date+ ' '+df.Time, format='%m/%d/%Y %H:%M:%S')