dtoolkit.transformer.QueryTF#

class dtoolkit.transformer.QueryTF(*args, **kwargs)[source]#

A transformer query the columns of a DataFrame with a boolean expression.

See also

pandas.DataFrame.query

This transformer’s prototype method.

Notes

query()’s inplace parameter is not work for this transformer. Actually this break pipeline stream. If a transformer’s inplace is True, the next tf input would get None.

Examples

>>> import pandas as pd
>>> from dtoolkit.transformer import QueryTF
>>> df = pd.DataFrame(
...     {
...         'A': range(1, 6),
...         'B': range(10, 0, -2),
...         'C C': range(10, 5, -1),
...     },
... )
>>> df
    A   B  C C
0  1  10   10
1  2   8    9
2  3   6    8
3  4   4    7
4  5   2    6
>>> tf = QueryTF('A > B')
>>> tf.transform(df)
    A  B  C C
4  5  2    6

The previous expression is equivalent to

>>> df[df.A > df.B]
    A  B  C C
4  5  2    6

For columns with spaces in their name, you can use backtick quoting.

>>> tf = QueryTF('B == `C C`')
>>> tf.transform(df)
    A   B  C C
0  1  10   10

The previous expression is equivalent to

>>> df[df.B == df['C C']]
    A   B  C C
0  1  10   10
Attributes:
inverse_transform_method

Methods

fit(*_)

Fit transformer.

fit_transform(X[, y])

Fit to data, then transform it.

inverse_transform(X)

Undo transform to X.

set_output(*[, transform])

Set output container.

transform(X)

Transform X.

transform_method(self, expr, *[, inplace])

Query the columns of a DataFrame with a boolean expression.

update_invargs(*args, **kwargs)

Inverse transform method argument entry.