-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Shorter truncated Series/DataFrame repr: introduce min_rows #27095
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1361fa4
Shorter truncated Series/DataFrame repr: introduce min_rows
jorisvandenbossche d62407a
Merge remote-tracking branch 'upstream/master' into shorter-repr
jorisvandenbossche b8f483e
add tests
jorisvandenbossche 98e7d43
add some comments
jorisvandenbossche e4b2144
simplify
jorisvandenbossche 8060faf
add whatsnew + docs
jorisvandenbossche 41c8543
doc fix
jorisvandenbossche 577f078
add to Formatter / to_string docstring
jorisvandenbossche 577c5cf
fixup
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1486,17 +1486,20 @@ def __repr__(self): | |
width, height = get_terminal_size() | ||
max_rows = (height if get_option("display.max_rows") == 0 else | ||
get_option("display.max_rows")) | ||
min_rows = (height if get_option("display.max_rows") == 0 else | ||
get_option("display.min_rows")) | ||
show_dimensions = get_option("display.show_dimensions") | ||
|
||
self.to_string(buf=buf, name=self.name, dtype=self.dtype, | ||
max_rows=max_rows, length=show_dimensions) | ||
min_rows=min_rows, max_rows=max_rows, | ||
length=show_dimensions) | ||
result = buf.getvalue() | ||
|
||
return result | ||
|
||
def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True, | ||
index=True, length=False, dtype=False, name=False, | ||
max_rows=None): | ||
max_rows=None, min_rows=None): | ||
""" | ||
Render a string representation of the Series. | ||
|
||
|
@@ -1522,6 +1525,9 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True, | |
max_rows : int, optional | ||
Maximum number of rows to show before truncating. If None, show | ||
all. | ||
min_rows : int, optional | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in theory should add a versionaded here, but not worth repushing |
||
The number of rows to display in a truncated repr (when number | ||
of rows is above `max_rows`). | ||
|
||
Returns | ||
------- | ||
|
@@ -1533,6 +1539,7 @@ def to_string(self, buf=None, na_rep='NaN', float_format=None, header=True, | |
header=header, index=index, | ||
dtype=dtype, na_rep=na_rep, | ||
float_format=float_format, | ||
min_rows=min_rows, | ||
max_rows=max_rows) | ||
result = formatter.to_string() | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,9 @@ | |
* unset. | ||
max_rows : int, optional | ||
Maximum number of rows to display in the console. | ||
min_rows : int, optional | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
The number of rows to display in the console in a truncated repr | ||
(when number of rows is above `max_rows`). | ||
max_cols : int, optional | ||
Maximum number of columns to display in the console. | ||
show_dimensions : bool, default False | ||
|
@@ -159,7 +162,7 @@ class SeriesFormatter: | |
|
||
def __init__(self, series, buf=None, length=True, header=True, index=True, | ||
na_rep='NaN', name=False, float_format=None, dtype=True, | ||
max_rows=None): | ||
max_rows=None, min_rows=None): | ||
self.series = series | ||
self.buf = buf if buf is not None else StringIO() | ||
self.name = name | ||
|
@@ -168,6 +171,7 @@ def __init__(self, series, buf=None, length=True, header=True, index=True, | |
self.length = length | ||
self.index = index | ||
self.max_rows = max_rows | ||
self.min_rows = min_rows | ||
|
||
if float_format is None: | ||
float_format = get_option("display.float_format") | ||
|
@@ -179,10 +183,17 @@ def __init__(self, series, buf=None, length=True, header=True, index=True, | |
|
||
def _chk_truncate(self): | ||
from pandas.core.reshape.concat import concat | ||
min_rows = self.min_rows | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
max_rows = self.max_rows | ||
# truncation determined by max_rows, actual truncated number of rows | ||
# used below by min_rows | ||
truncate_v = max_rows and (len(self.series) > max_rows) | ||
series = self.series | ||
if truncate_v: | ||
if min_rows: | ||
# if min_rows is set (not None or 0), set max_rows to minimum | ||
# of both | ||
max_rows = min(min_rows, max_rows) | ||
if max_rows == 1: | ||
row_num = max_rows | ||
series = series.iloc[:max_rows] | ||
|
@@ -391,8 +402,8 @@ def __init__(self, frame, buf=None, columns=None, col_space=None, | |
header=True, index=True, na_rep='NaN', formatters=None, | ||
justify=None, float_format=None, sparsify=None, | ||
index_names=True, line_width=None, max_rows=None, | ||
max_cols=None, show_dimensions=False, decimal='.', | ||
table_id=None, render_links=False, **kwds): | ||
min_rows=None, max_cols=None, show_dimensions=False, | ||
decimal='.', table_id=None, render_links=False, **kwds): | ||
self.frame = frame | ||
if buf is not None: | ||
self.buf = _expand_user(_stringify_path(buf)) | ||
|
@@ -414,6 +425,7 @@ def __init__(self, frame, buf=None, columns=None, col_space=None, | |
self.index = index | ||
self.line_width = line_width | ||
self.max_rows = max_rows | ||
self.min_rows = min_rows | ||
self.max_cols = max_cols | ||
self.max_rows_displayed = min(max_rows or len(self.frame), | ||
len(self.frame)) | ||
|
@@ -471,6 +483,10 @@ def _chk_truncate(self): | |
max_rows = h | ||
|
||
if not hasattr(self, 'max_rows_adj'): | ||
if max_rows: | ||
if (len(self.frame) > max_rows) and self.min_rows: | ||
# if truncated, set max_rows showed to min_rows | ||
max_rows = min(self.min_rows, max_rows) | ||
self.max_rows_adj = max_rows | ||
if not hasattr(self, 'max_cols_adj'): | ||
self.max_cols_adj = max_cols | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can the logic be added before this call to
.to_string
(isn't it as simple as just set max_rows to min_rows if len(self.frame) > max_rows?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the simple cases, yes. And I agree that could be a nice option. But there are some corner cases that are only handled within the
DataFrameFormatter
class (eg if max_rows = 0, we check the terminal size)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although, looking at it more closely, that special case is only if
max_rows
is 0 or None, so that could be easily checked. So yes, this would in principle be possible (and probably even give easier code). But that means that we move some of the logic of the repr out of the Formatter class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i agree. but this logic is related to display options. i'd prefer to see all the display option handling removed from the Formatter classes entirely and only appear in
__repr__
and_repr_html_
it appears from the comments that adding arguments to
to_string
is not considered undesirable. so this topic can be addressed another day.