superleaf.operators.comparison#

Classes

ComparisonFunctions()

Factories for common BooleanOperator predicates, usually imported as F.

F

class superleaf.operators.comparison.ComparisonFunctions[source]#

Bases: object

Factories for common BooleanOperator predicates, usually imported as F.

Every predicate works on single values (F.gt(0)(5)) and on Series via on_series, which dfilter and partition use for keyword filters. on_series gives the same result per element as the scalar call, using vectorized pandas operations where they agree with it.

In eq, ne, lt, le, gt and ge with a non-missing value, missing values (None, NaN, NaT, pd.NA) are treated like NaN: only ne is True.

Predicates that take fallback and exceptions return fallback when evaluating a value raises one of exceptions. Passing either one enables this; exceptions defaults to Exception and fallback to False. Without them, exceptions propagate.

Examples

>>> F.gt(0)(1)
True
>>> (F.isna | F.lt(0))(float("nan"))
True
>>> F.lt(0, fallback=False)("a")
False
Variables:
static eq(value: Any, *, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test x == value.

Parameters:
  • value (Any) – Value to compare against.

  • fallback (bool, optional) – Result when comparing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

static ne(value: Any, *, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test x != value.

Parameters:
  • value (Any) – Value to compare against.

  • fallback (bool, optional) – Result when comparing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

static lt(value: Any, *, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test x < value.

Parameters:
  • value (Any) – Value to compare against.

  • fallback (bool, optional) – Result when comparing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

static le(value: Any, *, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test x <= value.

Parameters:
  • value (Any) – Value to compare against.

  • fallback (bool, optional) – Result when comparing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

static gt(value: Any, *, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test x > value.

Parameters:
  • value (Any) – Value to compare against.

  • fallback (bool, optional) – Result when comparing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

static ge(value: Any, *, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test x >= value.

Parameters:
  • value (Any) – Value to compare against.

  • fallback (bool, optional) – Result when comparing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

static isin(values: Any, *, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test x in values.

Parameters:
  • values (iterable or str) – Collection to test membership in; unhashable x gives False. A str tests for a substring.

  • fallback (bool, optional) – Result when testing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

static contains(value: Any, *, exceptions=<class 'TypeError'>, fallback=False) → BooleanOperator[source]#

Test value in x.

Parameters:
  • value (Any) – Item or substring to look for.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback. Default TypeError, so non-containers give False.

  • fallback (bool, optional) – Result when testing raises one of exceptions. Default False.

Returns:

The predicate.

Return type:

BooleanOperator

static contains_all(values: Any, *, exceptions=<class 'TypeError'>, fallback=False) → BooleanOperator[source]#

Test that every item of values is in x.

Parameters:
  • values (Any) – Items to look for. A str or non-iterable is treated as a single item.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback. Default TypeError, so non-containers give False.

  • fallback (bool, optional) – Result when testing raises one of exceptions. Default False.

Returns:

The predicate.

Return type:

BooleanOperator

static contains_any(values: Any, *, exceptions=<class 'TypeError'>, fallback=False) → BooleanOperator[source]#

Test that at least one item of values is in x.

Parameters:
  • values (Any) – Items to look for. A str or non-iterable is treated as a single item.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback. Default TypeError, so non-containers give False.

  • fallback (bool, optional) – Result when testing raises one of exceptions. Default False.

Returns:

The predicate.

Return type:

BooleanOperator

static startswith(value: Any, *, str_converter=<class 'str'>, raise_type_error=False, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test that x starts with value.

Parameters:
  • value (Any) – Prefix. Converted with str_converter if not a str.

  • str_converter (callable or None, optional) – Converts non-missing, non-str values of x (and value) to str. Default str. If None, non-str values give False.

  • raise_type_error (bool, optional) – If True, raise TypeError for non-str values instead of converting them.

  • fallback (bool, optional) – Result when testing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate. Missing values give False unless raise_type_error is True.

Return type:

BooleanOperator

static endswith(value: Any, *, str_converter=<class 'str'>, raise_type_error=False, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test that x ends with value.

Parameters:
  • value (Any) – Suffix. Converted with str_converter if not a str.

  • str_converter (callable or None, optional) – Converts non-missing, non-str values of x (and value) to str. Default str. If None, non-str values give False.

  • raise_type_error (bool, optional) – If True, raise TypeError for non-str values instead of converting them.

  • fallback (bool, optional) – Result when testing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate. Missing values give False unless raise_type_error is True.

Return type:

BooleanOperator

static startswith_one_of(values: ~typing.Iterable[~typing.Any], *, str_converter=<class 'str'>, raise_type_error=False, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test that x starts with any of values.

Parameters:
  • values (iterable) – Prefixes. Each is converted with str_converter if not a str.

  • str_converter (callable or None, optional) – See startswith().

  • raise_type_error (bool, optional) – See startswith().

  • fallback (bool, optional) – Result when testing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

static endswith_one_of(values: ~typing.Iterable[~typing.Any], *, str_converter=<class 'str'>, raise_type_error=False, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test that x ends with any of values.

Parameters:
  • values (iterable) – Suffixes. Each is converted with str_converter if not a str.

  • str_converter (callable or None, optional) – See endswith().

  • raise_type_error (bool, optional) – See endswith().

  • fallback (bool, optional) – Result when testing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

static matches_regex(pattern: str, how: Literal['match', 'search', 'fullmatch']='match', *, flags: int = 0, str_converter=<class 'str'>, raise_type_error=False, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test that x matches a regular expression.

Parameters:
  • pattern (str) – Regular expression.

  • how ({"match", "search", "fullmatch"}, optional) – "match" (default) anchors the pattern at the start of x, "search" finds it anywhere, and "fullmatch" requires it to match all of x; as in re.

  • flags (int, optional) – re flags.

  • str_converter (callable or None, optional) – See startswith().

  • raise_type_error (bool, optional) – See startswith().

  • fallback (bool, optional) – Result when testing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

Raises:

ValueError – If how is not one of the allowed values.

static fuzzy_match(targets: str | ~typing.Iterable[str], *, normalizer: ~typing.Callable[[str], str] | None = <unset>, substring: bool = True, score_threshold: float = 80.0, raise_type_error: bool = False, fallback=<unset>, exceptions=None) → BooleanOperator[source]#

Test that x approximately matches any of targets. Requires superleaf[fuzzy].

Parameters:
  • targets (str or iterable of str) – Strings to match against.

  • normalizer (callable or None, optional) – Applied to x and targets before scoring. Defaults to rapidfuzz.utils.default_process (lowercase, non-alphanumerics replaced by spaces, trimmed). None disables normalization.

  • substring (bool, optional) – If True (default), score with rapidfuzz.fuzz.partial_ratio, so a target can match part of x. Otherwise use rapidfuzz.fuzz.ratio.

  • score_threshold (float, optional) – Minimum score (0-100) for a match. Default 80.

  • raise_type_error (bool, optional) – If True, raise TypeError for non-str values; otherwise they give False.

  • fallback (bool, optional) – Result when testing raises one of exceptions.

  • exceptions (type, tuple of types, or str, optional) – Exceptions that produce fallback.

Returns:

The predicate.

Return type:

BooleanOperator

isna: BooleanOperator = <superleaf.operators.base.VectorizedBooleanOperator object>#
notna: BooleanOperator = <superleaf.operators.base._NotBoolOp object>#
isinf: BooleanOperator = <superleaf.operators.base.VectorizedBooleanOperator object>#
notinf: BooleanOperator = <superleaf.operators.base._NotBoolOp object>#
superleaf.operators.comparison.F[source]#

alias of ComparisonFunctions