-1

I have a pandas dataframe where date and hour is in two different columns as shown below - I want to concat these two columns to have a new datatime column where I can apply pandas window/shift functions. Please share your views.

        date          hour
0       20190409       0
1       20190409       0
2       20190409       0
3       20190409       0
4       20190409       0
Aritra Sen
  • 143
  • 1
  • 11
  • I'm assuming you meant your data to be something like this: `pd.DataFrame(columns=['date', 'hour'], data=[[20190409,0], [20190409, 1], [20190409,2], [20190409, 3]])` – Itamar Mushkin May 23 '19 at 10:35

1 Answers1

1

Use pandas.to_datetime and pd.to_timedelta and add them together:

df['datetime'] = pd.to_datetime(df['date'], format='%Y%m%d') + pd.to_timedelta(df['hour'], unit='H')
Chris Adams
  • 18,389
  • 4
  • 22
  • 39