How to join a series of pandas type pandas.Timestamp with another series type datetime.time?

Question:

I have a series of pandas.Timestamp pandas with dates in %Y-%m-%d format and another series of datetime.time with hours.

I would like to join both series to have a date with its hour. Here a example of inputs:

from pandas import Series, Timestamp, to_datetime
import datetime

s1 = Series([Timestamp("2021-03-01"),Timestamp("2021-03-01")])
s2 = Series([datetime.time(0,0), datetime.time(0,15)])

The way I have found to do it is to first pass both series to String, join them and after that convert them to datetime but I think this is an inefficient way:

s3 = s1.dt.strftime("%Y-%m-%d") + " " + s2.astype(str)
to_datetime(s3)

Is there a more efficient way without having to do string conversions?

Answers:

You can convert the series s2 to timedelta then add that with s1

s1 + pd.to_timedelta(s2.astype(str))

0   2021-03-01 00:00:00
1   2021-03-01 00:15:00
dtype: datetime64[ns]
Answered By: Shubham Sharma
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.