1#!/usr/bin/env python
2
3from agate.aggregations.base import Aggregation
4from agate.aggregations.has_nulls import HasNulls
5from agate.aggregations.percentiles import Percentiles
6from agate.data_types import Number
7from agate.exceptions import DataTypeError
8from agate.utils import Quantiles
9from agate.warns import warn_null_calculation
10
11
12class Deciles(Aggregation):
13    """
14    Calculate the deciles of a column based on its percentiles.
15
16    Deciles will be equivalent to the 10th, 20th ... 90th percentiles.
17
18    "Zeroth" (min value) and "Tenth" (max value) deciles are included for
19    reference and intuitive indexing.
20
21    See :class:`Percentiles` for implementation details.
22
23    This aggregation can not be applied to a :class:`.TableSet`.
24
25    :param column_name:
26        The name of a column containing :class:`.Number` data.
27    """
28    def __init__(self, column_name):
29        self._column_name = column_name
30
31    def validate(self, table):
32        column = table.columns[self._column_name]
33
34        if not isinstance(column.data_type, Number):
35            raise DataTypeError('Deciles can only be applied to columns containing Number data.')
36
37        has_nulls = HasNulls(self._column_name).run(table)
38
39        if has_nulls:
40            warn_null_calculation(self, column)
41
42    def run(self, table):
43        """
44        :returns:
45            An instance of :class:`Quantiles`.
46        """
47        percentiles = Percentiles(self._column_name).run(table)
48
49        return Quantiles([percentiles[i] for i in range(0, 101, 10)])
50