BinningSpec#

from tmlt.analytics import BinningSpec
class tmlt.analytics.BinningSpec(bin_edges, names=None, right=True, include_both_endpoints=True, nan_bin=None)#

Bases: Generic[BinT, BinNameT]

A spec object defining an operation where values are assigned to bins.

A BinningSpec divides values into bins based on a list of bin edges, for use with the bin_column() method. All supported data types can be binned using a BinningSpec.

Values outside the range of the provided bins and None types are all mapped to None (null in Spark), as are NaN values by default. Bin names are generated based on the bin edges, but custom names can be provided.

By default, the right edge of each bin is included in that bin: using edges [0, 5, 10] will lead to bins [0, 5] and (5, 10]. To include the left edge instead, set the right parameter to False.

Examples

>>> spec = BinningSpec([0,5,10])
>>> spec.bins()
['[0, 5]', '(5, 10]']
>>> spec(0)
'[0, 5]'
>>> spec(5)
'[0, 5]'
>>> spec(6)
'(5, 10]'
>>> spec(10)
'(5, 10]'
>>> spec(11) is None
True
__init__(bin_edges, names=None, right=True, include_both_endpoints=True, nan_bin=None)#

Initialize a BinningSpec.

Parameters:
  • bin_edges (Union[Sequence[TypeVar(BinT, str, Union[int, float], date, datetime)], ndarray]) – A list of the bin edges, sorted in ascending order.

  • names (Union[Sequence[Optional[TypeVar(BinNameT, str, int, float, date, datetime)]], ndarray, None]) – If given, used as the names of bins. Must be one element shorter than bin_edges. Duplicate values are allowed, which will place non-contiguous ranges of values into the same bin. Note that while using floats and timestamps as bin names is allowed here, grouping on the resulting column is not allowed.

  • right (bool) – When True, the right edge of each bin is included in that bin; otherwise, the left edge is. Defaults to True.

  • include_both_endpoints (bool) – When True, the outer edges of both the first and last bins will be included in their respective bins; when False, these edges are treated the same as the other bins, i.e. only one will be included based on how right is set. Defaults to True.

  • nan_bin (Optional[TypeVar(BinNameT, str, int, float, date, datetime)]) – If binning over a float-valued column, all NaNs will be placed in a bin with this name. The default value, None, causes these values to be placed in the same bin with out-of-range and null values.

__eq__(other)#

Adds equality comparison to the BinningSpec class.

__hash__()#

Hashes the bin spec on a tuple of its attributes.

__repr__()#

Returns a string representation of the BinningSpec.

property input_type: ColumnType#

Return the ColumnType of the column this binning can be applied to.

property column_descriptor: ColumnDescriptor#

Return the ColumnDescriptor that results from applying this binning.

bins(include_null=False)#

Return a list of all the bin names that could result from the binning.

The returned list is guaranteed to contain unique elements, even if multiple bins were mapped to the same name. The NaN bin, if one was specified, is included. If include_null is true, the null bin is included as well; by default, it is not included.

Return type:

List[Optional[TypeVar(BinNameT, str, int, float, date, datetime)]]

__call__(val)#

Given a value to bin, return its bin name.

In most cases this method only needs to be used internally, but it can be called on its own to test the binning that will be performed.

Parameters:

val (Optional[TypeVar(BinT, str, Union[int, float], date, datetime)]) – The value to be assigned to a bin.

Return type:

Any