Series/DataFrame, make decision between full and short repr configurable by lodagro · Pull Request #453 · pandas-dev/pandas (original) (raw)
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 andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation4 Commits3 Checks0 Files changed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
[ Show hidden characters]({{ revealButtonHref }})
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.
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
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.
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.
"""
this is awesome. I'm going to use max_columns=0
as the default...seems to work OK in the IPython HTML notebook also
2 participants