diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 74d3053821e39..f078cbb435d25 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -43,6 +43,7 @@ import pandas.core.algorithms as algos import pandas.core.common as com import pandas.core.missing as missing +from pandas.errors import UnserializableWarning from pandas.io.formats.printing import pprint_thing from pandas.io.formats.format import format_percentiles from pandas.tseries.frequencies import to_offset @@ -138,7 +139,12 @@ def _ipython_display_(self): # Series doesn't define _repr_html_ or _repr_latex_ latex = self._repr_latex_() if hasattr(self, '_repr_latex_') else None html = self._repr_html_() if hasattr(self, '_repr_html_') else None - table_schema = self._repr_table_schema_() + try: + table_schema = self._repr_table_schema_() + except Exception as e: + warnings.warn("Cannot create table schema representation. " + "{}".format(e), UnserializableWarning) + table_schema = None # We need the inital newline since we aren't going through the # usual __repr__. See # https://github.com/pandas-dev/pandas/pull/14904#issuecomment-277829277 diff --git a/pandas/errors/__init__.py b/pandas/errors/__init__.py index f6719e7be421b..8540d8776fbaa 100644 --- a/pandas/errors/__init__.py +++ b/pandas/errors/__init__.py @@ -55,3 +55,11 @@ class ParserWarning(Warning): one specified by the user due to lack of support or functionality for parsing particular attributes of a CSV file with the requsted engine """ + + +class UnserializableWarning(Warning): + """ + Warnng that is raised when a DataFrame cannot be serialzed. + + .. versionadded:: 0.20.0 + """ diff --git a/pandas/tests/io/formats/test_printing.py b/pandas/tests/io/formats/test_printing.py index f9d911f523699..63cd08545610f 100644 --- a/pandas/tests/io/formats/test_printing.py +++ b/pandas/tests/io/formats/test_printing.py @@ -5,6 +5,7 @@ import pandas as pd from pandas import compat +from pandas.errors import UnserializableWarning import pandas.io.formats.printing as printing import pandas.io.formats.format as fmt import pandas.util.testing as tm @@ -177,9 +178,16 @@ def test_publishes_not_implemented(self): 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): + with opt, make_patch as mock_display: + with pytest.warns(UnserializableWarning) as record: df._ipython_display_() + args, _ = mock_display.call_args + arg, = args # just one argument + + expected = {'text/plain', 'text/html'} + assert set(arg.keys()) == expected + assert "orient='table' is not supported for MultiIndex" in ( + record[-1].message.args[0]) def test_config_on(self): df = pd.DataFrame({"A": [1, 2]})