0

Hi I have a problem with average time from list of times to each test.

output:

test1 ['0:02:30.000000', '0:02:28.000000', '0:02:31.000000', '0:02:33.000000', '0:02:34.000000', '0:02:37.000000', '0:02:30.000000', '0:02:30.000000']

test2 ['0:23:25.789000', '0:23:04.653000']

test3 ['0:32:21.873000', '0:32:33.752000', '0:33:30.383000', '0:32:29.835000']

expected result:

test_name = average time on example:

test2 = 0:23:15:442000

I tried to convert str to time:

time == dt.strptime(str(time), '%H:%M:%S.%f')

but then i cannot sum all times and then divide to length of list.

Could you please help me with this issue?

def average_time(file_path):
    df = pd.DataFrame(pd.read_excel(file_path))
    test_names_list = collections.Counter(df.iloc[:, 0])
    times_list = {}
    for test_name in test_names_list:
        times_list[test_name] = []
        for row in range(df.shape[0]):
            if str(df.iloc[row, 0]) == test_name:
                times_list[test_name].append(str(df.iloc[row, 5]))
        print(test_name, times_list[test_name])
Community
  • 1
  • 1
  • 1
    you should convert your dates to `int`, do the mean and calculations and then convert back to date. – Thibault D. May 31 '19 at 07:43
  • Possible duplicate of [Using python to create an average out of a list of times](https://stackoverflow.com/questions/12033905/using-python-to-create-an-average-out-of-a-list-of-times) – Will May 31 '19 at 07:58

1 Answers1

0

Resolved.

def average_time(file_path):
    df = pd.DataFrame(pd.read_excel(file_path))
    test_names_list = collections.Counter(df.iloc[:, 0])
    times_list = {}
    for test_name in test_names_list:
        times_list[test_name] = []
        for row in range(df.shape[0]):
            if str(df.iloc[row, 0]) == test_name:
                times_list[test_name].append(get_sec(str(df.iloc[row, 5])))
        print(test_name, str(dt.timedelta(seconds=mean(times_list[test_name]))))