cycquery.ops.Apply

class Apply(cols, funcs, new_cols=None)[source]

Bases: QueryOp

Apply function(s) to column(s).

The function can take a sqlalchemy column object and also return a column object. It can also take multiple columns and return a single column or multiple columns. If multiple functions are provided, it is assumed that each function is applied to each input column.

Parameters:
  • cols (Union[str, List[str]]) – Column(s) to apply the function to.

  • funcs (Union[Callable[[Column], Column], List[Callable[[Column], Column]]]) – Function(s) that takes in sqlalchemy column(s) object and returns column(s) after applying the function or list of functions to apply to each column.

  • new_cols (Union[str, List[str], None]) – New column name(s) after function is applied to the specified column(s).

Examples

>>> Apply("col1", lambda x: x + 1)(table)
>>> Apply(["col1", "col2"], [lambda x: x + 1, lambda x: x + 2])(table)
>>> Apply("col1", lambda x: x + 1, new_cols="col1_new")(table)
>>> Apply(["col1", "col2"], lambda x, y: x + y, new_cols="col1_new")(table)
>>> Apply(
...     ["col1", "col2"],
...     lambda x, y: (x + y, x - y),
...     new_cols=["col1_new", "col2_new"],
... )(table)  # noqa: E501, pylint: disable=line-too-long

Initialize the operation.

Methods

__call__(table)[source]

Process the table.

Parameters:

table (Union[Select, Subquery, Table, DBTable]) – Table on which to perform the operation.

Returns:

Processed table.

Return type:

sqlalchemy.sql.selectable.Subquery

__init__(cols, funcs, new_cols=None)[source]

Initialize the operation.