Skip to content

Commit

Permalink
BUG: rework display logic again. deprecate display.height
Browse files Browse the repository at this point in the history
  • Loading branch information
y-p committed May 21, 2013
1 parent e554cfb commit b036a7e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 54 deletions.
16 changes: 11 additions & 5 deletions pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,17 @@

pc_line_width_doc = """
: int
When printing wide DataFrames, this is the width of each line.
Deprecated.
"""

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

pc_height_deprecation_warning = """\
height has been deprecated.
"""

pc_width_doc = """
: int
Width of the display in characters. In case python/IPython is running in
Expand All @@ -138,10 +142,7 @@

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

pc_chop_threshold_doc = """
Expand Down Expand Up @@ -244,10 +245,15 @@ def mpl_style_cb(key):
validator=is_instance_factory([type(None), int]))
# redirected to width, make defval identical
cf.register_option('line_width', get_default_val('display.width'), pc_line_width_doc)

cf.deprecate_option('display.line_width',
msg=pc_line_width_deprecation_warning,
rkey='display.width')

cf.deprecate_option('display.height',
msg=pc_height_deprecation_warning,
rkey='display.height')

tc_sim_interactive_doc = """
: boolean
Whether to simulate interactive mode for purposes of testing
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ def detect_console_encoding():
def get_console_size():
"""Return console size as tuple = (width, height).
May return (None,None) in some cases.
Returns (None,None) in non-interactive session.
"""
display_width = get_option('display.width')
display_height = get_option('display.height')
Expand Down
62 changes: 26 additions & 36 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,21 +605,10 @@ def __nonzero__(self):

def _repr_fits_vertical_(self):
"""
Check if full repr fits in vertical boundaries imposed by the display
options height and max_rows. In case of non-interactive session,
no boundaries apply.
Check length against max_rows.
"""
width, height = fmt.get_console_size()
max_rows = get_option("display.max_rows")

if height is None and max_rows is None:
return True

else:
# min of two, where one may be None
height = height or max_rows +1
max_rows = max_rows or height +1
return len(self) <= min(max_rows, height)
return len(self) <= max_rows

def _repr_fits_horizontal_(self,ignore_width=False):
"""
Expand All @@ -632,8 +621,6 @@ def _repr_fits_horizontal_(self,ignore_width=False):
GH3541, GH3573
"""

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

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

if (get_option('display.width') is not None or
com.in_ipython_frontend()):
# check at least the column row for excessive width
max_rows = 1
else:
max_rows = get_option("display.max_rows")

# when auto-detecting, so width=None and not in ipython front end
# check whether repr fits horizontal by actualy checking
# the width of the rendered repr
buf = StringIO()

# only care about the stuff we'll actually print out
# and to_string on entire frame may be expensive
d = self
max_rows = get_option("display.max_rows")
if not (height is None and max_rows is None):

if not (max_rows is None): # unlimited rows
# min of two, where one may be None
height = height or max_rows +1
max_rows = max_rows or height +1
d=d.iloc[:min(max_rows, height,len(d))]
d=d.iloc[:min(max_rows,len(d))]
else:
return True

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

# special case ipnb+HTML repr
if not ignore_width:
return repr_width <= width
else:
return True
return repr_width < width

def __str__(self):
"""
Expand Down Expand Up @@ -709,14 +702,11 @@ def __unicode__(self):
if fits_vertical and fits_horizontal:
self.to_string(buf=buf)
else:
width, height = fmt.get_console_size()
max_rows = get_option("display.max_rows") or height
# expand_repr basically takes the extrac columns that don't
# fit the width, and creates a new page, which increases
# the effective row count. check number of cols agaibst
# max rows to catch wrapping. that would exceed max_rows.
if (get_option("display.expand_frame_repr") and fits_vertical and
len(self.columns) < max_rows):
width, _ = fmt.get_console_size()
max_rows = get_option("display.max_rows")
if (get_option("display.expand_frame_repr")
and fits_vertical):
# and len(self.columns) < max_rows)
self.to_string(buf=buf, line_width=width)
else:
max_info_rows = get_option('display.max_info_rows')
Expand Down Expand Up @@ -892,7 +882,7 @@ def __contains__(self, key):

