-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Conversation
Not at a computer to check but why is this not optional? Wouldn't not specifying it just return a Series? |
emm, not sure about this behaviour, but from documentation, it is supposed to return a reshaped |
pandas/core/reshape/pivot.py
Outdated
@@ -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.") |
There was a problem hiding this comment.
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
pandas/tests/reshape/test_pivot.py
Outdated
df = pd.DataFrame( | ||
{"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]} | ||
) | ||
msg = 'pivot["(][")] missing 1 required argument: ["\']columns["\']' |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
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? |
Yea makes sense. Let's do 1.1 instead (comment still applies) |
pandas/tests/reshape/test_pivot.py
Outdated
df = pd.DataFrame( | ||
{"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]} | ||
) | ||
msg = 'pivot["(][")] missing 1 required argument: ["\']columns["\']' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
msg = 'pivot["(][")] missing 1 required argument: ["\']columns["\']' | |
msg = re.escape("pivot() missing 1 required argument: 'columns'") |
also need to import re
There was a problem hiding this comment.
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
thanks @WillAyd and @simonjayhawkins I will add a whatsnew note once 1.1 is there |
ping @WillAyd @simonjayhawkins |
Thanks @charlesdong1991 lgtm. @WillAyd |
There was a problem hiding this 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
pandas/core/reshape/pivot.py
Outdated
@@ -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'") |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get it! thanks!
ping @jreback |
pandas/tests/reshape/test_pivot.py
Outdated
df = pd.DataFrame( | ||
{"col1": ["a", "b", "c"], "col2": [1, 2, 3], "col3": [1, 2, 3]} | ||
) | ||
msg = re.escape("pivot() missing 1 required argument: 'columns'") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
doc/source/whatsnew/v1.1.0.rst
Outdated
@@ -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`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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
ping @WillAyd |
thanks @charlesdong1991 |
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff