Commit ac56e1fa authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Redesign the get_metric method using pandas.DataFrame.

parent c77c137b
......@@ -2,6 +2,7 @@
"""
import json
import pandas as pd
import re
......@@ -123,7 +124,7 @@ class Report(SelectorActiveItems):
@rtype: int
"""
value = 0
value = None
now = date.today()
if row.people.birth_date:
value = ((now - row.people.birth_date).days / 365)
......@@ -245,32 +246,41 @@ class Report(SelectorActiveItems):
"""
db = current.globalenv['db']
tablename, fieldname = groupby.split('.')
data, xgroup = [], None
for row in self.select(db.history, orderby=db[tablename][fieldname]):
if row[tablename][fieldname] != xgroup:
xgroup = row[tablename][fieldname]
data.append(dict(group=xgroup, count=set(), sum_fte=0., avg_age=[]))
# configuration
cfg = [Storage(label=groupby, field=groupby, op=None),
Storage(label='count', field='people.id', op='size'),
Storage(label='sum_fte', field='history.fte', op='sum'),
Storage(label='avg_age', field='people.age', op='mean')]
# fields, labels and operators
fields = [el.field.split('.') for el in cfg]
labels = [el.label for el in cfg]
operators = dict()
for el in cfg:
if el.op != None:
operators[el.label] = el.op
# interrogate the database and fill the data frame
data = []
rows = self.select(db.history, orderby=db[tablename][fieldname])
data[-1]['count'].add("%s %s" %(row.people.first_name, row.people.last_name))
data[-1]['sum_fte'] += row.history.fte
data[-1]['avg_age'].append(row.people.age)
for row in rows:
data.append([row[table][field] for table, field in fields])
df = pd.DataFrame(data, columns=labels)
for el in data:
# count the number of people
# using set avoid trouble with duplicate entries
el['count'] = len(el['count'])
# average the age
# remove entry equal to 0
avg = 0.
if el['count']:
nz = el['avg_age'].count(0)
if nz < el['count']:
avg = sum(el['avg_age']) / float(el['count']-nz)
el['avg_age'] = avg
# aggregate the data running dedicated operator per column
df = df.groupby(groupby)
df = df.agg(operators)
# build the output: a list of dictionary
# The latter contains the key group, count, avg_age and sum_fte
data = []
for group, di in df.T.to_dict().iteritems():
di['group'] = group
data.append(di)
return data
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment