-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
API/BUG: Make to_json
index=
arg consistent with orient
arg
#52143
Conversation
- split and table allow index=True/False - records and values only allow index=False - index and columns only allow index=True - raise for contradictions in the latter two - see pandas-dev#25513
Whether to include the index values in the JSON string. Not | ||
including the index (``index=False``) is only supported when | ||
orient is 'split' or 'table'. | ||
index : bool or None, default None |
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.
I think it would be easier to just say that the index is only used in split, index, column and table orients. Of those formats, index and column cannot be False.
You are kind of doing this now but I think in a way that is a bit more confusing. If you structure the commentary and code this will I think will help simplify the logic
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.
Made some changes, let me know what you think!
pandas/io/json/_json.py
Outdated
"'index=False' is only valid when 'orient' is 'split', 'table', " | ||
"'records', or 'values'." | ||
) | ||
elif index and orient in ["records", "values"]: |
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.
The orient check is the same here as on L149 - can these not be consolidated?
pandas/io/json/_json.py
Outdated
indent: int = 0, | ||
storage_options: StorageOptions = None, | ||
mode: Literal["a", "w"] = "w", | ||
) -> str | None: | ||
if not index and orient not in ["split", "table"]: | ||
if index is None and orient in ["records", "values"]: |
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.
Is this branch necessary? Or can you just change L151 to set it to True for appropriate orients?
Consolidated the checks up front, hopefully this is clearer |
This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this. |
Waiting on review, friendly ping @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.
I think looks good. This could use a whatsnew note for 2.1. @mroeschke any thoughts on your end?
Looks good. Can you add the 2.1 whatsnew note? |
Took a stab at it, lmk if the whatsnew note looks good (never done one before) |
|
@mroeschke sorted! |
Great! Thanks @dshemetov |
…ndas-dev#52143) * API/BUG: Make to_json index= consistent with orient - split and table allow index=True/False - records and values only allow index=False - index and columns only allow index=True - raise for contradictions in the latter two - see pandas-dev#25513 * style: lint * style: make mypy happy * review: simplify * review: clarify and consolidate branches * style: add explainer comment * doc: change error message in _json * docs: update whatsnew 2.1.0 * docs: sort whatsnew
…ndas-dev#52143) * API/BUG: Make to_json index= consistent with orient - split and table allow index=True/False - records and values only allow index=False - index and columns only allow index=True - raise for contradictions in the latter two - see pandas-dev#25513 * style: lint * style: make mypy happy * review: simplify * review: clarify and consolidate branches * style: add explainer comment * doc: change error message in _json * docs: update whatsnew 2.1.0 * docs: sort whatsnew
A follow-on patch will regenerate Make-managed files. References: * pandas-dev/pandas#52143 Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
Hey all, for what it's worth I think I'm running into a segfault in release |
This PR only disallowed certain combinations of arguments to |
Updates
to_json
with less surprising behavior with theindex
arg:split and table allow index=True/False (as before)
records and values only allow index=False
index and columns only allow index=True
raise for contradictions in the latter two and update messages
add test
closes DataFrame.to_json silently ignores index parameter for most orients. #25513, closes BUG: DataFrame.to_json ignores index if it is repeated in the DataFrame. #37600
Tests added and passed if fixing a bug or adding a new feature
All code checks passed.
Added type annotations to new arguments/methods/functions.
Added an entry in the latest
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.