Skip to content

Commit b036a7e

Browse files
author
y-p
committed
BUG: rework display logic again. deprecate display.height
1 parent e554cfb commit b036a7e

File tree

4 files changed

+58
-54
lines changed

4 files changed

+58
-54
lines changed

pandas/core/config_init.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,17 @@
120120

121121
pc_line_width_doc = """
122122
: int
123-
When printing wide DataFrames, this is the width of each line.
123+
Deprecated.
124124
"""
125125

126126
pc_line_width_deprecation_warning = """\
127127
line_width has been deprecated, use display.width instead (currently both are identical)
128128
"""
129129

130+
pc_height_deprecation_warning = """\
131+
height has been deprecated.
132+
"""
133+
130134
pc_width_doc = """
131135
: int
132136
Width of the display in characters. In case python/IPython is running in
@@ -138,10 +142,7 @@
138142

139143
pc_height_doc = """
140144
: int
141-
Height of the display in lines. In case python/IPython is running in a
142-
terminal this can be set to None and pandas will auto-detect the width.
143-
Note that the IPython notebook, IPython qtconsole, or IDLE do not run
144-
in a terminal, and hence it is not possible to correctly detect the height.
145+
Deprecated.
145146
"""
146147

147148
pc_chop_threshold_doc = """
@@ -244,10 +245,15 @@ def mpl_style_cb(key):
244245
validator=is_instance_factory([type(None), int]))
245246
# redirected to width, make defval identical
246247
cf.register_option('line_width', get_default_val('display.width'), pc_line_width_doc)
248+
247249
cf.deprecate_option('display.line_width',
248250
msg=pc_line_width_deprecation_warning,
249251
rkey='display.width')
250252

