PyLegendBoolean

Boolean expression type in the PyLegend expression language.

PyLegendBoolean represents a boolean-valued column or computed expression within a PyLegend query. It supports the standard logical operators (&, |, ~, ^), comparison operators (<, <=, >, >=), and conditional branching via case().

Instances are never constructed directly. They are produced by operations on TDS frame columns — for example, comparing a column to a literal (frame["Age"] > 30) or accessing a boolean column through a row accessor (row.get_boolean("is_active")).

PyLegendBoolean also inherits general-purpose methods from PyLegendPrimitive, including equality / inequality tests, null checks, string conversion, and in_list.

__and__

PyLegendBoolean.__and__(other)[source]

Logical AND (&).

Combine two boolean expressions with a logical AND.

Parameters:

other (Union[bool, PyLegendBoolean]) – The right-hand operand.

Returns:

True where both operands are True.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a bool or PyLegendBoolean.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Orders with id >= 10250 AND id <= 10260
cond = (frame["Order Id"] >= 10250) & (frame["Order Id"] <= 10260)
frame[cond].head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name
0 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes
1 10251 1996-07-08 1996-08-05 1996-07-15 Victuailles en stock
2 10252 1996-07-09 1996-08-06 1996-07-11 Suprêmes délices

__eq__

PyLegendPrimitive.__eq__(other)[source]

Test element-wise equality (==).

Compare this expression against another primitive value or expression. Returns a boolean expression that is True where the values are equal.

Parameters:

other (Union[int, float, bool, str, date, datetime, Decimal, PyLegendPrimitive]) – The value or expression to compare against.

Returns:

A boolean expression representing the equality test.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a supported primitive type.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Filter rows where Ship Name equals a literal
frame[frame["Ship Name"] == "Around the Horn"].head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name
0 10355 1996-11-15 1996-12-13 1996-11-20 Around the Horn
1 10383 1996-12-16 1997-01-13 1996-12-18 Around the Horn
2 10453 1997-02-21 1997-03-21 1997-02-26 Around the Horn

__ge__

PyLegendBoolean.__ge__(other)[source]

Greater than or equal comparison (>=).

Parameters:

other (Union[bool, PyLegendBoolean]) – The right-hand operand.

Returns:

True where self >= other.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a bool or PyLegendBoolean.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["ge_check"] = (frame["Order Id"] > 10250) >= (frame["Order Id"] > 10260)
frame.head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name ge_check
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier True
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten True
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes True

__gt__

PyLegendBoolean.__gt__(other)[source]

Greater than comparison (>).

Parameters:

other (Union[bool, PyLegendBoolean]) – The right-hand operand.

Returns:

True where self > other.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a bool or PyLegendBoolean.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["gt_check"] = (frame["Order Id"] > 10250) > (frame["Order Id"] > 10260)
frame.head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name gt_check
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier False
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten False
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes False

__invert__

PyLegendBoolean.__invert__()[source]

Logical NOT (~).

Negate this boolean expression.

Returns:

True where the original expression is False, and vice-versa.

Return type:

PyLegendBoolean

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Negate a condition
frame[~(frame["Order Id"] > 10255)].head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes

__le__

PyLegendBoolean.__le__(other)[source]

Less than or equal comparison (<=).

Parameters:

other (Union[bool, PyLegendBoolean]) – The right-hand operand.

Returns:

True where self <= other.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a bool or PyLegendBoolean.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
frame["le_check"] = (frame["Order Id"] > 10260) <= (frame["Order Id"] > 10250)
frame.head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name le_check
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier True
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten True
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes True

__lt__

PyLegendBoolean.__lt__(other)[source]

Less than comparison (<).

Parameters:

other (Union[bool, PyLegendBoolean]) – The right-hand operand.

Returns:

True where self < other.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a bool or PyLegendBoolean.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Compare two boolean expressions: is (id > 10260) < (id > 10250)?
frame["lt_check"] = (frame["Order Id"] > 10260) < (frame["Order Id"] > 10250)
frame.head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name lt_check
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier False
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten False
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes False

