Skip to content

BUG/API: Catch exceptions in _ipython_display_ #16132

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
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
8 changes: 7 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pandas/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
12 changes: 10 additions & 2 deletions pandas/tests/io/formats/test_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]})
Expand Down