Skip to content

Commit

Permalink
API Change repr name for table schema (#16204)
Browse files Browse the repository at this point in the history
* API Change repr name for table schema

Not API breaking, since pandas 0.20.0 hasn't been released yet.

* REF: Move Formatter to printing

* pep8
  • Loading branch information
TomAugspurger authored May 3, 2017
1 parent 9051f0d commit 67e7efc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 39 deletions.
33 changes: 2 additions & 31 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
module is imported, register them here rather then in the module.
"""
import sys
import warnings

import pandas.core.config as cf
Expand Down Expand Up @@ -342,36 +341,8 @@ def mpl_style_cb(key):


def table_schema_cb(key):
# first, check if we are in IPython
if 'IPython' not in sys.modules:
# definitely not in IPython
return
from IPython import get_ipython
ip = get_ipython()
if ip is None:
# still not in IPython
return

formatters = ip.display_formatter.formatters

mimetype = "application/vnd.dataresource+json"

if cf.get_option(key):
if mimetype not in formatters:
# define tableschema formatter
from IPython.core.formatters import BaseFormatter

class TableSchemaFormatter(BaseFormatter):
print_method = '_repr_table_schema_'
_return_type = (dict,)
# register it:
formatters[mimetype] = TableSchemaFormatter()
# enable it if it's been disabled:
formatters[mimetype].enabled = True
else:
# unregister tableschema mime-type
if mimetype in formatters:
formatters[mimetype].enabled = False
from pandas.io.formats.printing import _enable_data_resource_formatter
_enable_data_resource_formatter(cf.get_option(key))


with cf.config_prefix('display'):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def __init__(self, data, axes=None, copy=False, dtype=None,
object.__setattr__(self, '_data', data)
object.__setattr__(self, '_item_cache', {})

def _repr_table_schema_(self):
def _repr_data_resource_(self):
"""
Not a real Jupyter special repr method, but we use the same
naming convention.
Expand Down
32 changes: 32 additions & 0 deletions pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
printing tools
"""

import sys
from pandas.core.dtypes.inference import is_sequence
from pandas import compat
from pandas.compat import u
Expand Down Expand Up @@ -233,3 +234,34 @@ def as_escaped_unicode(thing, escape_chars=escape_chars):
def pprint_thing_encoded(object, encoding='utf-8', errors='replace', **kwds):
value = pprint_thing(object) # get unicode representation of object
return value.encode(encoding, errors, **kwds)


def _enable_data_resource_formatter(enable):
if 'IPython' not in sys.modules:
# definitely not in IPython
return
from IPython import get_ipython
ip = get_ipython()
if ip is None:
# still not in IPython
return

formatters = ip.display_formatter.formatters
mimetype = "application/vnd.dataresource+json"

if enable:
if mimetype not in formatters:
# define tableschema formatter
from IPython.core.formatters import BaseFormatter

class TableSchemaFormatter(BaseFormatter):
print_method = '_repr_data_resource_'
_return_type = (dict,)
# register it:
formatters[mimetype] = TableSchemaFormatter()
# enable it if it's been disabled:
formatters[mimetype].enabled = True
else:
# unregister tableschema mime-type
if mimetype in formatters:
formatters[mimetype].enabled = False
10 changes: 3 additions & 7 deletions pandas/tests/io/formats/test_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,23 +180,19 @@ def test_publishes_not_implemented(self):
def test_config_on(self):
df = pd.DataFrame({"A": [1, 2]})
with pd.option_context("display.html.table_schema", True):
result = df._repr_table_schema_()
result = df._repr_data_resource_()

assert result is not None

def test_config_default_off(self):
df = pd.DataFrame({"A": [1, 2]})
with pd.option_context("display.html.table_schema", False):
result = df._repr_table_schema_()
result = df._repr_data_resource_()

assert result is None

def test_config_monkeypatches(self):
def test_enable_data_resource_formatter(self):
# GH 10491
df = pd.DataFrame({"A": [1, 2]})
assert not hasattr(df, '_ipython_display_')
assert not hasattr(df['A'], '_ipython_display_')

formatters = self.display_formatter.formatters
mimetype = 'application/vnd.dataresource+json'

Expand Down

0 comments on commit 67e7efc

Please sign in to comment.