Skip to content

Commit 4d9ffcf

Browse files
author
Zhengbo Wang
authored
BUG: Improve error message when transfrom() with incorrect axis (#58494)
1 parent 1c23c5e commit 4d9ffcf

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

pandas/core/apply.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,8 @@ def normalize_dictlike_arg(
628628

629629
cols = Index(list(func.keys())).difference(obj.columns, sort=True)
630630
if len(cols) > 0:
631-
raise KeyError(f"Column(s) {list(cols)} do not exist")
631+
# GH 58474
632+
raise KeyError(f"Label(s) {list(cols)} do not exist")
632633

633634
aggregator_types = (list, tuple, dict)
634635

pandas/tests/apply/test_invalid_arg.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ def test_dict_nested_renaming_depr(method):
118118
def test_missing_column(method, func):
119119
# GH 40004
120120
obj = DataFrame({"A": [1]})
121-
match = re.escape("Column(s) ['B'] do not exist")
122-
with pytest.raises(KeyError, match=match):
121+
msg = r"Label\(s\) \['B'\] do not exist"
122+
with pytest.raises(KeyError, match=msg):
123123
getattr(obj, method)(func)
124124

125125

126126
def test_transform_mixed_column_name_dtypes():
127127
# GH39025
128128
df = DataFrame({"a": ["1"]})
129-
msg = r"Column\(s\) \[1, 'b'\] do not exist"
129+
msg = r"Label\(s\) \[1, 'b'\] do not exist"
130130
with pytest.raises(KeyError, match=msg):
131131
df.transform({"a": int, 1: str, "b": int})
132132

@@ -359,3 +359,15 @@ def test_transform_reducer_raises(all_reductions, frame_or_series, op_wrapper):
359359
msg = "Function did not transform"
360360
with pytest.raises(ValueError, match=msg):
361361
obj.transform(op)
362+
363+
364+
def test_transform_missing_labels_raises():
365+
# GH 58474
366+
df = DataFrame({"foo": [2, 4, 6], "bar": [1, 2, 3]}, index=["A", "B", "C"])
367+
msg = r"Label\(s\) \['A', 'B'\] do not exist"
368+
with pytest.raises(KeyError, match=msg):
369+
df.transform({"A": lambda x: x + 2, "B": lambda x: x * 2}, axis=0)
370+
371+
msg = r"Label\(s\) \['bar', 'foo'\] do not exist"
372+
with pytest.raises(KeyError, match=msg):
373+
df.transform({"foo": lambda x: x + 2, "bar": lambda x: x * 2}, axis=1)

pandas/tests/groupby/aggregate/test_aggregate.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import datetime
66
import functools
77
from functools import partial
8-
import re
98

109
import numpy as np
1110
import pytest
@@ -816,8 +815,8 @@ def test_agg_relabel_other_raises(self):
816815

817816
def test_missing_raises(self):
818817
df = DataFrame({"A": [0, 1], "B": [1, 2]})
819-
match = re.escape("Column(s) ['C'] do not exist")
820-
with pytest.raises(KeyError, match=match):
818+
msg = r"Label\(s\) \['C'\] do not exist"
819+
with pytest.raises(KeyError, match=msg):
821820
df.groupby("A").agg(c=("C", "sum"))
822821

823822
def test_agg_namedtuple(self):

pandas/tests/groupby/aggregate/test_other.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def test_aggregate_api_consistency():
209209
expected = pd.concat([c_mean, c_sum, d_mean, d_sum], axis=1)
210210
expected.columns = MultiIndex.from_product([["C", "D"], ["mean", "sum"]])
211211

212-
msg = r"Column\(s\) \['r', 'r2'\] do not exist"
212+
msg = r"Label\(s\) \['r', 'r2'\] do not exist"
213213
with pytest.raises(KeyError, match=msg):
214214
grouped[["D", "C"]].agg({"r": "sum", "r2": "mean"})
215215

@@ -224,7 +224,7 @@ def test_agg_dict_renaming_deprecation():
224224
{"B": {"foo": ["sum", "max"]}, "C": {"bar": ["count", "min"]}}
225225
)
226226

227-
msg = r"Column\(s\) \['ma'\] do not exist"
227+
msg = r"Label\(s\) \['ma'\] do not exist"
228228
with pytest.raises(KeyError, match=msg):
229229
df.groupby("A")[["B", "C"]].agg({"ma": "max"})
230230

pandas/tests/resample/test_resample_api.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_agg_consistency():
328328

329329
r = df.resample("3min")
330330

331-
msg = r"Column\(s\) \['r1', 'r2'\] do not exist"
331+
msg = r"Label\(s\) \['r1', 'r2'\] do not exist"
332332
with pytest.raises(KeyError, match=msg):
333333
r.agg({"r1": "mean", "r2": "sum"})
334334

@@ -343,7 +343,7 @@ def test_agg_consistency_int_str_column_mix():
343343

344344
r = df.resample("3min")
345345

346-
msg = r"Column\(s\) \[2, 'b'\] do not exist"
346+
msg = r"Label\(s\) \[2, 'b'\] do not exist"
347347
with pytest.raises(KeyError, match=msg):
348348
r.agg({2: "mean", "b": "sum"})
349349

@@ -534,7 +534,7 @@ def test_agg_with_lambda(cases, agg):
534534
],
535535
)
536536
def test_agg_no_column(cases, agg):
537-
msg = r"Column\(s\) \['result1', 'result2'\] do not exist"
537+
msg = r"Label\(s\) \['result1', 'result2'\] do not exist"
538538
with pytest.raises(KeyError, match=msg):
539539
cases[["A", "B"]].agg(**agg)
540540

@@ -582,7 +582,7 @@ def test_agg_specificationerror_series(cases, agg):
582582
def test_agg_specificationerror_invalid_names(cases):
583583
# errors
584584
# invalid names in the agg specification
585-
msg = r"Column\(s\) \['B'\] do not exist"
585+
msg = r"Label\(s\) \['B'\] do not exist"
586586
with pytest.raises(KeyError, match=msg):
587587
cases[["A"]].agg({"A": ["sum", "std"], "B": ["mean", "std"]})
588588

@@ -631,7 +631,7 @@ def test_try_aggregate_non_existing_column():
631631
df = DataFrame(data).set_index("dt")
632632

633633
# Error as we don't have 'z' column
634-
msg = r"Column\(s\) \['z'\] do not exist"
634+
msg = r"Label\(s\) \['z'\] do not exist"
635635
with pytest.raises(KeyError, match=msg):
636636
df.resample("30min").agg({"x": ["mean"], "y": ["median"], "z": ["sum"]})
637637

0 commit comments

Comments
 (0)