@@ -32,6 +32,17 @@ def curpath():
3232 pth , _ = os .path .split (os .path .abspath (__file__ ))
3333 return pth
3434
35+ def has_info_repr (df ):
36+ r = repr (df )
37+ return r .split ('\n ' )[0 ].startswith ("<class" )
38+
39+ def has_expanded_repr (df ):
40+ r = repr (df )
41+ for line in r .split ('\n ' ):
42+ if line .endswith ('\\ ' ):
43+ return True
44+ return False
45+
3546
3647class TestDataFrameFormatting (unittest .TestCase ):
3748 _multiprocess_can_split_ = True
@@ -146,98 +157,55 @@ def test_repr_no_backslash(self):
146157 self .assertTrue ('\\ ' not in repr (df ))
147158
148159 def test_expand_frame_repr (self ):
149- import pandas .core .common as com
150- original_in_interactive_session = com .in_interactive_session
151- com .in_interactive_session = lambda : True
152- line_width = 50
153-
154160 df_small = DataFrame ('hello' , [0 ], [0 ])
155- df_wide = DataFrame ('hello' , [0 ], range (8 ))
161+ df_wide = DataFrame ('hello' , [0 ], range (10 ))
156162
157- def has_info_repr (df ):
158- r = repr (df )
159- return r .split ('\n ' )[0 ].startswith ("<class" )
163+ with option_context ('mode.sim_interactive' , True ):
164+ with option_context ('display.width' , 50 ):
165+ with option_context ('display.expand_frame_repr' , True ):
166+ self .assertFalse (has_info_repr (df_small ))
167+ self .assertFalse (has_expanded_repr (df_small ))
168+ self .assertFalse (has_info_repr (df_wide ))
169+ self .assertTrue (has_expanded_repr (df_wide ))
160170
161- def has_wide_repr (df ):
162- r = repr (df )
163- for line in r .split ('\n ' ):
164- if line .endswith ('\\ ' ):
165- return True
166- return False
167-
168- with option_context ('display.line_width' , line_width ):
169- with option_context ('display.expand_frame_repr' , True ):
170- self .assertFalse (has_info_repr (df_small ))
171- self .assertFalse (has_wide_repr (df_small ))
172- self .assertFalse (has_info_repr (df_wide ))
173- self .assertTrue (has_wide_repr (df_wide ))
174- with option_context ('display.max_columns' , 7 ):
171+ with option_context ('display.expand_frame_repr' , False ):
175172 self .assertFalse (has_info_repr (df_small ))
176- self .assertFalse (has_wide_repr (df_small ))
173+ self .assertFalse (has_expanded_repr (df_small ))
177174 self .assertTrue (has_info_repr (df_wide ))
178- self .assertFalse (has_wide_repr (df_wide ))
179-
180- with option_context ('display.expand_frame_repr' , False ):
181- self .assertFalse (has_info_repr (df_small ))
182- self .assertFalse (has_wide_repr (df_small ))
183- self .assertTrue (has_info_repr (df_wide ))
184- self .assertFalse (has_wide_repr (df_wide ))
185-
186- with option_context ('display.line_width' , None ):
187- with option_context ('display.expand_frame_repr' , True ):
188- self .assertFalse (has_info_repr (df_small ))
189- self .assertFalse (has_wide_repr (df_small ))
190- self .assertFalse (has_info_repr (df_wide ))
191- self .assertFalse (has_wide_repr (df_wide ))
192-
193- com .in_interactive_session = original_in_interactive_session
175+ self .assertFalse (has_expanded_repr (df_wide ))
194176
195177 def test_repr_max_columns_max_rows (self ):
196- import pandas .core .common as com
197- original_in_interactive_session = com .in_interactive_session
198- com .in_interactive_session = lambda : True
199-
200178 term_width , term_height = get_terminal_size ()
201179 if term_width < 10 or term_height < 10 :
202180 raise nose .SkipTest
203181
204- def repr_is_info_view (n ):
182+ def mkframe (n ):
205183 index = ['%05d' % i for i in range (n )]
206- df = DataFrame (0 , index , index )
207- r = repr (df )
208- nlines = len (r .split ('\n ' ))
209- return nlines > n + 2
210-
211- with option_context ('display.line_width' , term_width * 2 ):
212- with option_context ('display.max_rows' , 5 ,
213- 'display.max_columns' , 5 ):
214- self .assertFalse (repr_is_info_view (4 ))
215- self .assertFalse (repr_is_info_view (5 ))
216- self .assertTrue (repr_is_info_view (6 ))
217-
218- with option_context ('display.max_rows' , 10 ,
219- 'display.max_columns' , 5 ):
220- self .assertFalse (repr_is_info_view (5 ))
221- self .assertTrue (repr_is_info_view (6 ))
222-
223- with option_context ('display.max_rows' , 5 ,
224- 'display.max_columns' , 10 ):
225- self .assertFalse (repr_is_info_view (5 ))
226- self .assertTrue (repr_is_info_view (6 ))
227-
228- with option_context ('display.max_rows' , 0 ,
229- 'display.max_columns' , term_height ):
230- self .assertFalse (repr_is_info_view (term_height - 2 ))
231- self .assertTrue (repr_is_info_view (term_height + 1 ))
232-
233- with option_context ('display.max_rows' , term_height * 2 ,
234- 'display.max_columns' , 0 ):
235- self .assertTrue (com .in_interactive_session ())
236- n = (term_width + 2 ) // 7
237- self .assertFalse (repr_is_info_view (n - 1 ))
238- self .assertTrue (repr_is_info_view (n + 1 ))
239-
240- com .in_interactive_session = original_in_interactive_session
184+ return DataFrame (0 , index , index )
185+
186+ with option_context ('mode.sim_interactive' , True ):
187+ with option_context ('display.width' , term_width * 2 ):
188+ with option_context ('display.max_rows' , 5 ,
189+ 'display.max_columns' , 5 ):
190+ self .assertFalse (has_expanded_repr (mkframe (4 )))
191+ self .assertFalse (has_expanded_repr (mkframe (5 )))
192+ self .assertFalse (has_expanded_repr (mkframe (6 )))
193+ self .assertTrue (has_info_repr (mkframe (6 )))
194+
195+ with option_context ('display.max_rows' , 20 ,
196+ 'display.max_columns' , 5 ):
197+ # Out off max_columns boundary, but no extending
198+ # occurs ... can improve?
199+ self .assertFalse (has_expanded_repr (mkframe (6 )))
200+ self .assertFalse (has_info_repr (mkframe (6 )))
201+
202+ with option_context ('display.max_columns' , 0 ,
203+ 'display.max_rows' , term_width * 20 ,
204+ 'display.width' , 0 ):
205+ df = mkframe ((term_width // 7 ) - 2 )
206+ self .assertFalse (has_expanded_repr (df ))
207+ df = mkframe ((term_width // 7 ) + 2 )
208+ self .assertTrue (has_expanded_repr (df ))
241209
242210 def test_to_string_repr_unicode (self ):
243211 buf = StringIO ()
@@ -1271,8 +1239,8 @@ def get_ipython():
12711239 self .assert_ (repstr is not None )
12721240
12731241 fmt .set_printoptions (max_rows = 5 , max_columns = 2 )
1274-
1275- self .assert_ (self . frame . _repr_html_ () is None )
1242+ repstr = self . frame . _repr_html_ ()
1243+ self .assert_ ('class' in repstr ) # info fallback
12761244
12771245 fmt .reset_printoptions ()
12781246
0 commit comments