__ne__

PyLegendPrimitive.__ne__(other)[source]

Test element-wise inequality (!=).

Compare this expression against another primitive value or expression. Returns a boolean expression that is True where the values differ.

Parameters:

other (Union[int, float, bool, str, date, datetime, Decimal, PyLegendPrimitive]) – The value or expression to compare against.

Returns:

A boolean expression representing the inequality test.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a supported primitive type.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Filter rows where Ship Name is not a specific value
frame[frame["Ship Name"] != "Ship1"].head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes

__or__

PyLegendBoolean.__or__(other)[source]

Logical OR (|).

Combine two boolean expressions with a logical OR.

Parameters:

other (Union[bool, PyLegendBoolean]) – The right-hand operand.

Returns:

True where either operand is True.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a bool or PyLegendBoolean.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Orders with id < 10250 OR id > 10260
cond = (frame["Order Id"] < 10250) | (frame["Order Id"] > 10260)
frame[cond].head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten
2 10261 1996-07-19 1996-08-16 1996-07-30 Que Delícia

__rand__

PyLegendBoolean.__rand__(other)[source]

Reflected logical AND (bool & expr).

Called when a Python bool literal is on the left side of &. Behaves identically to __and__() with swapped operand order.

Parameters:

other (Union[bool, PyLegendBoolean]) – The left-hand operand.

Returns:

True where both operands are True.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a bool or PyLegendBoolean.

__ror__

PyLegendBoolean.__ror__(other)[source]

Reflected logical OR (bool | expr).

Called when a Python bool literal is on the left side of |. Behaves identically to __or__() with swapped operand order.

Parameters:

other (Union[bool, PyLegendBoolean]) – The left-hand operand.

Returns:

True where either operand is True.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a bool or PyLegendBoolean.

__rxor__

PyLegendBoolean.__rxor__(other)[source]

Reflected logical XOR (bool ^ expr).

Called when a Python bool literal is on the left side of ^. Behaves identically to __xor__() with swapped operand order.

Parameters:

other (Union[bool, PyLegendBoolean]) – The left-hand operand.

Returns:

True where exactly one operand is True.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a bool or PyLegendBoolean.

__xor__

PyLegendBoolean.__xor__(other)[source]

Logical XOR (^).

Exclusive OR — True when exactly one operand is True. Translates to SQL <> and Pure ->xor().

Parameters:

other (Union[bool, PyLegendBoolean]) – The right-hand operand.

Returns:

True where exactly one operand is True.

Return type:

PyLegendBoolean

Raises:

TypeError – If other is not a bool or PyLegendBoolean.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# XOR two conditions
cond = (frame["Order Id"] > 10255) ^ (frame["Order Id"] < 10250)
frame[cond].head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes

case

PyLegendBoolean.case(if_true, if_false)[source]

Conditional expression — SQL CASE WHEN THEN ELSE END.

Evaluate this boolean expression and return if_true where it is True, or if_false where it is False.

Both branches must resolve to the same type family (e.g. both numeric, both string, both date). The return type of the resulting expression matches that family.

Parameters:
  • if_true (Union[int, float, str, bool, date, datetime, Decimal, PyLegendPrimitive]) – Value returned when the condition is True.

  • if_false (Union[int, float, str, bool, date, datetime, Decimal, PyLegendPrimitive]) – Value returned when the condition is False.

Returns:

An expression whose concrete type depends on the branch values (e.g. PyLegendInteger if both branches are integers, PyLegendString if both are strings).

Return type:

PyLegendPrimitive

Raises:

TypeError – If if_true or if_false is not a supported primitive type, or if the two branches have incompatible types.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Classify orders by size
frame["size"] = (frame["Order Id"] > 10250).case("large", "small")
frame.head(5).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name size
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier small
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten small
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes small
3 10251 1996-07-08 1996-08-05 1996-07-15 Victuailles en stock large
4 10252 1996-07-09 1996-08-06 1996-07-11 Suprêmes délices large
# Numeric branch — compute a bonus
frame["bonus"] = (frame["Order Id"] > 10250).case(100, 0)
frame.head(5).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name size bonus
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier small 0
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten small 0
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes small 0
3 10251 1996-07-08 1996-08-05 1996-07-15 Victuailles en stock large 100
4 10252 1996-07-09 1996-08-06 1996-07-11 Suprêmes délices large 100

