Skip to content

Commit 802d8b6

Browse files
committed
BUG: Fix read_json shape error on multi-indexed DF/SR with orient='split' pandas-dev#4889
1 parent 55e8891 commit 802d8b6

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

pandas/io/json/_json.py

+4
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,8 @@ def _parse_no_numpy(self):
10041004
if self.orient == "split":
10051005
decoded = {str(k): v for k, v in data.items()}
10061006
self.check_keys_split(decoded)
1007+
if "index" in decoded:
1008+
decoded["index"] = np.transpose(decoded["index"]).tolist()
10071009
self.obj = create_series_with_explicit_dtype(**decoded)
10081010
else:
10091011
self.obj = create_series_with_explicit_dtype(data, dtype_if_empty=object)
@@ -1095,6 +1097,8 @@ def _parse_no_numpy(self):
10951097
for k, v in loads(json, precise_float=self.precise_float).items()
10961098
}
10971099
self.check_keys_split(decoded)
1100+
if "index" in decoded:
1101+
decoded["index"] = np.transpose(decoded["index"]).tolist()
10981102
self.obj = DataFrame(dtype=None, **decoded)
10991103
elif orient == "index":
11001104
self.obj = DataFrame.from_dict(

pandas/tests/io/json/test_pandas.py

+16
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,22 @@ def test_index_false_to_json_split(self, data, expected):
13941394

13951395
assert result == expected
13961396

1397+
def test_read_json_frame_mutltiindex_split(self):
1398+
index = pd.MultiIndex.from_tuples([(1, 1), (2, 1), (1, 2), (2, 2)])
1399+
expected = pd.DataFrame(
1400+
data=[[1, 1], [2, 2], [3, 3], [4, 4]], columns=["A", "B"], index=index
1401+
)
1402+
js = expected.to_json(orient="split")
1403+
result = pd.read_json(js, orient="split")
1404+
tm.assert_frame_equal(expected, result)
1405+
1406+
def test_read_json_series_mutltiindex_split(self):
1407+
index = pd.MultiIndex.from_tuples([(1, 1), (2, 1), (1, 2), (2, 2)])
1408+
expected = pd.Series(data=[1, 2, 3, 4], index=index)
1409+
js = expected.to_json(orient="split")
1410+
result = pd.read_json(js, orient="split", typ="series")
1411+
tm.assert_series_equal(expected, result)
1412+
13971413
@pytest.mark.parametrize(
13981414
"data",
13991415
[

0 commit comments

Comments
 (0)