Merge 'Date' and 'Time' columns with specific format into 'Date_Time' column Pandas

Question:

I have a df with Time and Date columns in this specific format.

Time    Date
213919  220806
220905  220806
225351  220806
4101    220806
12037   220806

The format of Time is: hhmmss

The format of Time is a bit strange, the reading must begin from the end and in order to complete the 6 digits, 0 must be added to the beginning, for example 4101 refers to 00:41:01, whereas, 12037 refers to 01:20:37

The format of Date is: yymmdd

I would like to add a new column to my df, Date_Time which looks like this:

Time    Date    Date_Time    
213919  220806  06/08/2022 21:39:19
220905  220806  06/08/2022 22:09:05
225351  220806  06/08/2022 22:53:51
4101    220806  06/08/2022 00:41:01
12037   220806  06/08/2022 01:20:37
Asked By: serdar_bay

||

Answers:

You can combine the two columns as string with help of str.zfill, then pass to to_datetime and dt.strftime with your custom format:

df['Date_Time'] = (pd.to_datetime(df['Date'].astype(str)
                                  +df['Time'].astype(str).str.zfill(6),
                                  format='%y%m%d%H%M%S')
                     .dt.strftime('%d/%m/%Y %H:%M:%S')
                  )

Output:

     Time    Date            Date_Time
0  213919  220806  06/08/2022 21:39:19
1  220905  220806  06/08/2022 22:09:05
2  225351  220806  06/08/2022 22:53:51
3    4101  220806  06/08/2022 00:41:01
4   12037  220806  06/08/2022 01:20:37
Answered By: mozway

If you load your data from a csv file or an excel file, you can use:

df = pd.read_csv('data.csv', parse_dates=[['Date', 'Time']])
print(df)

# Output
            Date_Time
0 2006-08-22 21:39:19
1 2006-08-22 22:09:05
2 2006-08-22 22:53:51
3 2006-08-22 00:41:01
4 2006-08-22 01:20:37

If you don’t use parse_dates parameter, you will have:

df = pd.read_csv('data.csv')
print(df)

# Output
     Time    Date
0  213919  220806
1  220905  220806
2  225351  220806
3    4101  220806
4   12037  220806

Input data file:

Time,Date
213919,220806
220905,220806
225351,220806
004101,220806
012037,220806

Update

The numbers are not padded in the original csv

def pad_time(x):
    return x.zfill(6)
df = pd.read_csv('data.csv', parse_dates=[['Date', 'Time']], converters={'Time': pad_time})
print(df)

# Output
            Date_Time
0 2006-08-22 21:39:19
1 2006-08-22 22:09:05
2 2006-08-22 22:53:51
3 2006-08-22 00:41:01
4 2006-08-22 01:20:37
Answered By: Corralien