Skip to content

Commit edb0e38

Browse files
committed
truncate output for wide dataframes
1 parent 9fed80e commit edb0e38

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

pandas/core/col.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,14 @@ def col(col_name: Hashable) -> Expression:
260260

261261
def func(df: DataFrame) -> Series:
262262
if col_name not in df.columns:
263+
columns_list = df.columns.tolist()
264+
if len(columns_list) > 10:
265+
columns_hint = columns_list[:10] + ["..."]
266+
else:
267+
columns_hint = columns_list
263268
msg = (
264269
f"Column '{col_name}' not found in given DataFrame.\n\n"
265-
f"Hint: did you mean one of {df.columns.tolist()} instead?"
270+
f"Hint: did you mean one of {columns_hint} instead?"
266271
)
267272
raise ValueError(msg)
268273
return df[col_name]

pandas/tests/test_col.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,19 @@ def test_namespaces(
6767

6868

6969
def test_invalid() -> None:
70-
df = pd.DataFrame({"a": [1, 2]})
71-
with pytest.raises(ValueError, match="did you mean"):
72-
df.assign(c=pd.col("b").mean())
70+
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
71+
with pytest.raises(ValueError, match=r"did you mean one of \['a', 'b'\] instead"):
72+
df.assign(c=pd.col("c").mean())
73+
df = pd.DataFrame({f"col_{i}": [0] for i in range(11)})
74+
msg = (
75+
"did you mean one of "
76+
r"\['col_0', 'col_1', 'col_2', 'col_3', "
77+
"'col_4', 'col_5', 'col_6', 'col_7', "
78+
r"'col_8', 'col_9', '\.\.\.'\] instead"
79+
)
80+
""
81+
with pytest.raises(ValueError, match=msg):
82+
df.assign(c=pd.col("c").mean())
7383

7484

7585
def test_custom_accessor() -> None:

0 commit comments

Comments
 (0)