Search code examples
pandastimeinteger

How to get the time from column, which contains numbers in pandas?


I want to convert the AVG column's numbers to hour(s):minute(s) form.

  • if it is more than 24h, I still want only hour:minutes form like this 73:45
  • consider these numbers as seconds

plus I want to show the AVG time of these activities on a bar. All the activities. I us this for it:

df2 = df.groupby(['Activity']).mean()

I want to see the hour:minutes

df = pd.read_csv('practice.csv')

df['AVG'] = df['AVG'].astype('datetime64[ns]')

out:

Activity AVG 0 sleep 1970-01-01 00:00:00.000000221 1 sleep 1970-01-01 00:00:00.000000201 2 swim 1970-01-01 00:00:00.000000300 3 run 1970-01-01 00:00:00.000001768 4 tv 1970-01-01 00:00:00.000000105


Solution

  • First aggregate mean, convert column to timedeltas and format it in custom function:

    df2 = df.groupby('Activity', as_index=False)['AVG'].mean()
    
    #https://stackoverflow.com/a/51102096/2901002
    def f(x):
        ts = x.total_seconds()
        hours, remainder = divmod(ts, 3600)
        minutes, seconds = divmod(remainder, 60)
        return ('{}:{:02d}').format(int(hours), int(minutes)) 
    
    
    df2['AVG'] = pd.to_timedelta(df2['AVG'], unit='s').apply(f)