Skip to content

BUG: fix df repr troubles #3395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pandas.core.index import Index, MultiIndex, _ensure_index
from pandas.util import py3compat
from pandas.util.compat import OrderedDict
from pandas.util.terminal import get_terminal_size
from pandas.core.config import get_option, set_option, reset_option
import pandas.core.common as com
import pandas.lib as lib
Expand Down Expand Up @@ -1665,6 +1666,19 @@ def detect_console_encoding():
return encoding


def get_console_size():
"""Return console size as tuple = (width, height)."""
display_width = get_option('display.width')
display_height = get_option('display.height')

if com.in_interactive_session():
terminal_width, terminal_height = get_terminal_size()
else:
terminal_width, terminal_height = 80, 100

return (display_width or terminal_width, display_height or terminal_height)


class EngFormatter(object):
"""
Formats float values according to engineering format.
Expand Down
39 changes: 19 additions & 20 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,39 +605,31 @@ def _repr_fits_vertical_(self):
options height and max_columns. In case off non-interactive session,
no boundaries apply.
"""
if not com.in_interactive_session():
return True

terminal_width, terminal_height = get_terminal_size()
width, height = fmt.get_console_size()

# excluding column axis area
max_rows = get_option("display.max_rows") or terminal_height
display_height = get_option("display.height") or terminal_height
return len(self.index) <= min(max_rows, display_height)
max_rows = get_option("display.max_rows") or height
return len(self) <= min(max_rows, height)

def _repr_fits_horizontal_(self):
"""
Check if full repr fits in horizontal boundaries imposed by the display
options width and max_columns. In case off non-interactive session, no
boundaries apply.
"""
if not com.in_interactive_session():
return True

terminal_width, terminal_height = get_terminal_size()
width, height = fmt.get_console_size()

max_columns = get_option("display.max_columns")
display_width = get_option("display.width") or terminal_width
nb_columns = len(self.columns)
if ((max_columns and nb_columns > max_columns) or
(nb_columns > (display_width // 2))):
(nb_columns > (width // 2))):
return False

buf = StringIO()
self.to_string(buf=buf)
value = buf.getvalue()
repr_width = max([len(l) for l in value.split('\n')])
return repr_width <= display_width
return repr_width <= width

def __str__(self):
"""
Expand Down Expand Up @@ -670,19 +662,21 @@ def __unicode__(self):
"""
buf = StringIO(u"")
fits_vertical = self._repr_fits_vertical_()
fits_horizontal = self._repr_fits_horizontal_()
fits_horizontal = False
if fits_vertical:
fits_horizontal = self._repr_fits_horizontal_()

if fits_vertical and fits_horizontal:
self.to_string(buf=buf)
else:
terminal_width, terminal_height = get_terminal_size()
max_rows = get_option("display.max_rows") or terminal_height
width, height = fmt.get_console_size()
max_rows = get_option("display.max_rows") or height
# Expand or info? Decide based on option display.expand_frame_repr
# and keep it sane for the number of display rows used by the
# expanded repr.
if (get_option("display.expand_frame_repr") and fits_vertical and
len(self.columns) < max_rows):
line_width = get_option("display.width") or terminal_width
self.to_string(buf=buf, line_width=line_width)
self.to_string(buf=buf, line_width=width)
else:
max_info_rows = get_option('display.max_info_rows')
verbose = (max_info_rows is None or
Expand Down Expand Up @@ -711,7 +705,12 @@ def _repr_html_(self):
raise ValueError('Disable HTML output in QtConsole')

if get_option("display.notebook_repr_html"):
if self._repr_fits_horizontal_() and self._repr_fits_vertical_():
fits_vertical = self._repr_fits_vertical_()
fits_horizontal = False
if fits_vertical:
fits_horizontal = self._repr_fits_horizontal_()

if fits_horizontal and fits_vertical:
return ('<div style="max-height:1000px;'
'max-width:1500px;overflow:auto;">\n' +
self.to_html() + '\n</div>')
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ def test_expand_frame_repr(self):
self.assertTrue(has_info_repr(df_tall))
self.assertFalse(has_expanded_repr(df_tall))

def test_repr_non_interactive(self):
# in non interactive mode, there can be no dependency on the
# result of terminal auto size detection
df = DataFrame('hello', range(99), range(5))

with option_context('mode.sim_interactive', False,
'display.width', 0,
'display.height', 0):
self.assertFalse(has_info_repr(df))
self.assertFalse(has_expanded_repr(df))

def test_repr_max_columns_max_rows(self):
term_width, term_height = get_terminal_size()
if term_width < 10 or term_height < 10:
Expand Down
17 changes: 8 additions & 9 deletions vb_suite/frame_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,19 @@ def f(K=500):

##
setup = common_setup + """
from pandas.core.config import option_context

def interactive_repr(frame):
with option_context('mode.sim_interactive', True):
repr(frame)

df = pandas.DataFrame(np.random.randn(10,10000))
"""

frame_wide_repr = Benchmark('repr(df)', setup,
frame_repr_wide = Benchmark('repr(df)', setup,
start_date=datetime(2012, 8, 1))

frame_wide_repr_interactive = Benchmark('interactive_repr(df)', setup,
start_date=datetime(2012, 8, 1))
##
setup = common_setup + """
df = pandas.DataFrame(np.random.randn(10000, 10))
"""

frame_repr_tall = Benchmark('repr(df)', setup,
start_date=datetime(2012, 8, 1))

##
setup = common_setup + """
Expand Down