Skip to content

BUG: make JSONTableWriter fail if no index.name and 'index' in columns #58985

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

taranarmo
Copy link
Contributor

@taranarmo taranarmo commented Jun 12, 2024

This commit is intended to fix GH #58925. If index.name is empty it will use set_default_names inside init to make check on overlapping names fail. Otherwise it's done during schema creation and not reflected on the dataframe itself which creates inconsistency between the data and its schema.

Probably new behavior should be mentioned in the to_json docs? I added the docs change as a separate commit to ease squashing or reverting.

@@ -2287,6 +2287,8 @@ Please note that the literal string 'index' as the name of an :class:`Index`
is not round-trippable, nor are any names beginning with ``'level_'`` within a
:class:`MultiIndex`. These are used by default in :func:`DataFrame.to_json` to
indicate missing values and the subsequent read cannot distinguish the intent.
The string 'index' as a column name with empty :class:`Index` or if it is
Copy link
Member

Choose a reason for hiding this comment

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

Can you add this to the docstring instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added to the docstring into index keyword section

@mroeschke mroeschke added Error Reporting Incorrect or improved errors from pandas IO JSON read_json, to_json, json_normalize labels Jul 8, 2024
@taranarmo taranarmo force-pushed the make-jsontablewriter-fail-if-empty-index-name-and-index-is-a-column-name branch 2 times, most recently from cf81d79 to bf97c6a Compare July 8, 2024 17:27
This commit is itended to fix GH pandas-dev#58925. If index.name is empty it will
use set_default_names inside __init__ to make check on overlapping names
fail. Otherwise it's done during schema creation and not reflected on
the dataframe itself which creates inconsistency between the data and
its schema.

add mention of the raised error to the `to_json` documentation

move new logic description from IO docs to to_json docstring
@taranarmo taranarmo force-pushed the make-jsontablewriter-fail-if-empty-index-name-and-index-is-a-column-name branch from bf97c6a to 7d772d5 Compare July 8, 2024 19:40
Rephrase the what's new addition

Co-authored-by: Matthew Roeschke <[email protected]>
@mroeschke mroeschke added this to the 3.0 milestone Jul 8, 2024
@mroeschke mroeschke merged commit 374f386 into pandas-dev:main Jul 8, 2024
45 checks passed
@mroeschke
Copy link
Member

Thanks @taranarmo

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 IO JSON read_json, to_json, json_normalize
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG:
2 participants