Skip to content

Commit

Permalink
BUG/API: Catch exceptions in _ipython_display_ (#16132)
Browse files Browse the repository at this point in the history
We raise an UnserializeableWarning when we fail to generate
the table schema and do not publish a `table_schema` repr.
  • Loading branch information
TomAugspurger authored Apr 25, 2017
1 parent 186957e commit d50b162
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
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.

This comment has been minimized.

Copy link
@crayxt

crayxt Apr 26, 2017

Contributor

Typos, Warning, serialized

.. 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

0 comments on commit d50b162

Please sign in to comment.