55import pandas as pd
66
77from pandas import compat
8- from pandas .errors import UnserializableWarning
98import pandas .io .formats .printing as printing
109import pandas .io .formats .format as fmt
1110import pandas .util .testing as tm
@@ -137,57 +136,46 @@ def setUpClass(cls):
137136 except ImportError :
138137 pytest .skip ("Mock is not installed" )
139138 cls .mock = mock
139+ from IPython .core .interactiveshell import InteractiveShell
140+ cls .display_formatter = InteractiveShell .instance ().display_formatter
140141
141142 def test_publishes (self ):
143+
142144 df = pd .DataFrame ({"A" : [1 , 2 ]})
143145 objects = [df ['A' ], df , df ] # dataframe / series
144146 expected_keys = [
145147 {'text/plain' , 'application/vnd.dataresource+json' },
146148 {'text/plain' , 'text/html' , 'application/vnd.dataresource+json' },
147149 ]
148150
149- make_patch = self .mock .patch ('IPython.display.display' )
150151 opt = pd .option_context ('display.html.table_schema' , True )
151152 for obj , expected in zip (objects , expected_keys ):
152- with opt , make_patch as mock_display :
153- handle = obj ._ipython_display_ ()
154- assert mock_display .call_count == 1
155- assert handle is None
156- args , kwargs = mock_display .call_args
157- arg , = args # just one argument
158-
159- assert kwargs == {"raw" : True }
160- assert set (arg .keys ()) == expected
153+ with opt :
154+ formatted = self .display_formatter .format (obj )
155+ assert set (formatted [0 ].keys ()) == expected
161156
162157 with_latex = pd .option_context ('display.latex.repr' , True )
163158
164- with opt , with_latex , make_patch as mock_display :
165- handle = obj ._ipython_display_ ()
166- args , kwargs = mock_display .call_args
167- arg , = args
159+ with opt , with_latex :
160+ formatted = self .display_formatter .format (obj )
168161
169162 expected = {'text/plain' , 'text/html' , 'text/latex' ,
170163 'application/vnd.dataresource+json' }
171- assert set (arg .keys ()) == expected
164+ assert set (formatted [ 0 ] .keys ()) == expected
172165
173166 def test_publishes_not_implemented (self ):
174167 # column MultiIndex
175168 # GH 15996
176169 midx = pd .MultiIndex .from_product ([['A' , 'B' ], ['a' , 'b' , 'c' ]])
177170 df = pd .DataFrame (np .random .randn (5 , len (midx )), columns = midx )
178171
179- make_patch = self .mock .patch ('IPython.display.display' )
180172 opt = pd .option_context ('display.html.table_schema' , True )
181- with opt , make_patch as mock_display :
182- with pytest .warns (UnserializableWarning ) as record :
183- df ._ipython_display_ ()
184- args , _ = mock_display .call_args
185- arg , = args # just one argument
173+
174+ with opt :
175+ formatted = self .display_formatter .format (df )
186176
187177 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 ])
178+ assert set (formatted [0 ].keys ()) == expected
191179
192180 def test_config_on (self ):
193181 df = pd .DataFrame ({"A" : [1 , 2 ]})
@@ -209,26 +197,23 @@ def test_config_monkeypatches(self):
209197 assert not hasattr (df , '_ipython_display_' )
210198 assert not hasattr (df ['A' ], '_ipython_display_' )
211199
200+ formatters = self .display_formatter .formatters
201+ mimetype = 'application/vnd.dataresource+json'
202+
212203 with pd .option_context ('display.html.table_schema' , True ):
213- assert hasattr (df , '_ipython_display_' )
214- # smoke test that it works
215- df ._ipython_display_ ()
216- assert hasattr (df ['A' ], '_ipython_display_' )
217- df ['A' ]._ipython_display_ ()
204+ assert 'application/vnd.dataresource+json' in formatters
205+ assert formatters [mimetype ].enabled
218206
219- assert not hasattr (df , '_ipython_display_' )
220- assert not hasattr (df ['A' ], '_ipython_display_' )
221- # re-unsetting is OK
222- assert not hasattr (df , '_ipython_display_' )
223- assert not hasattr (df ['A' ], '_ipython_display_' )
207+ # still there, just disabled
208+ assert 'application/vnd.dataresource+json' in formatters
209+ assert not formatters [mimetype ].enabled
224210
225211 # able to re-set
226212 with pd .option_context ('display.html.table_schema' , True ):
227- assert hasattr (df , '_ipython_display_' )
213+ assert 'application/vnd.dataresource+json' in formatters
214+ assert formatters [mimetype ].enabled
228215 # smoke test that it works
229- df ._ipython_display_ ()
230- assert hasattr (df ['A' ], '_ipython_display_' )
231- df ['A' ]._ipython_display_ ()
216+ self .display_formatter .format (cf )
232217
233218
234219# TODO: fix this broken test
0 commit comments