File: percentile_rank.py

package info (click to toggle)
python-agate 1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,996 kB
  • sloc: python: 8,512; makefile: 126
file content (34 lines) | stat: -rw-r--r-- 966 bytes parent folder | download | duplicates (2)
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
from agate.aggregations.percentiles import Percentiles
from agate.computations.rank import Rank
from agate.data_types import Number
from agate.exceptions import DataTypeError


class PercentileRank(Rank):
    """
    Calculate the percentile into which each value falls.

    See :class:`.Percentiles` for implementation details.

    :param column_name:
        The name of a column containing the :class:`.Number` values.
    """
    def validate(self, table):
        column = table.columns[self._column_name]

        if not isinstance(column.data_type, Number):
            raise DataTypeError('PercentileRank column must contain Number data.')

    def run(self, table):
        """
        :returns:
            :class:`int`
        """
        percentiles = Percentiles(self._column_name).run(table)

        new_column = []

        for row in table.rows:
            new_column.append(percentiles.locate(row[self._column_name]))

        return new_column