Skip to content

Set

Bases: BaseOperableBlock

A set which can then be used to index variables.

Examples:

>>> pf.Set(x=range(2), y=range(3))
<Set 'unnamed' height=6>
┌─────┬─────┐
│ x   ┆ y   │
│ (2) ┆ (3) │
╞═════╪═════╡
│ 0   ┆ 0   │
│ 0   ┆ 1   │
│ 0   ┆ 2   │
│ 1   ┆ 0   │
│ 1   ┆ 1   │
│ 1   ┆ 2   │
└─────┴─────┘

Methods:

Name Description
drop

Returns a new Set with the given dimensions dropped.

to_expr

Converts the Set to an Expression equal to 1 for each index.

Source code in pyoframe/_core.py
def __init__(self, *data: SetTypes | Iterable[SetTypes], **named_data):
    data_list = list(data)
    for name, set in named_data.items():
        data_list.append({name: set})
    df = self._parse_acceptable_sets(*data_list)
    if not df.is_empty() and df.is_duplicated().any():
        raise ValueError("Duplicate rows found in input data.")
    super().__init__(df, name="unnamed_set")

drop(*dims: str) -> Set

Returns a new Set with the given dimensions dropped.

Only unique rows are kept in the resulting Set.

Examples:

>>> xy = pf.Set(x=range(3), y=range(2))
>>> xy
<Set 'unnamed' height=6>
┌─────┬─────┐
│ x   ┆ y   │
│ (3) ┆ (2) │
╞═════╪═════╡
│ 0   ┆ 0   │
│ 0   ┆ 1   │
│ 1   ┆ 0   │
│ 1   ┆ 1   │
│ 2   ┆ 0   │
│ 2   ┆ 1   │
└─────┴─────┘
>>> x = xy.drop("y")
>>> x
<Set 'unnamed_set.drop(…)' height=3>
┌─────┐
│ x   │
│ (3) │
╞═════╡
│ 0   │
│ 1   │
│ 2   │
└─────┘
Source code in pyoframe/_core.py
def drop(self, *dims: str) -> Set:
    """Returns a new Set with the given dimensions dropped.

    Only unique rows are kept in the resulting Set.

    Examples:
        >>> xy = pf.Set(x=range(3), y=range(2))
        >>> xy
        <Set 'unnamed' height=6>
        ┌─────┬─────┐
        │ x   ┆ y   │
        │ (3) ┆ (2) │
        ╞═════╪═════╡
        │ 0   ┆ 0   │
        │ 0   ┆ 1   │
        │ 1   ┆ 0   │
        │ 1   ┆ 1   │
        │ 2   ┆ 0   │
        │ 2   ┆ 1   │
        └─────┴─────┘
        >>> x = xy.drop("y")
        >>> x
        <Set 'unnamed_set.drop(…)' height=3>
        ┌─────┐
        │ x   │
        │ (3) │
        ╞═════╡
        │ 0   │
        │ 1   │
        │ 2   │
        └─────┘
    """
    if not dims:
        raise ValueError("At least one dimension must be provided to drop.")
    return self._new(
        self.data.drop(dims).unique(maintain_order=Config.maintain_order),
        name=f"{self.name}.drop(…)",
    )

to_expr() -> Expression

Converts the Set to an Expression equal to 1 for each index.

Useful when multiplying a Set by an Expression.

Source code in pyoframe/_core.py
def to_expr(self) -> Expression:
    """Converts the Set to an Expression equal to 1 for each index.

    Useful when multiplying a Set by an Expression.
    """
    return Expression(
        self.data.with_columns(
            pl.lit(1).alias(COEF_KEY), pl.lit(CONST_TERM).alias(VAR_KEY)
        ),
        name=self.name,
    )