I have a Pandas dataframe of which each column specifies an element of a datetime (year, month, day, etc.) in integer format. I want to join these separate columns into a single datetime object, which according to the documentation is a legal operation. But after one frustrating hour I haven't yet figured out how to do this. See for instance this minimal example:
import pandas as pd
df = pd.DataFrame(
[[2011, 5, 3], [2014, 9, 13], [2022, 1, 1]],
columns=("year", "month", "day")
)
datetime = df.apply(pd.to_datetime)
Desired result:
0 2011-05-03
1 2014-09-13
2 2022-01-01
Actual result:
year ... day
0 1970-01-01 00:00:00.000002011 ... 1970-01-01 00:00:00.000000003
1 1970-01-01 00:00:00.000002014 ... 1970-01-01 00:00:00.000000013
2 1970-01-01 00:00:00.000002022 ... 1970-01-01 00:00:00.000000001
Any suggestions?