253+
cf.deprecate_option('display.height',
254+
msg=pc_height_deprecation_warning,
255+
rkey='display.height')
256+
251257
tc_sim_interactive_doc = """
252258
: boolean
253259
Whether to simulate interactive mode for purposes of testing

pandas/core/format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ def detect_console_encoding():
17021702
def get_console_size():
17031703
"""Return console size as tuple = (width, height).
17041704
1705-
May return (None,None) in some cases.
1705+
Returns (None,None) in non-interactive session.
17061706
"""
17071707
display_width = get_option('display.width')
17081708
display_height = get_option('display.height')

pandas/core/frame.py

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -605,21 +605,10 @@ def __nonzero__(self):
605605

606606
def _repr_fits_vertical_(self):
607607
"""
608-
Check if full repr fits in vertical boundaries imposed by the display
609-
options height and max_rows. In case of non-interactive session,
610-
no boundaries apply.
608+
Check length against max_rows.
611609
"""
612-
width, height = fmt.get_console_size()
613610
max_rows = get_option("display.max_rows")
614-
615-
if height is None and max_rows is None:
616-
return True
617-
618-
else:
619-
# min of two, where one may be None
620-
height = height or max_rows +1
621-
max_rows = max_rows or height +1
622-
return len(self) <= min(max_rows, height)
611+
return len(self) <= max_rows
623612

624613
def _repr_fits_horizontal_(self,ignore_width=False):
625614
"""
@@ -632,8 +621,6 @@ def _repr_fits_horizontal_(self,ignore_width=False):
632621
GH3541, GH3573
633622
"""
634623

635-
# everytime you add an if-clause here, god slaughters a kitten.
636-
# please. think of the kittens.
637624
width, height = fmt.get_console_size()
638625
max_columns = get_option("display.max_columns")
639626
nb_columns = len(self.columns)
@@ -643,31 +630,37 @@ def _repr_fits_horizontal_(self,ignore_width=False):
643630
((not ignore_width) and width and nb_columns > (width // 2))):
644631
return False
645632

646-
if width is None:
647-
# no sense finding width of repr if no width set
633+
if (ignore_width # used by repr_html under IPython notebook
634+
or not com.in_interactive_session()): # scripts ignore terminal dims
648635
return True
649636

637+
if (get_option('display.width') is not None or
638+
com.in_ipython_frontend()):
639+
# check at least the column row for excessive width
640+
max_rows = 1
641+
else:
642+
max_rows = get_option("display.max_rows")
643+
644+
# when auto-detecting, so width=None and not in ipython front end
645+
# check whether repr fits horizontal by actualy checking
646+
# the width of the rendered repr
650647
buf = StringIO()
651648

652649
# only care about the stuff we'll actually print out
653650
# and to_string on entire frame may be expensive
654651
d = self
655-
max_rows = get_option("display.max_rows")
656-
if not (height is None and max_rows is None):
652+
653+
if not (max_rows is None): # unlimited rows
657654
# min of two, where one may be None
658-
height = height or max_rows +1
659-
max_rows = max_rows or height +1
660-
d=d.iloc[:min(max_rows, height,len(d))]
655+
d=d.iloc[:min(max_rows,len(d))]
656+
else:
657+
return True
661658

662659
d.to_string(buf=buf)
663660
value = buf.getvalue()
664661
repr_width = max([len(l) for l in value.split('\n')])
665662

666-
# special case ipnb+HTML repr
667-
if not ignore_width:
668-
return repr_width <= width
669-
else:
670-
return True
663+
return repr_width < width
671664

672665
def __str__(self):
673666
"""
@@ -709,14 +702,11 @@ def __unicode__(self):
709702
if fits_vertical and fits_horizontal:
710703
self.to_string(buf=buf)
711704
else:
712-
width, height = fmt.get_console_size()
713-
max_rows = get_option("display.max_rows") or height
714-
# expand_repr basically takes the extrac columns that don't
715-
# fit the width, and creates a new page, which increases
716-
# the effective row count. check number of cols agaibst
717-
# max rows to catch wrapping. that would exceed max_rows.
718-
if (get_option("display.expand_frame_repr") and fits_vertical and
719-
len(self.columns) < max_rows):
705+
width, _ = fmt.get_console_size()
706+
max_rows = get_option("display.max_rows")
707+
if (get_option("display.expand_frame_repr")
708+
and fits_vertical):
709+
# and len(self.columns) < max_rows)
720710
self.to_string(buf=buf, line_width=width)
721711
else:
722712
max_info_rows = get_option('display.max_info_rows')
@@ -892,7 +882,7 @@ def __contains__(self, key):
892882

