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?
CodePudding user response:
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]