Skip to content

Commit d50b162

Browse files
BUG/API: Catch exceptions in _ipython_display_ (#16132)
We raise an UnserializeableWarning when we fail to generate the table schema and do not publish a `table_schema` repr.
1 parent 186957e commit d50b162

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

pandas/core/generic.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import pandas.core.algorithms as algos
4444
import pandas.core.common as com
4545
import pandas.core.missing as missing
46+
from pandas.errors import UnserializableWarning
4647
from pandas.io.formats.printing import pprint_thing
4748
from pandas.io.formats.format import format_percentiles
4849
from pandas.tseries.frequencies import to_offset
@@ -138,7 +139,12 @@ def _ipython_display_(self):
138139
# Series doesn't define _repr_html_ or _repr_latex_
139140
latex = self._repr_latex_() if hasattr(self, '_repr_latex_') else None
140141
html = self._repr_html_() if hasattr(self, '_repr_html_') else None
141-
table_schema = self._repr_table_schema_()
142+
try:
143+
table_schema = self._repr_table_schema_()
144+
except Exception as e:
145+
warnings.warn("Cannot create table schema representation. "
146+
"{}".format(e), UnserializableWarning)
147+
table_schema = None
142148
# We need the inital newline since we aren't going through the
143149
# usual __repr__. See
144150
# https://github.com/pandas-dev/pandas/pull/14904#issuecomment-277829277

pandas/errors/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,11 @@ class ParserWarning(Warning):
5555
one specified by the user due to lack of support or functionality for
5656
parsing particular attributes of a CSV file with the requsted engine
5757
"""
58+
59+
60+
class UnserializableWarning(Warning):
61+
"""
62+
Warnng that is raised when a DataFrame cannot be serialzed.
63+
64+
.. versionadded:: 0.20.0
65+
"""

pandas/tests/io/formats/test_printing.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pandas as pd
66

77
from pandas import compat
8+
from pandas.errors import UnserializableWarning
89
import pandas.io.formats.printing as printing
910
import pandas.io.formats.format as fmt
1011
import pandas.util.testing as tm
@@ -177,9 +178,16 @@ def test_publishes_not_implemented(self):
177178

178179
make_patch = self.mock.patch('IPython.display.display')
179180
opt = pd.option_context('display.html.table_schema', True)
180-
with opt, make_patch as mock_display: # noqa
181-
with pytest.raises(NotImplementedError):
181+
with opt, make_patch as mock_display:
182+
with pytest.warns(UnserializableWarning) as record:
182183
df._ipython_display_()
184+
args, _ = mock_display.call_args
185+
arg, = args # just one argument
186+
187+
expected = {'text/plain', 'text/html'}
188+
assert set(arg.keys()) == expected
189+
assert "orient='table' is not supported for MultiIndex" in (
190+
record[-1].message.args[0])
183191

184192
def test_config_on(self):
185193
df = pd.DataFrame({"A": [1, 2]})

0 commit comments

Comments
 (0)