# Python 2 division methods
if not py3compat.PY3:
__div__ = _arith_method(operator.div, '__div__', '/',
__div__ = _arith_method(operator.div, '__div__', '/',
default_axis=None, fill_zeros=np.inf)
__rdiv__ = _arith_method(lambda x, y: y / x, '__rdiv__',
default_axis=None, fill_zeros=np.inf)
Expand Down
32 changes: 20 additions & 12 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ def test_expand_frame_repr(self):
df_tall = DataFrame('hello', range(30), range(5))

with option_context('mode.sim_interactive', True):
with option_context('display.width', 50,
'display.height', 20):
with option_context('display.max_columns', 5,
'display.width',20,
'display.max_rows', 20):
with option_context('display.expand_frame_repr', True):
self.assertFalse(has_info_repr(df_small))
self.assertFalse(has_expanded_repr(df_small))
Expand Down Expand Up @@ -226,19 +227,21 @@ def mkframe(n):
# since not exceeding width
self.assertFalse(has_expanded_repr(df6))
self.assertFalse(has_info_repr(df6))

with option_context('display.max_rows', 9,
'display.max_columns', 10):
# out vertical bounds can not result in exanded repr
self.assertFalse(has_expanded_repr(df10))
self.assertTrue(has_info_repr(df10))

with option_context('display.max_columns', 0,
# width=None in terminal, auto detection
with option_context('display.max_columns', 100,
'display.max_rows', term_width * 20,
'display.width', 0):
'display.width', None):
df = mkframe((term_width // 7) - 2)
self.assertFalse(has_expanded_repr(df))
df = mkframe((term_width // 7) + 2)
print( df._repr_fits_horizontal_())
self.assertTrue(has_expanded_repr(df))

def test_to_string_repr_unicode(self):
Expand Down Expand Up @@ -787,7 +790,8 @@ def test_pprint_thing(self):
def test_wide_repr(self):
with option_context('mode.sim_interactive', True):
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
df = DataFrame([col(20, 25) for _ in range(10)])
max_cols = get_option('display.max_columns')
df = DataFrame([col(max_cols+1, 25) for _ in range(10)])
set_option('display.expand_frame_repr', False)
rep_str = repr(df)
set_option('display.expand_frame_repr', True)
Expand All @@ -810,7 +814,8 @@ def test_wide_repr_wide_columns(self):
def test_wide_repr_named(self):
with option_context('mode.sim_interactive', True):
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
df = DataFrame([col(20, 25) for _ in range(10)])
max_cols = get_option('display.max_columns')
df = DataFrame([col(max_cols+1, 25) for _ in range(10)])
df.index.name = 'DataFrame Index'
set_option('display.expand_frame_repr', False)

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

def test_wide_repr_multiindex_cols(self):
with option_context('mode.sim_interactive', True):
max_cols = get_option('display.max_columns')
col = lambda l, k: [tm.rands(k) for _ in xrange(l)]
midx = pandas.MultiIndex.from_arrays([np.array(col(10, 5)),
np.array(col(10, 5))])
mcols = pandas.MultiIndex.from_arrays([np.array(col(20, 3)),
np.array(col(20, 3))])
df = DataFrame([col(20, 25) for _ in range(10)],
mcols = pandas.MultiIndex.from_arrays([np.array(col(max_cols+1, 3)),
np.array(col(max_cols+1, 3))])
df = DataFrame([col(max_cols+1, 25) for _ in range(10)],
index=midx, columns=mcols)
df.index.names = ['Level 0', 'Level 1']
set_option('display.expand_frame_repr', False)
Expand All @@ -876,7 +883,8 @@ def test_wide_repr_multiindex_cols(self):
def test_wide_repr_unicode(self):
with option_context('mode.sim_interactive', True):
col = lambda l, k: [tm.randu(k) for _ in xrange(l)]
df = DataFrame([col(20, 25) for _ in range(10)])
max_cols = get_option('display.max_columns')
df = DataFrame([col(max_cols+1, 25) for _ in range(10)])
set_option('display.expand_frame_repr', False)
rep_str = repr(df)
set_option('display.expand_frame_repr', True)
Expand Down

0 comments on commit b036a7e

Please sign in to comment.