superleaf.operators.base#

Functions

bool_operator(f[, exceptions, fallback])

Wrap a single-argument predicate as a BooleanOperator.

operator(f[, exceptions, fallback])

Wrap a single-argument function as a pipeable Operator.

Classes

BooleanFunctionOperator(f)

A BooleanOperator wrapping a single-argument function; its result is converted with bool.

BooleanOperator()

A predicate on a single value, combinable with |, & and ~.

FunctionOperator(f)

An Operator wrapping a single-argument function.

Operator()

A callable that can be piped into another callable with >>.

VectorizedBooleanOperator(f, vf[, ...])

A BooleanFunctionOperator with a vectorized implementation for Series.

Exceptions

NotVectorizable

Raised by a vectorized implementation to request element-wise evaluation.

class superleaf.operators.base.Operator[source]#

Bases: object

A callable that can be piped into another callable with >>.

(a >> b)(x) is b(a(x)). If b is a BooleanOperator, the result is also a BooleanOperator.

class superleaf.operators.base.FunctionOperator(f: Callable)[source]#

Bases: Operator

An Operator wrapping a single-argument function.

class superleaf.operators.base.BooleanOperator[source]#

Bases: Operator

A predicate on a single value, combinable with |, & and ~.

Calling the operator evaluates one value and returns a bool. on_series() evaluates every element of a pd.Series and returns a boolean pd.Series with the same index, giving the same result per element as calling the operator on it.

| and & short-circuit like or and and: the right operand is evaluated only where the left one does not decide the result, so F.notna & F.gt(0) never compares a missing value.

on_series(s: Series) → Series[source]#

Evaluate the predicate on each element of a Series.

Parameters:

s (pd.Series) – Values to test.

Returns:

Boolean Series with the same index as s.

Return type:

pd.Series

class superleaf.operators.base.BooleanFunctionOperator(f: Callable[[Any], Any])[source]#

Bases: BooleanOperator

A BooleanOperator wrapping a single-argument function; its result is converted with bool.

class superleaf.operators.base.VectorizedBooleanOperator(f: Callable[[Any], Any], vf: Callable[[Series], Any], vectorize_missing: bool = False)[source]#

Bases: BooleanFunctionOperator

A BooleanFunctionOperator with a vectorized implementation for Series.

Parameters:
  • f (callable) – Scalar predicate.

  • vf (callable) – Function from a Series to a boolean array-like of the same length, matching f element-wise. It may raise TypeError or ValueError (or NotVectorizable) to fall back to evaluating f on each element.

  • vectorize_missing (bool, optional) – If True, use vf results for missing values too. Otherwise they are replaced with f results.

on_series(s: Series) → Series[source]#

Evaluate the predicate on each element of a Series.

Parameters:

s (pd.Series) – Values to test.

Returns:

Boolean Series with the same index as s.

Return type:

pd.Series

exception superleaf.operators.base.NotVectorizable[source]#

Bases: Exception

Raised by a vectorized implementation to request element-wise evaluation.

superleaf.operators.base.operator(f: Callable, exceptions=None, fallback=None) → FunctionOperator[source]#

Wrap a single-argument function as a pipeable Operator.

Parameters:
  • f (callable) – The function to wrap.

  • exceptions (type, tuple of types, or str, optional) – Exceptions to catch, returning fallback instead (see with_fallback()). Not caught if None.

  • fallback (Any, optional) – Value returned when one of exceptions is raised.

Returns:

The wrapped function.

Return type:

FunctionOperator

superleaf.operators.base.bool_operator(f: Callable, exceptions=None, fallback=False) → BooleanFunctionOperator[source]#

Wrap a single-argument predicate as a BooleanOperator.

Parameters:
  • f (callable) – The predicate; its result is converted with bool.

  • exceptions (type, tuple of types, or str, optional) – Exceptions to catch, returning fallback instead (see with_fallback()). Not caught if None.

  • fallback (bool, optional) – Value returned when one of exceptions is raised. Default is False.

Returns:

The wrapped predicate.

Return type:

BooleanFunctionOperator

Raises:

TypeError – If fallback is not a bool.