juanchit
juanchit

Reputation: 29

Add a series of dates with a series of hours

I have a dataframe 'df' with two columns: 'Date' and 'Time Start (UTM)' (see attached png).

I WISH a new column with the addition of both, for example:

Date                 Time Start (UTM)  New Column
2018-04-10 00:00:00  13:03:00          2018-04-10 13:03:00
2018-04-10 00:00:00  14:01:00          2018-04-10 14:01:00

dtypes are:

df['Date'] --> dtype = datetime64[ns]

df['Time Start (UTM)'] --> dtype = object

But when I see element by element I get

df['Time Start (UTM)'].ix[0]

datetime.time(13, 36)

df['Date'].ix[0]

Timestamp('2018-04-04 00:00:00')

Upvotes: 0

Views: 43

Answers (2)

jpp
jpp

Reputation: 164623

One way is to convert your Date column to str, concatenate with Time, then pass to pd.to_datetime:

df['DateTime'] = pd.to_datetime(df['Date'].astype(str) + ' ' + df['Time'])

print(df)

        Date      Time            DateTime
0 2018-04-10  13:03:00 2018-04-10 13:03:00
1 2018-04-10  14:01:00 2018-04-10 14:01:00

Upvotes: 0

Konrad
Konrad

Reputation: 562

You can use DataFrame.to_timedelta() function for this one

df['DateTime'] = df.Date + pd.to_timedelta(df.Time)
df

Output:

    Date        Time        DateTime
0   2018-04-10  13:03:00    2018-04-10 13:03:00
1   2018-04-10  14:01:00    2018-04-10 14:01:00

Upvotes: 1

Related Questions