I have a dataframe
with a column composed by date
object and a column composed by time
object.
I have to merge the two columns.
Personally, I think it is so ugly the following solution.
Why I have to cast to str
?
I crafted my solution based on this answer
#importing all the necessary libraries
import pandas as pd
import datetime
#I have only to create a Minimal Reproducible Example
time1 = datetime.time(3,45,12)
time2 = datetime.time(3,49,12)
date1 = datetime.datetime(2020, 5, 17)
date2 = datetime.datetime(2021, 5, 17)
date_dict= {"time1":[time1,time2],"date1":[date1,date2]}
df=pd.DataFrame(date_dict)
df["TimeMerge"] = pd.to_datetime(df.date1.astype(str)+' '+df.time1.astype(str))
+
fordate
andtime
, but your currentstr
/to_datetime()
code is the fastest way to do it (even if it looks uglier) \$\endgroup\$pd.read_csv(..., parse_dates=[['date1','time1']])
would probably be the "prettiest" and fastest option \$\endgroup\$