Merge date and time column in datetime column

I have a Pandas dataframe like this; (obtained by analyzing the excel file)

| | COMPANY NAME | MEETING DATE | MEETING TIME| -----------------------------------------------------------------------| |YKSGR| YAPI KREDİ SİGORTA A.Ş. | 2013-12-16 00:00:00 |14:00:00 | |TRCAS| TURCAS PETROL A.Ş. | 2013-12-12 00:00:00 |13:30:00 | 

The MEETING DATE column is a timestamp with a representation of type Timestamp('2013-12-20 00:00:00', tz=None) and MEETING TIME is a datetime.time object with a representation of type datetime.time(14, 0)

I want to combine MEETING DATE and MEETING TIME into one column. datetime.combine seems to do what I want, however I need to somehow apply this function column by column. How can I achieve this?

+6
source share
3 answers

You can use the apply method and apply the combination as follows:

 >>> df.apply(lambda x: combine(x['MEETING DATE'], x['MEETING TIME']), axis=1) 0 2013-12-16 14:00:00 1 2013-12-12 13:00:00 
+7
source

Other solutions did not work for me, so I applied a workaround using replace instead of combine :

 def combine_date_time(df, datecol, timecol): return df.apply(lambda row: row[datecol].replace( hour=row[timecol].hour, minute=row[timecol].minute), axis=1 ) 

In your case:

 combine_date_time(df, 'MEETING DATE', 'MEETING TIME') 

It feels slow (I did not schedule it) , but it works .

UPDATE . I calculated both approaches for a relatively large dataset (> 500,000 rows) and both have the same runtime, but using combine is faster (59s for replace vs 50 seconds for combine ). Also see jezrael on this.

UPDATE2 . I tried jezrael approach:

 def combine_date_time(df, datecol, timecol): return pd.to_datetime(df[datecol].dt.date.astype(str) + ' ' + df[timecol].astype(str)) 

This approach quickly sparkles in comparison, jezrael is right. I could not measure it, but it is obvious.

+2
source

You can convert the Time column to string and then to_timedelta , then it is easy to sum both columns:

 print (type(df['MEETING DATE'].iat[0])) <class 'pandas.tslib.Timestamp'> print (type(df['MEETING TIME'].iat[0])) <class 'datetime.time'> print (df['MEETING DATE'] + pd.to_timedelta(df['MEETING TIME'].astype(str))) YKSGR 2013-12-16 14:00:00 TRCAS 2013-12-12 13:30:00 dtype: datetime64[ns] 
+1
source

Source: https://habr.com/ru/post/958167/


All Articles