-
-
Notifications
You must be signed in to change notification settings - Fork 18.9k
ENH: Introduce pandas.col
#62103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Introduce pandas.col
#62103
Changes from 31 commits
3d17e56
9fcaba3
b41b99d
60c09c2
9e4e0c5
fe78aa2
04044af
a95aeb4
4dc8e55
13d8e5c
e2aeb4f
fa3e793
0bc918a
a0939f9
a703982
48228cc
d6f55a1
b2ed136
c8f0193
e6ea343
96990d6
548ee20
cfbd5a3
e74438c
b4de244
31192e0
83b70e8
3b6906b
9fed80e
edb0e38
72faba9
b6f4961
3791cf6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ Top-level evaluation | |
.. autosummary:: | ||
:toctree: api/ | ||
|
||
col | ||
eval | ||
|
||
Datetime formats | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import ( | ||
Callable, | ||
Hashable, | ||
) | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
) | ||
|
||
from pandas.core.series import Series | ||
|
||
if TYPE_CHECKING: | ||
from pandas import DataFrame | ||
|
||
|
||
# Used only for generating the str repr of expressions. | ||
_OP_SYMBOLS = { | ||
"__add__": "+", | ||
"__radd__": "+", | ||
"__sub__": "-", | ||
"__rsub__": "-", | ||
"__mul__": "*", | ||
"__rmul__": "*", | ||
"__truediv__": "/", | ||
"__rtruediv__": "/", | ||
"__floordiv__": "//", | ||
"__rfloordiv__": "//", | ||
"__mod__": "%", | ||
"__rmod__": "%", | ||
"__ge__": ">=", | ||
"__gt__": ">", | ||
"__le__": "<=", | ||
"__lt__": "<", | ||
"__eq__": "==", | ||
"__ne__": "!=", | ||
} | ||
|
||
|
||
def _parse_args(df: DataFrame, *args: Any) -> tuple[Series]: | ||
# Parse `args`, evaluating any expressions we encounter. | ||
return tuple([x(df) if isinstance(x, Expression) else x for x in args]) | ||
|
||
|
||
def _parse_kwargs(df: DataFrame, **kwargs: Any) -> dict[str, Any]: | ||
# Parse `kwargs`, evaluating any expressions we encounter. | ||
return { | ||
key: val(df) if isinstance(val, Expression) else val | ||
for key, val in kwargs.items() | ||
} | ||
|
||
|
||
def _pretty_print_args_kwargs(*args: Any, **kwargs: Any) -> str: | ||
inputs_repr = ", ".join( | ||
arg._repr_str if isinstance(arg, Expression) else repr(arg) for arg in args | ||
) | ||
kwargs_repr = ", ".join( | ||
f"{k}={v._repr_str if isinstance(v, Expression) else v!r}" | ||
for k, v in kwargs.items() | ||
) | ||
|
||
all_args = [] | ||
if inputs_repr: | ||
all_args.append(inputs_repr) | ||
if kwargs_repr: | ||
all_args.append(kwargs_repr) | ||
|
||
return ", ".join(all_args) | ||
|
||
|
||
class Expression: | ||
""" | ||
Class representing a deferred column. | ||
|
||
This is not meant to be instantiated directly. Instead, use :meth:`pandas.col`. | ||
""" | ||
|
||
def __init__(self, func: Callable[[DataFrame], Any], repr_str: str) -> None: | ||
self._func = func | ||
self._repr_str = repr_str | ||
|
||
def __call__(self, df: DataFrame) -> Any: | ||
return self._func(df) | ||
|
||
def _with_binary_op(self, op: str, other: Any) -> Expression: | ||
op_symbol = _OP_SYMBOLS.get(op, op) | ||
|
||
if isinstance(other, Expression): | ||
if op.startswith("__r"): | ||
repr_str = f"({other._repr_str} {op_symbol} {self._repr_str})" | ||
else: | ||
repr_str = f"({self._repr_str} {op_symbol} {other._repr_str})" | ||
return Expression(lambda df: getattr(self(df), op)(other(df)), repr_str) | ||
else: | ||
if op.startswith("__r"): | ||
repr_str = f"({other!r} {op_symbol} {self._repr_str})" | ||
else: | ||
repr_str = f"({self._repr_str} {op_symbol} {other!r})" | ||
return Expression(lambda df: getattr(self(df), op)(other), repr_str) | ||
|
||
# Binary ops | ||
def __add__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__add__", other) | ||
|
||
def __radd__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__radd__", other) | ||
|
||
def __sub__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__sub__", other) | ||
|
||
def __rsub__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__rsub__", other) | ||
|
||
def __mul__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__mul__", other) | ||
|
||
def __rmul__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__rmul__", other) | ||
|
||
def __truediv__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__truediv__", other) | ||
|
||
def __rtruediv__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__rtruediv__", other) | ||
|
||
def __floordiv__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__floordiv__", other) | ||
|
||
def __rfloordiv__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__rfloordiv__", other) | ||
|
||
def __ge__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__ge__", other) | ||
|
||
def __gt__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__gt__", other) | ||
|
||
def __le__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__le__", other) | ||
|
||
def __lt__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__lt__", other) | ||
|
||
def __eq__(self, other: object) -> Expression: # type: ignore[override] | ||
return self._with_binary_op("__eq__", other) | ||
|
||
def __ne__(self, other: object) -> Expression: # type: ignore[override] | ||
return self._with_binary_op("__ne__", other) | ||
|
||
def __mod__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__mod__", other) | ||
|
||
def __rmod__(self, other: Any) -> Expression: | ||
return self._with_binary_op("__rmod__", other) | ||
|
||
def __array_ufunc__( | ||
self, ufunc: Callable[..., Any], method: str, *inputs: Any, **kwargs: Any | ||
) -> Expression: | ||
def func(df: DataFrame) -> Any: | ||
parsed_inputs = _parse_args(df, *inputs) | ||
parsed_kwargs = _parse_kwargs(df, *kwargs) | ||
return ufunc(*parsed_inputs, **parsed_kwargs) | ||
|
||
args_str = _pretty_print_args_kwargs(*inputs, **kwargs) | ||
repr_str = f"{ufunc.__name__}({args_str})" | ||
|
||
return Expression(func, repr_str) | ||
|
||
# Everything else | ||
def __getattr__(self, attr: str, /) -> Any: | ||
if attr in Series._accessors: | ||
return NamespaceExpression(self, attr) | ||
|
||
def func(df: DataFrame, *args: Any, **kwargs: Any) -> Any: | ||
parsed_args = _parse_args(df, *args) | ||
parsed_kwargs = _parse_kwargs(df, **kwargs) | ||
return getattr(self(df), attr)(*parsed_args, **parsed_kwargs) | ||
|
||
def wrapper(*args: Any, **kwargs: Any) -> Expression: | ||
args_str = _pretty_print_args_kwargs(*args, **kwargs) | ||
repr_str = f"{self._repr_str}.{attr}({args_str})" | ||
|
||
return Expression(lambda df: func(df, *args, **kwargs), repr_str) | ||
|
||
return wrapper | ||
|
||
def __repr__(self) -> str: | ||
return self._repr_str or "Expr(...)" | ||
|
||
|
||
class NamespaceExpression: | ||
def __init__(self, func: Expression, namespace: str) -> None: | ||
self._func = func | ||
self._namespace = namespace | ||
|
||
def __call__(self, df: DataFrame) -> Any: | ||
return self._func(df) | ||
|
||
def __getattr__(self, attr: str) -> Any: | ||
if isinstance(getattr(getattr(Series, self._namespace), attr), property): | ||
repr_str = f"{self._func._repr_str}.{self._namespace}.{attr}" | ||
return Expression( | ||
lambda df: getattr(getattr(self(df), self._namespace), attr), | ||
repr_str, | ||
) | ||
|
||
def func(df: DataFrame, *args: Any, **kwargs: Any) -> Any: | ||
parsed_args = _parse_args(df, *args) | ||
parsed_kwargs = _parse_kwargs(df, **kwargs) | ||
return getattr(getattr(self(df), self._namespace), attr)( | ||
*parsed_args, **parsed_kwargs | ||
) | ||
|
||
def wrapper(*args: Any, **kwargs: Any) -> Expression: | ||
args_str = _pretty_print_args_kwargs(*args, **kwargs) | ||
repr_str = f"{self._func._repr_str}.{self._namespace}.{attr}({args_str})" | ||
return Expression(lambda df: func(df, *args, **kwargs), repr_str) | ||
|
||
return wrapper | ||
|
||
|
||
def col(col_name: Hashable) -> Expression: | ||
""" | ||
Generate deferred object representing a column of a `DataFrame`. | ||
|
||
Any place which accepts ``lambda df: df[col_name]``, such as | ||
:meth:`DataFrame.assign` or :meth:`DataFrame.loc`, can also accept | ||
``pd.col(col_name)``. | ||
|
||
Arguments | ||
--------- | ||
col_name : Hashable | ||
Column name. | ||
|
||
Returns | ||
------- | ||
`pandas.api.typing.Expression` | ||
|
||
Examples | ||
-------- | ||
|
||
You can use `col` in `assign`. | ||
|
||
>>> df = pd.DataFrame({"name": ["beluga", "narwhal"], "speed": [100, 110]}) | ||
>>> df.assign(name_titlecase=pd.col("name").str.title()) | ||
name speed name_titlecase | ||
0 beluga 100 Beluga | ||
1 narwhal 110 Narwhal | ||
|
||
You can also use it for filtering. | ||
|
||
>>> df.loc[pd.col("speed") > 105] | ||
name speed | ||
1 narwhal 110 | ||
""" | ||
if not isinstance(col_name, Hashable): | ||
msg = f"Expected Hashable, got: {type(col_name)}" | ||
raise TypeError(msg) | ||
|
||
def func(df: DataFrame) -> Series: | ||
if col_name not in df.columns: | ||
columns_list = df.columns.tolist() | ||
max_cols = 10 | ||
if len(columns_list) > max_cols: | ||
columns_hint = columns_list[:max_cols] + ["..."] | ||
else: | ||
columns_hint = columns_list | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to base this on the length of the column names. If the column names were all of length 15 on average, you'd have a very long message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, done, thanks (using a totally arbitrary limit of 90, but i think it looks good) |
||
msg = ( | ||
f"Column '{col_name}' not found in given DataFrame.\n\n" | ||
f"Hint: did you mean one of {columns_hint} instead?" | ||
) | ||
raise ValueError(msg) | ||
return df[col_name] | ||
|
||
return Expression(func, f"col({col_name!r})") | ||
|
||
|
||
__all__ = ["Expression", "col"] |
Uh oh!
There was an error while loading. Please reload this page.