'Pandas - Generate date column from other TimeStamp columns

I've a DataFrame as below -

date time url username ip
2022-02-22 1900-01-01 00:00:02 /test/fun.aspx mitesh 192.168.0.25
2022-02-23 1900-01-01 01:00:02 /test/run.aspx steve 192.168.0.15
2022-02-24 1900-01-01 01:00:02 /test/bun.aspx azim 192.168.0.11

I need a new datetime column constructed by taking year, month and day from the date column and hour, minute, second from the time column.

New to pandas. Please advice.



Solution 1:[1]

There are many ways, the most efficient is probably to concatenate the date and time to feed to pandas.to_datetime:

df['datetime'] = pd.to_datetime(df['date']+' '
                               +df['time'].str.extract('(\S+)$',
                                                       expand=False)
                                )

output:

         date                 time             url username            ip            datetime
0  2022-02-22  1900-01-01 00:00:02  /test/fun.aspx   mitesh  192.168.0.25 2022-02-22 00:00:02
1  2022-02-23  1900-01-01 01:00:02  /test/run.aspx    steve  192.168.0.15 2022-02-23 01:00:02
2  2022-02-24  1900-01-01 01:00:02  /test/bun.aspx     azim  192.168.0.11 2022-02-24 01:00:02

Alternative to extract the time:

df['time'].str.split().str[-1]

Full date operations alternative:

pd.to_datetime(df['date']) + pd.to_datetime(df['time']).sub(pd.Timestamp('1900-01-01'))

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 mozway