date - Pandas - Convert HH:MM:SS.F string to seconds - Caveat : HH sometimes goes over 24H -


i have following dataframe :

**flashtalking_df =**

+--------------+--------------------------+------------------------+ | placement id | average interaction time | total interaction time | +--------------+--------------------------+------------------------+ |      2041083 | 00:01:04.12182           | 24:29:27.500           | |      2041083 | 00:00:54.75043           | 52:31:48.89108         | +--------------+--------------------------+------------------------+ 

where 00:01:04.12182 = hh:mm:ss.f

i need convert both columns, average interaction time, , total interaction time seconds.

the problem total interaction time goes on 24h.

i found following code works part. however, when total interaction time goes on 24h, gives me

valueerror: time data '24:29:27.500' not match format '%h:%m:%s.%f'

this function using, grabbed stack overflow question, both average interaction time , total interaction time:

flashtalking_df['time'] = flashtalking_df['total interaction time'].apply(lambda x: datetime.datetime.strptime(x,'%h:%m:%s.%f')) flashtalking_df['timedelta'] = flashtalking_df['time'] - datetime.datetime.strptime('00:00:00.00000','%h:%m:%s.%f') flashtalking_df['total interaction time'] = flashtalking_df['timedelta'].apply(lambda x: x / np.timedelta64(1, 's')) 

if there's easier way, please let me know.

thank help

i think need first convert to_timedelta , seconds astype:

df['average interaction time'] = pd.to_timedelta(df['average interaction time'])                                    .astype('timedelta64[s]')                                    .astype(int)  df['total interaction time'] =   pd.to_timedelta(df['total interaction time'])                                    .astype('timedelta64[s]')                                    .astype(int)                                    .map('{:,.2f}'.format) print (df)    placement id  average interaction time total interaction time 0       2041083                        64              88,167.00 1       2041083                        54             189,108.00 

solution total_seconds, thank nickilmaveli:

df['average interaction time'] = pd.to_timedelta(df['average interaction time'])                                    .dt.total_seconds()                                    .map('{:,.2f}'.format) df['total interaction time'] =   pd.to_timedelta(df['total interaction time'])                                    .dt.total_seconds()                                    .map('{:,.2f}'.format)  print (df)       placement id average interaction time total interaction time 0       2041083                    64.12              88,167.50 1       2041083                    54.75             189,108.89 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -