Skip to content
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

BUG: Improve error message when tansfrom() with incorrect axis #58494

Merged
merged 11 commits into from
May 10, 2024
2 changes: 1 addition & 1 deletion pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ def normalize_dictlike_arg(

cols = Index(list(func.keys())).difference(obj.columns, sort=True)
if len(cols) > 0:
raise KeyError(f"Column(s) {list(cols)} do not exist")
raise KeyError(f"Row(s) or Column(s) {list(cols)} do not exist")
luke396 marked this conversation as resolved.
Show resolved Hide resolved

aggregator_types = (list, tuple, dict)

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/apply/test_invalid_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,15 @@ def test_transform_reducer_raises(all_reductions, frame_or_series, op_wrapper):
msg = "Function did not transform"
with pytest.raises(ValueError, match=msg):
obj.transform(op)


def test_transform_missing_labels_raises():
# GH 58474
df = DataFrame({"foo": [2, 4, 6], "bar": [1, 2, 3]}, index=["A", "B", "C"])
msg = r"Row\(s\) or Column\(s\) \['A', 'B'\] do not exist"
with pytest.raises(KeyError, match=msg):
df.transform({"A": lambda x: x + 2, "B": lambda x: x * 2}, axis=0)

msg = r"Row\(s\) or Column\(s\) \['bar', 'foo'\] do not exist"
with pytest.raises(KeyError, match=msg):
df.transform({"foo": lambda x: x + 2, "bar": lambda x: x * 2}, axis=1)
Loading