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

Conversation

charlesdong1991
Copy link
Member

@charlesdong1991 charlesdong1991 commented Jan 11, 2020

@WillAyd
Copy link
Member

WillAyd commented Jan 11, 2020

Not at a computer to check but why is this not optional? Wouldn't not specifying it just return a Series?

@WillAyd WillAyd added the Reshaping Concat, Merge/Join, Stack/Unstack, Explode label Jan 11, 2020
@charlesdong1991
Copy link
Member Author

charlesdong1991 commented Jan 12, 2020

emm, not sure about this behaviour, but from documentation, it is supposed to return a reshaped DataFrame, so probably this is why this is not optional @WillAyd

@@ -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("`columns` is not optional.")
Copy link
Member

Choose a reason for hiding this comment

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

since I don't think we can change the function signature, then to be semi consistent with standard error messages, then maybe TypeError: pivot() missing 1 required argument: 'columns'

(or an enhancement to the code so that columns is the remaining columns not in index and values arguments, and then raise a message that at least one of columns and values must be specified

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?

@WillAyd
Copy link
Member

WillAyd commented Jan 13, 2020

OK I think I see the point here. If you can address @simonjayhawkins comments and put a note in for 1.0.1 when available I think should be good

@simonjayhawkins
Copy link
Member

OK I think I see the point here. If you can address @simonjayhawkins comments and put a note in for 1.0.1 when available I think should be good

@WillAyd maybe i missed the discussion, but the first patch release following 1.0.0 will be for regressions only. This should be 1.1.0?

@WillAyd
Copy link
Member

WillAyd commented Jan 13, 2020

Yea makes sense. Let's do 1.1 instead (comment still applies)

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.

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

@simonjayhawkins simonjayhawkins added the Error Reporting Incorrect or improved errors from pandas label Jan 13, 2020
@simonjayhawkins simonjayhawkins added this to the 1.1 milestone Jan 13, 2020
@charlesdong1991
Copy link
Member Author

thanks @WillAyd and @simonjayhawkins I will add a whatsnew note once 1.1 is there

@charlesdong1991
Copy link
Member Author

ping @WillAyd @simonjayhawkins

@simonjayhawkins
Copy link
Member

Thanks @charlesdong1991 lgtm. @WillAyd

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

@jreback in case there's any historical reason not to do this, but otherwise looks close

@@ -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!

@charlesdong1991
Copy link
Member Author

ping @jreback

df = pd.DataFrame(
{"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]}
)
msg = re.escape("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.

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

Minor nit but just use raw strings no need to import re for this

@@ -141,6 +141,8 @@ Reshaping

-
- Bug in :meth:`DataFrame.pivot_table` when only MultiIndexed columns is set (:issue:`17038`)
- Correct incorrect error message in :meth:`DataFrame.pivot` when ``columns`` is set to ``None``. (:issue:`30924`)
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
- Correct incorrect error message in :meth:`DataFrame.pivot` when ``columns`` is set to ``None``. (:issue:`30924`)
- Fix incorrect error message in :meth:`DataFrame.pivot` when ``columns`` is set to ``None``. (:issue:`30924`)

Just reads a little better

@charlesdong1991
Copy link
Member Author

ping @WillAyd

@jreback jreback merged commit f792d8c into pandas-dev:master Jan 18, 2020
@jreback
Copy link
Contributor

jreback commented Jan 18, 2020

thanks @charlesdong1991

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Wrong error message is raised when columns=None in df.pivot
4 participants