0

I'm working on a pandas dataframe, one of my column is a date (YYYYMMDD), another one is an hour (HH:MM), I would like to concatenate the two column as one timestamp or datetime64 column, to later use that column as an index (for a time series). Here is the situation :

My dataframe

Do you have any ideas? The classic pandas.to_datetime() seems to work only if the columns contain hours only, day only and year only, ... etc...

IanS
  • 15,771
  • 9
  • 60
  • 84
plalanne
  • 1,010
  • 2
  • 13
  • 30
  • what are the dtypes of the 2 columns here? If the first column is a str then `pd.to_datetime(df['date'] + df['time'], format='%Y%m%d%H:%M:%S')` should work – EdChum May 15 '17 at 09:52
  • Possible duplicate of [Combine Date and Time columns using python pandas](http://stackoverflow.com/questions/17978092/combine-date-and-time-columns-using-python-pandas) – IanS May 15 '17 at 09:55
  • I'm sorry I didn't mention : The `date` column is an `int`, the `hour` column is already a `datetime.time` – plalanne May 15 '17 at 12:22

1 Answers1

0

Setup

df
Out[1735]: 
     id      date      hour  other
0  1820  20140423  19:00:00      8
1  4814  20140424  08:20:00     22

Solution

import datetime as dt
#convert date and hour to str, concatenate them and then convert them to datetime format.
df['new_date'] = df[['date','hour']].astype(str).apply(lambda x: dt.datetime.strptime(x.date + x.hour, '%Y%m%d%H:%M:%S'), axis=1)

df
Out[1756]: 
     id      date      hour  other            new_date
0  1820  20140423  19:00:00      8 2014-04-23 19:00:00
1  4814  20140424  08:20:00     22 2014-04-24 08:20:00
Allen Qin
  • 19,507
  • 8
  • 51
  • 67