File: bins.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 (106 lines) | stat: -rw-r--r-- 3,227 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from decimal import Decimal

from babel.numbers import format_decimal

from agate import utils
from agate.aggregations import Max, Min


def bins(self, column_name, count=10, start=None, end=None):
    """
    Generates (approximately) evenly sized bins for the values in a column.
    Bins may not be perfectly even if the spread of the data does not divide
    evenly, but all values will always be included in some bin.

    The resulting table will have two columns. The first will have
    the same name as the specified column, but will be type :class:`.Text`.
    The second will be named :code:`count` and will be of type
    :class:`.Number`.

    :param column_name:
        The name of the column to bin. Must be of type :class:`.Number`
    :param count:
        The number of bins to create. If not specified then each value will
        be counted as its own bin.
    :param start:
        The minimum value to start the bins at. If not specified the
        minimum value in the column will be used.
    :param end:
        The maximum value to end the bins at. If not specified the maximum
        value in the column will be used.
    :returns:
        A new :class:`Table`.
    """
    minimum, maximum = utils.round_limits(
        Min(column_name).run(self),
        Max(column_name).run(self)
    )
    # Infer bin start/end positions
    start = minimum if not start else Decimal(start)
    end = maximum if not end else Decimal(end)

    # Calculate bin size
    spread = abs(end - start)
    size = spread / count

    breaks = [start]

    # Calculate breakpoints
    for i in range(1, count + 1):
        top = start + (size * i)

        breaks.append(top)

    # Format bin names
    decimal_places = utils.max_precision(breaks)
    break_formatter = utils.make_number_formatter(decimal_places)

    def name_bin(i, j, first_exclusive=True, last_exclusive=False):
        inclusive = format_decimal(i, format=break_formatter)
        exclusive = format_decimal(j, format=break_formatter)

        output = '[' if first_exclusive else '('
        output += f'{inclusive} - {exclusive}'
        output += ']' if last_exclusive else ')'

        return output

    # Generate bins
    bin_names = []

    for i in range(1, len(breaks)):
        last_exclusive = (i == len(breaks) - 1)

        if i == 1 and minimum < start:
            name = name_bin(minimum, breaks[i], last_exclusive=last_exclusive)
        elif i == len(breaks) - 1 and maximum > end:
            name = name_bin(breaks[i - 1], maximum, last_exclusive=last_exclusive)
        else:
            name = name_bin(breaks[i - 1], breaks[i], last_exclusive=last_exclusive)

        bin_names.append(name)

    bin_names.append(None)

    # Lambda method for actually assigning values to bins
    def binner(row):
        value = row[column_name]

        if value is None:
            return None

        i = 1

        try:
            while value >= breaks[i]:
                i += 1
        except IndexError:
            i -= 1

        return bin_names[i - 1]

    # Pivot by lambda
    table = self.pivot(binner, key_name=column_name)

    # Sort by bin order
    return table.order_by(lambda r: bin_names.index(r[column_name]))