Skip to content

fixed issue, multiindex dataframe now created as expected #56415

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
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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ I/O
- Bug in :func:`read_json` not handling dtype conversion properly if ``infer_string`` is set (:issue:`56195`)
- Bug in :meth:`DataFrame.to_excel`, with ``OdsWriter`` (``ods`` files) writing boolean/string value (:issue:`54994`)
- Bug in :meth:`DataFrame.to_hdf` and :func:`read_hdf` with ``datetime64`` dtypes with non-nanosecond resolution failing to round-trip correctly (:issue:`55622`)
- Bug in :meth:`DataFrame.to_json` where it would produce duplicate column names for orient=split (:issue:`50456`)
- Bug in :meth:`~pandas.read_excel` with ``engine="odf"`` (``ods`` files) when string contains annotation (:issue:`55200`)
- Bug in :meth:`~pandas.read_excel` with an ODS file without cached formatted cell for float values (:issue:`55219`)
- Bug where :meth:`DataFrame.to_json` would raise an ``OverflowError`` instead of a ``TypeError`` with unsupported NumPy types (:issue:`55403`)
Expand Down
6 changes: 6 additions & 0 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ class FrameWriter(Writer):

@property
def obj_to_write(self) -> NDFrame | Mapping[IndexLabel, Any]:
if isinstance(self.obj.columns, MultiIndex):
copy = self.obj.copy(deep=True)
self.obj = copy
self.obj.columns = MultiIndex.from_arrays(list(self.obj.columns))
if not self.index and self.orient == "split":
obj_to_write = self.obj.to_dict(orient="split")
del obj_to_write["index"]
Expand Down Expand Up @@ -1396,6 +1400,8 @@ def _parse(self) -> None:
orig_names,
is_potential_multi_index(orig_names, None),
)
if is_potential_multi_index(orig_names, None):
decoded["columns"] = [list(tup) for tup in decoded["columns"]]
self.obj = DataFrame(dtype=None, **decoded)
elif orient == "index":
self.obj = DataFrame.from_dict(
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,6 @@ def test_roundtrip_mixed(self, orient, convert_axes):

assert_json_roundtrip_equal(result, expected, orient)

@pytest.mark.xfail(
reason="#50456 Column multiindex is stored and loaded differently",
raises=AssertionError,
)
@pytest.mark.parametrize(
"columns",
[
Expand Down