in_list

PyLegendPrimitive.in_list(lst)[source]

Test whether the value is contained in a list of values.

Returns a boolean expression equivalent to SQL IN (...).

Parameters:

lst (List[Union[int, float, bool, str, date, datetime, Decimal, PyLegendPrimitive]]) – A non-empty list of literal values or expressions to check membership against.

Returns:

True where the value matches any element in lst.

Return type:

PyLegendBoolean

Raises:
  • ValueError – If lst is not a list or is empty.

  • TypeError – If any element in lst is not a supported primitive type.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Keep rows where Ship Name is one of several values
frame[
    frame["Ship Name"].in_list(["Hanari Carnes", "Victuailles en stock", "Suprêmes délices"])
].head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name
0 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes
1 10251 1996-07-08 1996-08-05 1996-07-15 Victuailles en stock
2 10252 1996-07-09 1996-08-06 1996-07-11 Suprêmes délices

is_empty

PyLegendPrimitive.is_empty()[source]

Test whether the value is null / empty.

Returns a boolean expression that is True where the underlying column value is NULL.

Returns:

True where the value is null.

Return type:

PyLegendBoolean

See also

is_not_empty

The inverse test.

is_null

Alias for is_empty.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Keep only rows where Ship Name is null
frame[frame["Shipped Date"].is_empty()].head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name
0 11008 1998-04-08 1998-05-06 NaT Ernst Handel
1 11019 1998-04-13 1998-05-11 NaT Rancho grande
2 11039 1998-04-21 1998-05-19 NaT LINO-Delicateses

is_not_empty

PyLegendPrimitive.is_not_empty()[source]

Test whether the value is not null / not empty.

Returns a boolean expression that is True where the underlying column value is not NULL.

Returns:

True where the value is not null.

Return type:

PyLegendBoolean

See also

is_empty

The inverse test.

is_not_null

Alias for is_not_empty.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Keep only rows where Ship Name is present
frame[frame["Ship Name"].is_not_empty()].head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes

is_not_null

PyLegendPrimitive.is_not_null()[source]

Test whether the value is not null.

Alias for is_not_empty().

Returns:

True where the value is not null.

Return type:

PyLegendBoolean

See also

is_not_empty

Canonical not-null-check method.

is_null

The inverse test.

is_null

PyLegendPrimitive.is_null()[source]

Test whether the value is null.

Alias for is_empty().

Returns:

True where the value is null.

Return type:

PyLegendBoolean

See also

is_empty

Canonical null-check method.

is_not_null

The inverse test.

to_string

PyLegendPrimitive.to_string()[source]

Convert the value to its string representation.

Returns a string expression produced by applying the database’s CAST(... AS TEXT) (SQL) or ->toString() (Pure).

Returns:

The stringified expression.

Return type:

PyLegendString

See also

toString

Alias for to_string.

Examples

Download Interactive Notebook

import pylegend
frame = pylegend.samples.pandas_api.northwind_orders_frame()
# Convert an integer column to a string
frame["order_str"] = frame["Order Id"].to_string()
frame.head(3).to_pandas()
Order Id Order Date Required Date Shipped Date Ship Name order_str
0 10248 1996-07-04 1996-08-01 1996-07-16 Vins et alcools Chevalier 10248
1 10249 1996-07-05 1996-08-16 1996-07-10 Toms Spezialitäten 10249
2 10250 1996-07-08 1996-08-05 1996-07-12 Hanari Carnes 10250

toString

PyLegendPrimitive.toString()[source]

Convert the value to its string representation.

Alias for to_string().

Returns:

The stringified expression.

Return type:

PyLegendString

See also

to_string

Canonical string-conversion method.