Skip to content

Commit 67e7efc

Browse files
API Change repr name for table schema (#16204)
* 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
1 parent 9051f0d commit 67e7efc

File tree

4 files changed

+38
-39
lines changed

4 files changed

+38
-39
lines changed

pandas/core/config_init.py

+2-31
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
module is imported, register them here rather then in the module.
1010
1111
"""
12-
import sys
1312
import warnings
1413

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

343342

344343
def table_schema_cb(key):
345-
# first, check if we are in IPython
346-
if 'IPython' not in sys.modules:
347-
# definitely not in IPython
348-
return
349-
from IPython import get_ipython
350-
ip = get_ipython()
351-
if ip is None:
352-
# still not in IPython
353-
return
354-
355-
formatters = ip.display_formatter.formatters
356-
357-
mimetype = "application/vnd.dataresource+json"
358-
359-
if cf.get_option(key):
360-
if mimetype not in formatters:
361-
# define tableschema formatter
362-
from IPython.core.formatters import BaseFormatter
363-
364-
class TableSchemaFormatter(BaseFormatter):
365-
print_method = '_repr_table_schema_'
366-
_return_type = (dict,)
367-
# register it:
368-
formatters[mimetype] = TableSchemaFormatter()
369-
# enable it if it's been disabled:
370-
formatters[mimetype].enabled = True
371-
else:
372-
# unregister tableschema mime-type
373-
if mimetype in formatters:
374-
formatters[mimetype].enabled = False
344+
from pandas.io.formats.printing import _enable_data_resource_formatter
345+
_enable_data_resource_formatter(cf.get_option(key))
375346

376347

377348
with cf.config_prefix('display'):

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def __init__(self, data, axes=None, copy=False, dtype=None,
129129
object.__setattr__(self, '_data', data)
130130
object.__setattr__(self, '_item_cache', {})
131131

132-
def _repr_table_schema_(self):
132+
def _repr_data_resource_(self):
133133
"""
134134
Not a real Jupyter special repr method, but we use the same
135135
naming convention.

pandas/io/formats/printing.py

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
printing tools
33
"""
44

5+
import sys
56
from pandas.core.dtypes.inference import is_sequence
67
from pandas import compat
78
from pandas.compat import u
@@ -233,3 +234,34 @@ def as_escaped_unicode(thing, escape_chars=escape_chars):
233234
def pprint_thing_encoded(object, encoding='utf-8', errors='replace', **kwds):
234235
value = pprint_thing(object) # get unicode representation of object
235236
return value.encode(encoding, errors, **kwds)
237+
238+
239+
def _enable_data_resource_formatter(enable):
240+
if 'IPython' not in sys.modules:
241+
# definitely not in IPython
242+
return
243+
from IPython import get_ipython
244+
ip = get_ipython()
245+
if ip is None:
246+
# still not in IPython
247+
return
248+
249+
formatters = ip.display_formatter.formatters
250+
mimetype = "application/vnd.dataresource+json"
251+
252+
if enable:
253+
if mimetype not in formatters:
254+
# define tableschema formatter
255+
from IPython.core.formatters import BaseFormatter
256+
257+
class TableSchemaFormatter(BaseFormatter):
258+
print_method = '_repr_data_resource_'
259+
_return_type = (dict,)
260+
# register it:
261+
formatters[mimetype] = TableSchemaFormatter()
262+
# enable it if it's been disabled:
263+
formatters[mimetype].enabled = True
264+
else:
265+
# unregister tableschema mime-type
266+
if mimetype in formatters:
267+
formatters[mimetype].enabled = False

pandas/tests/io/formats/test_printing.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,19 @@ def test_publishes_not_implemented(self):
180180
def test_config_on(self):
181181
df = pd.DataFrame({"A": [1, 2]})
182182
with pd.option_context("display.html.table_schema", True):
183-
result = df._repr_table_schema_()
183+
result = df._repr_data_resource_()
184184

185185
assert result is not None
186186

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

192192
assert result is None
193193

194-
def test_config_monkeypatches(self):
194+
def test_enable_data_resource_formatter(self):
195195
# GH 10491
196-
df = pd.DataFrame({"A": [1, 2]})
197-
assert not hasattr(df, '_ipython_display_')
198-
assert not hasattr(df['A'], '_ipython_display_')
199-
200196
formatters = self.display_formatter.formatters
201197
mimetype = 'application/vnd.dataresource+json'
202198

0 commit comments

Comments
 (0)