Skip to content

Brackets(limits, bounds=Bounds.LOW_ONLY, labels=None, first=BracketEnd.CLOSED, last=BracketEnd.CLOSED, others=Unmatched.ERROR) Source

Brackets cutting a number column into categories, used as the format of a TableConversion to CATEGORY.

The categories keep the order of the brackets, so that sorting and plots follow it. A missing value stays missing.

Changed in version 0.2.0

Refuses others=Unmatched.KEEP.

Version history (1)

Added in version 0.1.0

Parameters:

  • limits (list[int | float])

    The limits between the brackets, at least one, in strictly increasing order. Two limits make one bracket.

  • bounds (Bounds, default: Bounds.LOW_ONLY)

    Which limit of each bracket it holds: Bounds.LOW_ONLY by default, 0 <= x < 10, or Bounds.HIGH_ONLY, 0 < x <= 10. The other members are refused, as they would put a value in two brackets or in none.

  • labels (list[str] | None, default: None)

    The name of each bracket, in order. When omitted, each name gives the values the bracket holds: 0 to 9 for integers, 0 to under 10 for decimals, 80 or more or under 0 for an open bracket. Whole limits are written without decimals.

  • first (BracketEnd, default: BracketEnd.CLOSED)

    Whether the first bracket stops at the first limit, BracketEnd.CLOSED by default, or holds every value below it with BracketEnd.OPEN.

  • last (BracketEnd, default: BracketEnd.CLOSED)

    Whether the last bracket stops at the last limit, BracketEnd.CLOSED by default, or holds every value above it with BracketEnd.OPEN.

  • others (Unmatched, default: Unmatched.ERROR)

    What happens to a value in no bracket, Unmatched.ERROR by default, or Unmatched.MISSING. Unmatched.KEEP is refused, as a number cannot name a bracket.

Raises:

Examples:

import numdaplot as ndp

ages = ndp.Array.from_list([4, 17, 30, 92])
victims = ndp.Table.from_arrays({"age": ages})
brackets = ndp.Brackets(limits=[0, 10, 20], last=ndp.BracketEnd.OPEN)
victims.convert(ndp.TableConversion(columns=["age"], dtype=ndp.Type.CATEGORY, format=brackets))
#           age
#      CATEGORY
# 0      0 to 9
# 1    10 to 19
# 2  20 or more
# 3  20 or more

Methods:

count_brackets() Source

Return the number of brackets: one between two limits, plus one per open end.

Added in version 0.1.0

Returns:

  • int

    The number of brackets.

Examples:

import numdaplot as ndp

ndp.Brackets(limits=[0, 18, 65], last=ndp.BracketEnd.OPEN).count_brackets()
# 3