Skip to content

Series/DataFrame, make decision between full and short __repr__ configurable #453

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 3 commits into from

Conversation

lodagro
Copy link
Contributor

@lodagro lodagro commented Dec 6, 2011

Extending pandas.set_printoptions() with two extra arguments: max_rows and max_columns.
These two numbers impact pandas.Series.__repr__() and pandas.DataFrame.__repr__().
Decision between a short representation or a full one is now configurable. Default values are set to the current ones, so the change is transparent.

When using DataFrames with large number of columns, i got tired of typing print df.to_string() all the time :-)

set_printoptions(precision=None, column_space=None, max_rows=None,
           max_columns=None)
    Alter default behavior of DataFrame.toString

    precision : int
        Floating point output precision
    column_space : int
        Default space for DataFrame columns, defaults to 12
    max_rows : int
    max_columns : int
        max_rows and max_columns are used in __repr__() methods to decide if
        to_string() or info() is used to render an object to a string.

@wesm
Copy link
Member

wesm commented Dec 6, 2011

This is cool. I've also gotten tired of it myself.

What would you think with adding some logic that will print df.to_string() whenever the DataFrame fits in the console buffer? There are some tricks you can use to get the console width (this would only apply when IPython is in use), if it can't infer the width then it would just default to 80 or something. Been on my todo list for a while (because I also get quite tired of typing print df.to_string() as you can imagine).

There is also the R approach of breaking the DataFrame apart and printing multiple "rows" of columns. Not sure how I feel about doing that

@lodagro
Copy link
Contributor Author

lodagro commented Dec 6, 2011

Using the actual width of the console is the best way to avoid line wraps of course, the current code gives no guarantee that wrapping will not occur. Furthermore this will save a call to set_printoptions(), handy for interactive use.

Did some googling on how to retrieve the width of the console in (i)python, this does not look so straight forward, especially across platforms. Need to study this a bit more in detail before i can add the idea you suggest.

The R approach looks a bit awkward.

@lodagro
Copy link
Contributor Author

lodagro commented Dec 7, 2011

Added a clever screen render mode (default nothing is changed!).
When either max_rows, max_columns, or both is set to 0 pandas will figure out how big the terminal is and will not display more rows or/and columns that can fit on it.

Terminal width and height is determined using new method pandas.util.terminal.get_terminal_size(), since i only have python on linux, other operating systems are untested. Furthermore i only tried python 2.7 (with and without IPython 0.11). That is why i named this mode experimental in the documentation of pandas.set_printoptions().

def set_printoptions(precision=None, column_space=None, max_rows=None,
        max_columns=None):
    """
    Alter default behavior of DataFrame.toString

    precision : int
        Floating point output precision
    column_space : int
        Default space for DataFrame columns, defaults to 12
    max_rows : int
    max_columns : int
        max_rows and max_columns are used in __repr__() methods to decide if
        to_string() or info() is used to render an object to a string.
        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.
    """

@wesm
Copy link
Member

wesm commented Dec 8, 2011

this is awesome. I'm going to use max_columns=0 as the default...seems to work OK in the IPython HTML notebook also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants