Skip to content

BUG: JSON serialization with orient split fails roundtrip with MultiIndex #50904

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,7 @@ I/O
- Fixed memory leak which stemmed from the initialization of the internal JSON module (:issue:`49222`)
- Fixed issue where :func:`json_normalize` would incorrectly remove leading characters from column names that matched the ``sep`` argument (:issue:`49861`)
- Bug in :meth:`DataFrame.to_json` where it would segfault when failing to encode a string (:issue:`50307`)
- Bug in :meth:`DataFrame.to_json` where it would produce duplicate column names for orient=split (:issue:`50456`)

Period
^^^^^^
Expand Down
14 changes: 14 additions & 0 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ def to_json(
indent=indent,
).write()

if orient == "split" and isinstance(obj, DataFrame):
if isinstance(obj.columns, MultiIndex):
lst = []
# backwards of multindex.fromArray
for i in range(len(obj.columns[0])):
sub = []
for j in range(len(obj.columns)):
sub.append(obj.columns[j][i])
lst.append(sub)
newS = loads(s)
# fixes columns to original columns
newS["columns"] = lst
s = dumps(newS)

if lines:
s = convert_to_line_delimits(s)

Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/io/json/Untitled-1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
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 not be commited

"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
7 changes: 0 additions & 7 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,13 +1528,6 @@ def test_timedelta_as_label(self, date_format, key):
("index", "{\"('a', 'b')\":{\"('c', 'd')\":1}}"),
("columns", "{\"('c', 'd')\":{\"('a', 'b')\":1}}"),
# TODO: the below have separate encoding procedures
pytest.param(
"split",
"",
marks=pytest.mark.xfail(
reason="Produces JSON but not in a consistent manner"
),
),
Copy link
Member

Choose a reason for hiding this comment

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

This is not the test, the test is test_roundtrip_multiindex and what needs to be removed is the xfail decorator.

pytest.param(
"table",
"",
Expand Down
3 changes: 3 additions & 0 deletions pp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pandas
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 not be commited either

print(pandas.__version__)
!pip install pytest