forked from wireservice/agate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mean.py
44 lines (33 loc) · 1.3 KB
/
mean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
from agate.aggregations.base import Aggregation
from agate.aggregations.has_nulls import HasNulls
from agate.aggregations.sum import Sum
from agate.data_types import Number
from agate.exceptions import DataTypeError
from agate.warns import warn_null_calculation
class Mean(Aggregation):
"""
Calculate the mean of a column.
:param column_name:
The name of a column containing :class:`.Number` data.
"""
def __init__(self, column_name):
self._column_name = column_name
self._sum = Sum(column_name)
def get_aggregate_data_type(self, table):
return Number()
def validate(self, table):
column = table.columns[self._column_name]
if not isinstance(column.data_type, Number):
raise DataTypeError('Mean can only be applied to columns containing Number data.')
has_nulls = HasNulls(self._column_name).run(table)
if has_nulls:
warn_null_calculation(self, column)
def run(self, table):
column = table.columns[self._column_name]
num_of_values = len(column.values_without_nulls())
# If there are no non-null columns then return null.
if num_of_values == 0:
return None
sum_total = self._sum.run(table)
return sum_total / num_of_values