Skip to content

API: raise notImplementedError on to_json(orient='table') for a DataFrame with a MultiIndex #16095

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

Merged
merged 1 commit into from
Apr 22, 2017
Merged
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
7 changes: 6 additions & 1 deletion pandas/io/json/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pandas._libs.tslib import iNaT
from pandas.compat import StringIO, long, u
from pandas import compat, isnull
from pandas import Series, DataFrame, to_datetime
from pandas import Series, DataFrame, to_datetime, MultiIndex
from pandas.io.common import get_filepath_or_buffer, _get_handle
from pandas.core.common import AbstractMethodError
from pandas.io.formats.printing import pprint_thing
Expand Down Expand Up @@ -138,6 +138,11 @@ def __init__(self, obj, orient, date_format, double_precision,

self.schema = build_table_schema(obj)

# NotImplementd on a column MultiIndex
if obj.ndim == 2 and isinstance(obj.columns, MultiIndex):
raise NotImplementedError(
"orient='table' is not supported for MultiIndex")

# TODO: Do this timedelta properly in objToJSON.c See GH #15137
if ((obj.ndim == 1) and (obj.name in set(obj.index.names)) or
len(obj.columns & obj.index.names)):
Expand Down
17 changes: 16 additions & 1 deletion pandas/tests/io/formats/test_printing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# -*- coding: utf-8 -*-
import pytest
from pandas import compat

import numpy as np
import pandas as pd

from pandas import compat
import pandas.io.formats.printing as printing
import pandas.io.formats.format as fmt
import pandas.util.testing as tm
Expand Down Expand Up @@ -166,6 +169,18 @@ def test_publishes(self):
'application/vnd.dataresource+json'}
self.assertEqual(set(arg.keys()), expected)

def test_publishes_not_implemented(self):
# column MultiIndex
# GH 15996
midx = pd.MultiIndex.from_product([['A', 'B'], ['a', 'b', 'c']])
df = pd.DataFrame(np.random.randn(5, len(midx)), columns=midx)

make_patch = self.mock.patch('IPython.display.display')
opt = pd.option_context('display.html.table_schema', True)
with opt, make_patch as mock_display: # noqa
with pytest.raises(NotImplementedError):
df._ipython_display_()

def test_config_on(self):
df = pd.DataFrame({"A": [1, 2]})
with pd.option_context("display.html.table_schema", True):
Expand Down