Jeff Saltfist
Jeff Saltfist

Reputation: 943

Pandas Combine date and time columns

Having an issue making one datetime column. My df has a 'date' column with the date as a tuple, and a timestamp column. As shown below:

df.head(2)
     date       time
0    (2016,5,7) 01:01:01.125
1    (2016,5,8) 02:03:05.691  

Is there a quick way in pandas to combine these two columns into one datetime column handling for the fact that the date is in a tuple?

Thanks

Upvotes: 2

Views: 3201

Answers (1)

piRSquared
piRSquared

Reputation: 294218

Construct a dataframe from the tuples to pass to pd.to_datetime
Convert the 'time' column to time deltas with pd.to_timedelta

date = pd.to_datetime(
    pd.DataFrame(
        df.date.tolist(),
        columns=['year', 'month', 'day']
    )
)

time = pd.to_timedelta(df.time)

date + time

0   2016-05-07 01:01:01.125
1   2016-05-08 02:03:05.691
dtype: datetime64[ns]

Upvotes: 3

Related Questions