Skip to content
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

fixed issue, multiindex dataframe now created as expected #56415

Closed
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ def to_json(
indent=indent,
).write()

if orient == "split" and isinstance(obj, DataFrame) and isinstance(obj.columns, MultiIndex):
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 probably be handled in the writer, not after the json string is written

Copy link
Author

Choose a reason for hiding this comment

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

@mroeschke good point, I have just made a new commit with this suggested change; I moved the code to the FrameWriter class

# inverse of multindex.fromArray
fixed_columns = [[obj.columns[i][j] for i in range(len(obj.columns))] for j in range(len(obj.columns[0]))]

new_str = ujson_loads(s)

# changes columns to starting columns
new_str["columns"] = fixed_columns
s = ujson_dumps(new_str)

if lines:
s = convert_to_line_delimits(s)

Expand Down Expand Up @@ -1396,6 +1406,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
Loading