893883
# Python 2 division methods
894884
if not py3compat.PY3:
895-
__div__ = _arith_method(operator.div, '__div__', '/',
885+
__div__ = _arith_method(operator.div, '__div__', '/',
896886
default_axis=None, fill_zeros=np.inf)
897887
__rdiv__ = _arith_method(lambda x, y: y / x, '__rdiv__',
898888
default_axis=None, fill_zeros=np.inf)

pandas/tests/test_format.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ def test_expand_frame_repr(self):
170170
df_tall = DataFrame('hello', range(30), range(5))
171171

172172
with option_context('mode.sim_interactive', True):
173-
with option_context('display.width', 50,
174-
'display.height', 20):
173+
with option_context('display.max_columns', 5,
174+
'display.width',20,
175+
'display.max_rows', 20):
175176
with option_context('display.expand_frame_repr', True):
176177
self.assertFalse(has_info_repr(df_small))
177178
self.assertFalse(has_expanded_repr(df_small))
@@ -226,19 +227,21 @@ def mkframe(n):
226227
# since not exceeding width
227228
self.assertFalse(has_expanded_repr(df6))
228229
self.assertFalse(has_info_repr(df6))
229-
230+
230231
with option_context('display.max_rows', 9,
231232
'display.max_columns', 10):
232233
# out vertical bounds can not result in exanded repr
233234
self.assertFalse(has_expanded_repr(df10))
234235
self.assertTrue(has_info_repr(df10))
235236

236-
with option_context('display.max_columns', 0,
237+
# width=None in terminal, auto detection
238+
with option_context('display.max_columns', 100,
237239
'display.max_rows', term_width * 20,
238-
'display.width', 0):
240+
'display.width', None):
239241
df = mkframe((term_width // 7) - 2)
240242
self.assertFalse(has_expanded_repr(df))
241243
df = mkframe((term_width // 7) + 2)
244+
print( df._repr_fits_horizontal_())
242245
self.assertTrue(has_expanded_repr(df))
243246

244247
def test_to_string_repr_unicode(self):
@@ -787,7 +790,8 @@ def test_pprint_thing(self):
787790
def test_wide_repr(self):
788791
with option_context('mode.sim_interactive', True):
789792
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
790-
df = DataFrame([col(20, 25) for _ in range(10)])
793+
max_cols = get_option('display.max_columns')
794+
df = DataFrame([col(max_cols+1, 25) for _ in range(10)])
791795
set_option('display.expand_frame_repr', False)
792796
rep_str = repr(df)
793797
set_option('display.expand_frame_repr', True)
@@ -810,7 +814,8 @@ def test_wide_repr_wide_columns(self):
810814
def test_wide_repr_named(self):
811815
with option_context('mode.sim_interactive', True):
812816
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
813-
df = DataFrame([col(20, 25) for _ in range(10)])
817+
max_cols = get_option('display.max_columns')
818+
df = DataFrame([col(max_cols+1, 25) for _ in range(10)])
814819
df.index.name = 'DataFrame Index'
815820
set_option('display.expand_frame_repr', False)
816821

@@ -833,7 +838,8 @@ def test_wide_repr_multiindex(self):
833838
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
834839
midx = pandas.MultiIndex.from_arrays([np.array(col(10, 5)),
835840
np.array(col(10, 5))])
836-
df = DataFrame([col(20, 25) for _ in range(10)],
841+
max_cols = get_option('display.max_columns')
842+
df = DataFrame([col(max_cols+1, 25) for _ in range(10)],
837843
index=midx)
838844
df.index.names = ['Level 0', 'Level 1']
839845
set_option('display.expand_frame_repr', False)
@@ -853,12 +859,13 @@ def test_wide_repr_multiindex(self):
853859

854860
def test_wide_repr_multiindex_cols(self):
855861
with option_context('mode.sim_interactive', True):
862+
max_cols = get_option('display.max_columns')
856863
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
857864
midx = pandas.MultiIndex.from_arrays([np.array(col(10, 5)),
858865
np.array(col(10, 5))])
859-
mcols = pandas.MultiIndex.from_arrays([np.array(col(20, 3)),
860-
np.array(col(20, 3))])
861-
df = DataFrame([col(20, 25) for _ in range(10)],
866+
mcols = pandas.MultiIndex.from_arrays([np.array(col(max_cols+1, 3)),
867+
np.array(col(max_cols+1, 3))])
868+
df = DataFrame([col(max_cols+1, 25) for _ in range(10)],
862869
index=midx, columns=mcols)
863870
df.index.names = ['Level 0', 'Level 1']
864871
set_option('display.expand_frame_repr', False)
@@ -876,7 +883,8 @@ def test_wide_repr_multiindex_cols(self):
876883
def test_wide_repr_unicode(self):
877884
with option_context('mode.sim_interactive', True):
878885
col = lambda l, k: [tm.randu(k) for _ in xrange(l)]
879-
df = DataFrame([col(20, 25) for _ in range(10)])
886+
max_cols = get_option('display.max_columns')
887+
df = DataFrame([col(max_cols+1, 25) for _ in range(10)])
880888
set_option('display.expand_frame_repr', False)
881889
rep_str = repr(df)
882890
set_option('display.expand_frame_repr', True)

0 commit comments

Comments
 (0)