diff --git a/pandas/core/format.py b/pandas/core/format.py index 4e03a5c68b2b3..f210b801d8cee 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -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 @@ -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. diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2a1ee06984aa8..a457430d2f563 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -605,15 +605,11 @@ 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): """ @@ -621,23 +617,19 @@ def _repr_fits_horizontal_(self): 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): """ @@ -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 @@ -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 ('
\n' + self.to_html() + '\n
') diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index 6be4c846845e9..ea18f3404b3e4 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -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: diff --git a/vb_suite/frame_methods.py b/vb_suite/frame_methods.py index ddf9124ac90ef..7745450e5c03b 100644 --- a/vb_suite/frame_methods.py +++ b/vb_suite/frame_methods.py @@ -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 + """