0

I have a dataframe where year, month, day, hour is split into separate columns:

df.head()
    Year   Month   Day  Hour
0   2020   1       1    0
1   2020   1       1    1
2   2020   1       1    2
   ...

I'd like to add a proper datetime column to the dataframe so I end up with something along these lines:

df.head()
    Year   Month   Day  Hour datetime
0   2020   1       1    0    2020-01-01T00:00
1   2020   1       1    1    2020-01-01T01:00
2   2020   1       1    2    2020-01-01T02:00
   ...

I could add a loop that processes one row at a time, but that's not panda-esque. Here are three things that don't work (not that I expected any of them to do so):

df['datetime'] = pd.to_datetime(datetime.datetime(df['Year'], df['Month'], df['Day'], df['Hour']))
df['datetime'] = pd.to_datetime(df['Year'], df['Month'], df['Day'], df['Hour'])
df['datetime'] = pd.datetime(df['Year'], df['Month'], df['Day'], df['Hour'])
fearless_fool
  • 33,645
  • 23
  • 135
  • 217
  • @BallpointBen Yes, but I like this answer better: https://stackoverflow.com/a/26142793/558639. voting to close my question... – fearless_fool Mar 14 '21 at 17:08
  • This works like you expected the last 3 examples to: df['datetime'] = pd.to_datetime({"year": df['Year'], "month": df['Month'], "day": df['Day'], "hour": df['Hour']}) – Kade Mar 30 '22 at 21:52

0 Answers0