Skip to content

BUG: correct wrong error message in df.pivot when columns=None #30925

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 13 commits into from
Jan 18, 2020
3 changes: 3 additions & 0 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ def _convert_by(by):
@Substitution("\ndata : DataFrame")
@Appender(_shared_docs["pivot"], indents=1)
def pivot(data: "DataFrame", index=None, columns=None, values=None) -> "DataFrame":
if columns is None:
raise ValueError("pivot() missing 1 required argument: 'columns'")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a TypeError no? I think @simonjayhawkins had that original comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i thought ValueError is more appropriate here for this case.

I changed to TypeError as suggested by @simonjayhawkins ! thanks both!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a TypeError no? I think @simonjayhawkins had that original comment

I think it should be TypeError, however, I would regard a change from ValueError to TypeError as a breaking change. Not sure what others think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm. The exception in the issue is a KeyError, so either is a change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get it! thanks!


if values is None:
cols = [columns] if index is None else [index, columns]
append = index is None
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,15 @@ def test_pivot_with_list_like_values_nans(self, values, method):
expected = DataFrame(data=data, index=index, columns=columns, dtype="object")
tm.assert_frame_equal(result, expected)

def test_pivot_columns_none_raise_error(self):
# GH 30924
df = pd.DataFrame(
{"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]}
)
msg = 'pivot["(][")] missing 1 required argument: ["\']columns["\']'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you use re.escape, this may be more readable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your quick response, i tried re.escape, but it seemed to work in an opposite way. @simonjayhawkins could you pls enlighten me a bit?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
msg = 'pivot["(][")] missing 1 required argument: ["\']columns["\']'
msg = re.escape("pivot() missing 1 required argument: 'columns'")

also need to import re

Copy link
Member Author

@charlesdong1991 charlesdong1991 Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, i see!!! thanks! @simonjayhawkins

with pytest.raises(ValueError, match=msg):
df.pivot(index="col1", values="col3")

@pytest.mark.xfail(
reason="MultiIndexed unstack with tuple names fails with KeyError GH#19966"
)
Expand Down