forked from wireservice/agate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
min.py
45 lines (34 loc) · 1.37 KB
/
min.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
45
#!/usr/bin/env python
from agate.aggregations.base import Aggregation
from agate.data_types import Date, DateTime, Number
from agate.exceptions import DataTypeError
class Min(Aggregation):
"""
Find the minimum value in a column.
This aggregation can be applied to columns containing :class:`.Date`,
:class:`.DateTime`, or :class:`.Number` data.
:param column_name:
The name of the column to be searched.
"""
def __init__(self, column_name):
self._column_name = column_name
def get_aggregate_data_type(self, table):
column = table.columns[self._column_name]
if (isinstance(column.data_type, Number) or
isinstance(column.data_type, Date) or
isinstance(column.data_type, DateTime)):
return column.data_type
def validate(self, table):
column = table.columns[self._column_name]
if not (isinstance(column.data_type, Number) or
isinstance(column.data_type, Date) or
isinstance(column.data_type, DateTime)):
raise DataTypeError('Min can only be applied to columns containing DateTime or Number data.')
def run(self, table):
column = table.columns[self._column_name]
values = column.values_without_nulls()
if len(values) == 0:
ret = None
else:
ret = min(column.values_without_nulls())
return ret