Skip to content

Commit e85dafe

Browse files
committed
BUG:pandas-dev#29928 Fix to_json output with 'table' orient for single level MultiIndex.
Index field name in written json was incorrect, so applying read_json resulted in NaN index values. Dataframe to_json with 'table' orient now treats single level MultiIndex like single Index.
1 parent c45da41 commit e85dafe

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ I/O
503503
- Bug in :class:`HDFStore` was dropping timezone information when exporting :class:`Series` with ``datetime64[ns, tz]`` dtypes with a fixed HDF5 store (:issue:`20594`)
504504
- :func:`read_csv` was closing user-provided binary file handles when ``engine="c"`` and an ``encoding`` was requested (:issue:`36980`)
505505
- Bug in :meth:`DataFrame.to_hdf` was not dropping missing rows with ``dropna=True`` (:issue:`35719`)
506+
- Bug in :meth:`~DataFrame.to_json` with 'table' orient was writting wrong index field name for MultiIndex Dataframe with a single level (:issue:`29928`)
506507

507508
Plotting
508509
^^^^^^^^

pandas/io/json/_json.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,6 @@ def __init__(
259259
)
260260
raise ValueError(msg)
261261

262-
self.schema = build_table_schema(obj, index=self.index)
263-
264262
# NotImplemented on a column MultiIndex
265263
if obj.ndim == 2 and isinstance(obj.columns, MultiIndex):
266264
raise NotImplementedError(
@@ -277,10 +275,17 @@ def __init__(
277275
raise ValueError(msg)
278276

279277
obj = obj.copy()
278+
279+
# Convert DataFrame to handled types before serializing
280+
if obj.index.nlevels == 1 and isinstance(obj.index, MultiIndex):
281+
obj.index = obj.index.get_level_values(0)
282+
283+
self.schema = build_table_schema(obj, index=self.index)
284+
280285
timedeltas = obj.select_dtypes(include=["timedelta"]).columns
281286
if len(timedeltas):
282287
obj[timedeltas] = obj[timedeltas].applymap(lambda x: x.isoformat())
283-
# Convert PeriodIndex to datetimes before serializing
288+
284289
if is_period_dtype(obj.index.dtype):
285290
obj.index = obj.index.to_timestamp()
286291

pandas/tests/io/json/test_json_table_schema.py

+17
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,23 @@ def test_to_json_categorical_index(self):
435435

436436
assert result == expected
437437

438+
@pytest.mark.parametrize("name", [None, "foo"])
439+
def test_multiindex_single_level(self, name):
440+
# GH29928
441+
index = pd.Index([1, 2, 3, 4], name=name)
442+
expected = DataFrame(
443+
data=[[1, 1], [2, 2], [3, 3], [4, 4]], columns=["A", "B"], index=index
444+
)
445+
446+
index = pd.MultiIndex.from_tuples([(1,), (2,), (3,), (4,)], names=[name])
447+
df = DataFrame(
448+
data=[[1, 1], [2, 2], [3, 3], [4, 4]], columns=["A", "B"], index=index
449+
)
450+
js = df.to_json(orient="table")
451+
result = pd.read_json(js, orient="table")
452+
453+
tm.assert_frame_equal(result, expected)
454+
438455
@pytest.mark.filterwarnings(
439456
"ignore:an integer is required (got type float)*:DeprecationWarning"
440457
)

0 commit comments

Comments
 (0)