superleaf.dataframe.selection#

Functions

dfilter(df, *filters, **col_filters)

Select the rows of a DataFrame that pass every filter.

partition(df, *filters, **col_filters)

Split a DataFrame into rows that pass every filter and rows that do not.

reorder_columns(df, columns[, back, after, ...])

Move columns to the front, the back, or next to another column.

Classes

where(f)

Row filter evaluated on the whole DataFrame at once.

class superleaf.dataframe.selection.where(f: Callable[[DataFrame], Any])[source]#

Bases: object

Row filter evaluated on the whole DataFrame at once.

Use with dfilter() or partition() for vectorized conditions across columns. A pd.col expression can be passed directly instead.

Parameters:

f (callable) – Function from the DataFrame to a boolean Series or array with one value per row.

Examples

>>> dfilter(df, where(lambda d: d["a"] > d["b"]))
superleaf.dataframe.selection.dfilter(df: DataFrame, *filters, **col_filters) → DataFrame[source]#

Select the rows of a DataFrame that pass every filter.

Parameters:
  • df (pd.DataFrame) – The DataFrame to filter. A Series is also accepted; its index can be filtered with index=....

  • *filters –

    Row filters, each one of:

    • a pd.col expression, such as pd.col("a") > pd.col("b");

    • a where, evaluated once on the whole DataFrame;

    • another callable, applied to each row (a Series) and returning a bool;

    • a dict of column filters, as for **col_filters (for column names that are not identifiers);

    • a boolean sequence or Series with one value per row.

  • **col_filters –

    Column filters, mapping a column name to one of:

    • a BooleanOperator such as F.gt(0), evaluated with on_series;

    • another callable, applied to each value;

    • a scalar, selecting values equal to it (as F.eq).

    Lists and other non-scalar values raise TypeError; use F.isin for membership.

    The keyword columns, unless it names a column of df, selects columns instead of rows. It takes a column name, a predicate on names, or a list of either; columns matching any of them are kept.

Returns:

The selected rows (and columns), as a new DataFrame.

Return type:

pd.DataFrame

Raises:
  • TypeError – If a filter has an unsupported type.

  • ValueError – If a boolean filter’s length or index does not match df.

Examples

>>> dfilter(df, status="active", age=F.ge(30))
>>> dfilter(df, name=F.startswith("A") | F.isna, columns=["name", "age"])
>>> dfilter(df, pd.col("score") > pd.col("threshold"), status="active")
superleaf.dataframe.selection.partition(df: DataFrame, *filters, **col_filters) → tuple[DataFrame, DataFrame][source]#

Split a DataFrame into rows that pass every filter and rows that do not.

Parameters:
  • df (pd.DataFrame) – The DataFrame to split.

  • *filters – Row filters, as for dfilter().

  • **col_filters – Column filters, as for dfilter(), including columns.

Returns:

  • pd.DataFrame – The rows that pass.

  • pd.DataFrame – The remaining rows.

Examples

>>> passed, failed = partition(df, score=F.gt(50))
superleaf.dataframe.selection.reorder_columns(df: DataFrame, columns: str | Sequence[str], back=False, after=None, before=None, errors='raise') → DataFrame[source]#

Move columns to the front, the back, or next to another column.

Parameters:
  • df (pd.DataFrame) – The DataFrame whose columns to reorder.

  • columns (str or sequence of str) – Column(s) to move, in the order they should appear.

  • back (bool, optional) – If True, move the columns to the end.

  • after (str, optional) – Place the columns immediately after this column.

  • before (str, optional) – Place the columns immediately before this column.

  • errors ({"raise", "warn", "ignore"}, optional) – How to handle names in columns that are not in df. With "warn" or "ignore", they are skipped.

Returns:

A new DataFrame with reordered columns. With none of back, after or before, the columns are moved to the front.

Return type:

pd.DataFrame

Raises:
  • ValueError – If more than one of back, after and before is given, or errors is invalid.

  • KeyError – If a column is missing and errors="raise".

Examples

>>> reorder_columns(df, ["age", "name"], after="id")