getaglow
getaglow

Reputation: 343

Combine date column and time column into datetime

I have two columns (both text objects), one date, the other hour-ending.

df = pd.DataFrame({'Date' : ['2018-10-01', '2018-10-01', '2018-10-01'],
                'Hour_Ending': ['1.0', '2.0', '3.0']})

How do I add the two columns together to get a datetime object that looks like this?

2018-10-01 01:00

As a bonus, how do I change Hour_Ending to Hour_Starting?

Upvotes: 0

Views: 58

Answers (1)

BENY
BENY

Reputation: 323226

Using to_datetime and Timedelta

pd.to_datetime(df.Date)+pd.to_timedelta(df.Hour_Ending.astype('float'), unit='h')
Out[122]: 
0   2018-10-01 01:00:00
1   2018-10-01 02:00:00
2   2018-10-01 03:00:00
dtype: datetime64[ns]

Upvotes: 1

Related Questions