diff --git a/pandas/core/format.py b/pandas/core/format.py index c572779139cec..3e45397d4ceda 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -581,7 +581,8 @@ def _has_names(index): # Global formatting options def set_printoptions(precision=None, column_space=None, max_rows=None, - max_columns=None, colheader_justify='right'): + max_columns=None, colheader_justify='right', + notebook_repr_html=None): """ Alter default behavior of DataFrame.toString @@ -597,6 +598,10 @@ def set_printoptions(precision=None, column_space=None, max_rows=None, Either one, or both can be set to 0 (experimental). Pandas will figure out how big the terminal is and will not display more rows or/and columns that can fit on it. + colheader_justify + notebook_repr_html : boolean + When True (default), IPython notebook will use html representation for + pandas objects (if it is available). """ if precision is not None: print_config.precision = precision @@ -608,6 +613,8 @@ def set_printoptions(precision=None, column_space=None, max_rows=None, print_config.max_columns = max_columns if colheader_justify is not None: print_config.colheader_justify = colheader_justify + if notebook_repr_html is not None: + print_config.notebook_repr_html = notebook_repr_html def reset_printoptions(): print_config.reset() @@ -733,6 +740,7 @@ def __init__(self): self.max_rows = 200 self.max_columns = 0 self.colheader_justify = 'right' + self.notebook_repr_html = True def reset(self): self.__init__() diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b5d34a1934254..24be80f68efbd 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -435,9 +435,10 @@ def __nonzero__(self): # e.g. "if frame: ..." return len(self.columns) > 0 and len(self.index) > 0 - def __repr__(self): + def _need_info_repr_(self): """ - Return a string representation for a particular DataFrame + Check if it is needed to use info/summary view to represent a + particular DataFrame. """ config = fmt.print_config @@ -446,35 +447,50 @@ def __repr__(self): else config.max_rows) max_columns = config.max_columns - buf = StringIO() if max_columns > 0: - if len(self.index) < max_rows and \ + if len(self.index) <= max_rows and \ len(self.columns) <= max_columns: - self.to_string(buf=buf) + return False else: - self.info(buf=buf, verbose=self._verbose_info) + return True else: if len(self.index) > max_rows: - self.info(buf=buf, verbose=self._verbose_info) + return True else: + buf = StringIO() self.to_string(buf=buf) value = buf.getvalue() if max([len(l) for l in value.split('\n')]) > terminal_width: - buf = StringIO() - self.info(buf=buf, verbose=self._verbose_info) - value = buf.getvalue() - return com.console_encode(value) - return com.console_encode(buf.getvalue()) + return True + else: + return False + + def __repr__(self): + """ + Return a string representation for a particular DataFrame + """ + buf = StringIO() + if self._need_info_repr_(): + self.info(buf=buf, verbose=self._verbose_info) + else: + self.to_string(buf=buf) + value = buf.getvalue() + return com.console_encode(value) def _repr_html_(self): - if len(self.index) <= 1000 and len(self.columns)<= 20: - return ('
' + - self.to_html() + '
') + """ + Return a html representation for a particular DataFrame. + Mainly for IPython notebook. + """ + if fmt.print_config.notebook_repr_html: + if self._need_info_repr_(): + return None + else: + return ('
\n' + + self.to_html() + '\n
') else: - buf = StringIO() - self.info(buf=buf, verbose=self._verbose_info) - return '
' + com.console_encode(buf.getvalue()) + '
' + return None def __iter__(self): """