1

I'm currently making a code for analyzing app usage stats.The dataset looks as below.

date         idx   AppName    EventType         EventTime 
2019-10-01  15835  clock    ACTIVITIY_RESUMED   15:30:02
2019-10-01  15836  clock    ACTIVITIY_PAUSED    15:30:15
2019-10-01  15837  gallery  ACTIVITY_RESUMED    15:31:42
.
. 

2019-10-02  16133  clock    ACTIVITY_RESUMED    16:40:30    

For each app usage, I coded as below and this works well. However, this gives overall used time which covers several days. I'd like to count used times by each day. Thanks in advance!

    app_names = df.AppName.unique()
    app_names = df.AppName.value_counts()
    app_usage_time = {}

for app in app_names: #
    app_resumed = False
    app_period = 0
    app_tmp_start = None
    for i, (index, data) in enumerate(df.iterrows()):
        if data.AppName == app: 
            if data.EventType == 'ACTIVITIY_RESUMED': 
                app_resumed = True
                app_tmp_start = data.datetime.timestamp()
            if (data.EventType == 'ACTIVITIY_PAUSED') and (app_tmp_start is not None):
                app_resumed = False
                app_period += data.datetime.timestamp() - app_tmp_start
                app_tmp_start = None

    app_usage_time[app] = app_period
  • can you please share which output you needs? – Akash Pagar Oct 17 '19 at 07:54
  • Currently my output gives e.g., Instagram: 204.0 (sec), which is the sum of spent time for two days. I'd like to see it separately, for example, something like this. 10-01 Instagram: xx (sec), 10-02 Instagram: xx (sec) – Hyunsoo Lee Oct 17 '19 at 07:58
  • To do this, I think I should do something with datetime library, but this seems complicated. – Hyunsoo Lee Oct 17 '19 at 08:01

1 Answers1

0

You can use both addition and subtraction on datetime objects as shown here and here.

For a day-by-day evaluation you would probably have to split the list by date first, and then calculate the sum of all single activites, each of which is given by the difference of the end time and the start time.

This discussion and this discussion may also help you.

Gerd
  • 2,568
  • 1
  • 7
  • 20