5
5
import pandas as pd
6
6
7
7
from pandas import compat
8
- from pandas .errors import UnserializableWarning
9
8
import pandas .io .formats .printing as printing
10
9
import pandas .io .formats .format as fmt
11
10
import pandas .util .testing as tm
@@ -137,57 +136,46 @@ def setUpClass(cls):
137
136
except ImportError :
138
137
pytest .skip ("Mock is not installed" )
139
138
cls .mock = mock
139
+ from IPython .core .interactiveshell import InteractiveShell
140
+ cls .display_formatter = InteractiveShell .instance ().display_formatter
140
141
141
142
def test_publishes (self ):
143
+
142
144
df = pd .DataFrame ({"A" : [1 , 2 ]})
143
145
objects = [df ['A' ], df , df ] # dataframe / series
144
146
expected_keys = [
145
147
{'text/plain' , 'application/vnd.dataresource+json' },
146
148
{'text/plain' , 'text/html' , 'application/vnd.dataresource+json' },
147
149
]
148
150
149
- make_patch = self .mock .patch ('IPython.display.display' )
150
151
opt = pd .option_context ('display.html.table_schema' , True )
151
152
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
161
156
162
157
with_latex = pd .option_context ('display.latex.repr' , True )
163
158
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 )
168
161
169
162
expected = {'text/plain' , 'text/html' , 'text/latex' ,
170
163
'application/vnd.dataresource+json' }
171
- assert set (arg .keys ()) == expected
164
+ assert set (formatted [ 0 ] .keys ()) == expected
172
165
173
166
def test_publishes_not_implemented (self ):
174
167
# column MultiIndex
175
168
# GH 15996
176
169
midx = pd .MultiIndex .from_product ([['A' , 'B' ], ['a' , 'b' , 'c' ]])
177
170
df = pd .DataFrame (np .random .randn (5 , len (midx )), columns = midx )
178
171
179
- make_patch = self .mock .patch ('IPython.display.display' )
180
172
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 )
186
176
187
177
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
191
179
192
180
def test_config_on (self ):
193
181
df = pd .DataFrame ({"A" : [1 , 2 ]})
@@ -209,26 +197,23 @@ def test_config_monkeypatches(self):
209
197
assert not hasattr (df , '_ipython_display_' )
210
198
assert not hasattr (df ['A' ], '_ipython_display_' )
211
199
200
+ formatters = self .display_formatter .formatters
201
+ mimetype = 'application/vnd.dataresource+json'
202
+
212
203
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
218
206
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
224
210
225
211
# able to re-set
226
212
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
228
215
# 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 )
232
217
233
218
234
219
# TODO: fix this broken test
0 commit comments