Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
3d17e56
ENH: Introduce `pandas.col`
MarcoGorelli Aug 13, 2025
9fcaba3
api test, typing
MarcoGorelli Aug 13, 2025
b41b99d
typing
MarcoGorelli Aug 14, 2025
60c09c2
add pretty repr
MarcoGorelli Aug 14, 2025
9e4e0c5
improve error message
MarcoGorelli Aug 16, 2025
fe78aa2
test repr
MarcoGorelli Aug 16, 2025
04044af
test namespaces
MarcoGorelli Aug 16, 2025
a95aeb4
docs
MarcoGorelli Aug 16, 2025
4dc8e55
reference in dsintro
MarcoGorelli Aug 16, 2025
13d8e5c
Merge remote-tracking branch 'upstream/main' into pandas-col
MarcoGorelli Aug 16, 2025
e2aeb4f
fixup link
MarcoGorelli Aug 16, 2025
fa3e793
fixup docs
MarcoGorelli Aug 16, 2025
0bc918a
fixup
MarcoGorelli Aug 16, 2025
a0939f9
add test file
MarcoGorelli Aug 16, 2025
a703982
simplify, support custom series extensions too
MarcoGorelli Aug 17, 2025
48228cc
test accessor
MarcoGorelli Aug 17, 2025
d6f55a1
:pencil: fix typo
MarcoGorelli Aug 17, 2025
b2ed136
typing
MarcoGorelli Aug 17, 2025
c8f0193
move Expr to api.typing
MarcoGorelli Aug 17, 2025
e6ea343
move Expr to api/typing
MarcoGorelli Aug 17, 2025
96990d6
rename Expr to Expression
MarcoGorelli Aug 17, 2025
548ee20
fix return type
MarcoGorelli Aug 17, 2025
cfbd5a3
support NumPy ufuncs
MarcoGorelli Aug 19, 2025
e74438c
support NumPy ufuncs too
MarcoGorelli Aug 19, 2025
b4de244
Merge remote-tracking branch 'upstream/main' into pandas-col
MarcoGorelli Aug 19, 2025
31192e0
Merge branch 'pandas-col' of github.com:MarcoGorelli/pandas into pand…
MarcoGorelli Aug 19, 2025
83b70e8
simplify repr_str type
MarcoGorelli Aug 19, 2025
3b6906b
fix typing, avoid floating point inaccuracies
MarcoGorelli Aug 19, 2025
9fed80e
add to api reference
MarcoGorelli Aug 19, 2025
edb0e38
truncate output for wide dataframes
MarcoGorelli Aug 19, 2025
72faba9
make `max_cols` variable
MarcoGorelli Aug 19, 2025
b6f4961
truncate based on message length rather than number of columns
MarcoGorelli Aug 19, 2025
3791cf6
fixup docstring
MarcoGorelli Aug 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/reference/general_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Top-level evaluation
.. autosummary::
:toctree: api/

col
eval

Datetime formats
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/col.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _parse_args(df: DataFrame, *args: Any) -> tuple[Series]:
return tuple([x(df) if isinstance(x, Expression) else x for x in args])


def _parse_kwargs(df: DataFrame, **kwargs: Any) -> dict[Hashable, Series]:
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
Expand Down Expand Up @@ -260,9 +260,15 @@ def col(col_name: Hashable) -> Expression:

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

@MarcoGorelli MarcoGorelli Aug 19, 2025

Choose a reason for hiding this comment

The 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 {df.columns.tolist()} instead?"
f"Hint: did you mean one of {columns_hint} instead?"
)
raise ValueError(msg)
return df[col_name]
Expand Down
18 changes: 14 additions & 4 deletions pandas/tests/test_col.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
(pd.col("a") < 1, [False, False], "(col('a') < 1)"),
(pd.col("a") <= 1, [True, False], "(col('a') <= 1)"),
(pd.col("a") == 1, [True, False], "(col('a') == 1)"),
(np.log(pd.col("a")), [0.0, 0.6931471805599453], "log(col('a'))"),
(np.power(pd.col("a"), 2), [1, 4], "power(col('a'), 2)"),
(np.divide(pd.col("a"), pd.col("a")), [1.0, 1.0], "divide(col('a'), col('a'))"),
],
)
Expand Down Expand Up @@ -67,9 +67,19 @@ def test_namespaces(


def test_invalid() -> None:
df = pd.DataFrame({"a": [1, 2]})
with pytest.raises(ValueError, match="did you mean"):
df.assign(c=pd.col("b").mean())
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
with pytest.raises(ValueError, match=r"did you mean one of \['a', 'b'\] instead"):
df.assign(c=pd.col("c").mean())
df = pd.DataFrame({f"col_{i}": [0] for i in range(11)})
msg = (
"did you mean one of "
r"\['col_0', 'col_1', 'col_2', 'col_3', "
"'col_4', 'col_5', 'col_6', 'col_7', "
r"'col_8', 'col_9', '\.\.\.'\] instead"
)
""
with pytest.raises(ValueError, match=msg):
df.assign(c=pd.col("c").mean())


def test_custom_accessor() -> None:
Expand Down
Loading