Skip to content

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

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

Merged
merged 11 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
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")

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_axis_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