Skip to content
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

Merged
merged 12 commits into from
May 11, 2023

Conversation

dshemetov
Copy link
Contributor

@dshemetov dshemetov commented Mar 23, 2023

Updates to_json with less surprising behavior with the index arg:

- 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
@dshemetov
Copy link
Contributor Author

Hey @WillAyd, this is a PR for your request here. Let me know who I can tag for review.

@mroeschke mroeschke requested a review from WillAyd March 27, 2023 21:47
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
Copy link
Member

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

Copy link
Contributor Author

@dshemetov dshemetov Mar 28, 2023

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!

@dshemetov dshemetov requested a review from WillAyd April 3, 2023 18:30
"'index=False' is only valid when 'orient' is 'split', 'table', "
"'records', or 'values'."
)
elif index and orient in ["records", "values"]:
Copy link
Member

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?

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"]:
Copy link
Member

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?

@dshemetov
Copy link
Contributor Author

Consolidated the checks up front, hopefully this is clearer

@dshemetov dshemetov requested a review from WillAyd April 6, 2023 22:13
@github-actions
Copy link
Contributor

github-actions bot commented May 7, 2023

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.

@github-actions github-actions bot added the Stale label May 7, 2023
@dshemetov
Copy link
Contributor Author

Waiting on review, friendly ping @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.

I think looks good. This could use a whatsnew note for 2.1. @mroeschke any thoughts on your end?

pandas/io/json/_json.py Show resolved Hide resolved
@WillAyd
Copy link
Member

WillAyd commented May 9, 2023

Looks good. Can you add the 2.1 whatsnew note?

@dshemetov
Copy link
Contributor Author

Took a stab at it, lmk if the whatsnew note looks good (never done one before)

@mroeschke
Copy link
Member

 sort whatsnew entries alphabetically....................................................................Failed
- hook id: sort-whatsnew-items
- duration: 0.05s
- exit code: 1
- files were modified by this hook
pre-commit hook(s) made changes.
If you are seeing this message in CI, reproduce locally with: `pre-commit run --all-files`.
To run `pre-commit` as part of git workflow, use `pre-commit install`.
All changes made by hooks:
diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst
index 56b7327..e0f417e 100644
--- a/doc/source/whatsnew/v2.1.0.rst
+++ b/doc/source/whatsnew/v2.1.0.rst
@@ -96,9 +96,9 @@ Other enhancements
 - Let :meth:`DataFrame.to_feather` accept a non-default :class:`Index` and non-string column names (:issue:`51787`)
 - Performance improvement in :func:`read_csv` (:issue:`52632`) with ``engine="c"``
 - :meth:`Categorical.from_codes` has gotten a ``validate`` parameter (:issue:`50975`)
+- Improved error messaging when using :meth:`DataFrame.to_json` with incompatible ``index`` and ``orient`` arguments (:issue:`52143`)
 - Performance improvement in :func:`concat` with homogeneous ``np.float64`` or ``np.float32`` dtypes (:issue:`52685`)
 - Performance improvement in :meth:`DataFrame.filter` when ``items`` is given (:issue:`52941`)
-- Improved error messaging when using :meth:`DataFrame.to_json` with incompatible ``index`` and ``orient`` arguments (:issue:`52143`)

@dshemetov
Copy link
Contributor Author

@mroeschke sorted!

@mroeschke mroeschke added this to the 2.1 milestone May 11, 2023
@mroeschke mroeschke merged commit 0a18ad7 into pandas-dev:main May 11, 2023
@mroeschke
Copy link
Member

Great! Thanks @dshemetov

@dshemetov dshemetov deleted the ds/to_json branch May 11, 2023 05:11
Rylie-W pushed a commit to Rylie-W/pandas that referenced this pull request May 19, 2023
…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
Daquisu pushed a commit to Daquisu/pandas that referenced this pull request Jul 8, 2023
…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
ajnelson-nist added a commit to casework/CASE-Utilities-Python that referenced this pull request Oct 26, 2023
A follow-on patch will regenerate Make-managed files.

References:
* pandas-dev/pandas#52143

Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
@paddymul
Copy link
Contributor

paddymul commented Apr 5, 2024

Hey all, for what it's worth I think I'm running into a segfault in release 2.0.3 that was fixed by this PR. Here is my bug report. If it wasn't this PR, it was something that was fixed between 2.0.3 and 2.1.0

#58160

@dshemetov
Copy link
Contributor Author

dshemetov commented Apr 5, 2024

This PR only disallowed certain combinations of arguments to to_json(), so I don't think it's the cause of the segfault in your report. I don't have any ideas that can help you off the top of my head, but I'll let you know if I do. Best of luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants