Home > Back-end >  pandas - combine time and date from two dataframe columns to a datetime column
pandas - combine time and date from two dataframe columns to a datetime column

Time:03-23

This is a follow up question of the accepted solution in here.

I have a pandas dataframe:

In one column 'time' is the time stored in the following format: 'HHMMSS' (e.g. 203412 means 20:34:12).

In another column 'date' the date is stored in the following format: 'YYmmdd' (e.g 200712 means 2020-07-12). YY represents the addon to the year 2000.

Example:

import pandas as pd

data = {'time': ['123455', '000010', '100000'],
        'date': ['200712', '210601', '190610']}

df = pd.DataFrame(data)

print(df)

#     time    date
#0  123455  200712
#1  000010  210601
#2  100000  190610

I need a third column which contains the combined datetime format (e.g. 2020-07-12 12:34:55) of the two other columns. So far, I can only modify the time but I do not know how to add the date.

df['datetime'] = pd.to_datetime(df['time'], format='%H%M%S')

print(df)

#     time    date            datetime
#0  123455  200712 1900-01-01 12:34:55
#1  000010  210601 1900-01-01 00:00:10
#2  100000  190610 1900-01-01 10:00:00

How can I add in column df['datetime'] the date from column df['date'], so that the dataframe is:

     time    date            datetime
0  123455  200712 2020-07-12 12:34:55
1  000010  210601 2021-06-01 00:00:10
2  100000  190610 2019-06-10 10:00:00

I found this question, but I am not exactly sure how to use it for my purpose.

CodePudding user response:

You can join columns first and then specify formar:

df['datetime'] = pd.to_datetime(df['date']   df['time'], format='%y%m%d%H%M%S')
print(df)
     time    date            datetime
0  123455  200712 2020-07-12 12:34:55
1  000010  210601 2021-06-01 00:00:10
2  100000  190610 2019-06-10 10:00:00

If possible integer columns:

df['datetime'] = pd.to_datetime(df['date'].astype(str)   df['time'].astype(str), format='%y%m%d%H%M%S')
  • Related