Skip to content

Commit 3c57381

Browse files
committed
Merge pull request #7108 from jreback/show_dim
API: allow option truncate for display.show_dimensions to only show dimensions if truncated (GH6457)
2 parents f26e668 + dc07e60 commit 3c57381

File tree

5 files changed

+60
-7
lines changed

5 files changed

+60
-7
lines changed

doc/source/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ Improvements to existing features
346346
- Regression in the display of a MultiIndexed Series with ``display.max_rows`` is less than the
347347
length of the series (:issue:`7101`)
348348
- :meth:`~DataFrame.describe` now accepts an array of percentiles to include in the summary statistics (:issue:`4196`)
349+
- allow option ``'truncate'`` for ``display.show_dimensions`` to only show the dimensions if the
350+
frame is truncated (:issue:`6547`)
349351

350352
.. _release.bug_fixes-0.14.0:
351353

doc/source/v0.14.0.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ API changes
204204
Display Changes
205205
~~~~~~~~~~~~~~~
206206

207+
- allow option ``'truncate'`` for ``display.show_dimensions`` to only show the dimensions if the
208+
frame is truncated (:issue:`6547`).
209+
210+
The default for ``display.show_dimensions`` will now be **truncate**! This is consistent with
211+
how Series display length.
212+
213+
.. ipython:: python
214+
215+
dfd = pd.DataFrame(np.arange(25).reshape(-1,5), index=[0,1,2,3,4], columns=[0,1,2,3,4])
216+
217+
# show dimensions only if truncated
218+
with pd.option_context('display.max_rows', 2, 'display.max_columns', 2,
219+
'display.show_dimensions', 'truncate'):
220+
print(dfd)
221+
222+
# show dimensions only if truncated
223+
with pd.option_context('display.max_rows', 10, 'display.max_columns', 40,
224+
'display.show_dimensions', 'truncate'):
225+
print(dfd)
226+
207227
- Regression in the display of a MultiIndexed Series with ``display.max_rows`` is less than the
208228
length of the series (:issue:`7101`)
209229
- Fixed a bug in the HTML repr of a truncated Series or DataFrame not showing the class name with the

pandas/core/config_init.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@
120120
"""
121121

122122
pc_show_dimensions_doc = """
123-
: boolean
123+
: boolean or 'truncate'
124124
Whether to print out dimensions at the end of DataFrame repr.
125+
If 'truncate' is specified, only print out the dimensions if the
126+
frame is truncated (e.g. not display all rows and/or columns)
125127
"""
126128

127129
pc_line_width_doc = """
@@ -247,7 +249,8 @@ def mpl_style_cb(key):
247249
cf.register_option('encoding', detect_console_encoding(), pc_encoding_doc,
248250
validator=is_text)
249251
cf.register_option('expand_frame_repr', True, pc_expand_repr_doc)
250-
cf.register_option('show_dimensions', True, pc_show_dimensions_doc)
252+
cf.register_option('show_dimensions', True, pc_show_dimensions_doc,
253+
validator=is_one_of_factory([True, False, 'truncate']))
251254
cf.register_option('chop_threshold', None, pc_chop_threshold_doc)
252255
cf.register_option('max_seq_items', 100, pc_max_seq_items)
253256
cf.register_option('mpl_style', None, pc_mpl_style_doc,

pandas/core/format.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ def _strlen(x):
236236

237237

238238
class TableFormatter(object):
239+
is_truncated = False
240+
show_dimensions = None
241+
242+
@property
243+
def should_show_dimensions(self):
244+
return self.show_dimensions is True or (self.show_dimensions == 'truncate' and self.is_truncated)
239245

240246
def _get_formatter(self, i):
241247
if isinstance(self.formatters, (list, tuple)):
@@ -315,9 +321,9 @@ def _to_str_columns(self):
315321
_strlen = _strlen_func()
316322

317323
cols_to_show = self.columns[:self.max_cols]
318-
truncate_h = self.max_cols and (len(self.columns) > self.max_cols)
319-
truncate_v = self.max_rows and (len(self.frame) > self.max_rows)
320-
self.truncated_v = truncate_v
324+
self.truncated_h = truncate_h = self.max_cols and (len(self.columns) > self.max_cols)
325+
self.truncated_v = truncate_v = self.max_rows and (len(self.frame) > self.max_rows)
326+
self.is_truncated = self.truncated_h or self.truncated_v
321327
if truncate_h:
322328
cols_to_show = self.columns[:self.max_cols]
323329
else:
@@ -380,7 +386,7 @@ def to_string(self):
380386

381387
self.buf.writelines(text)
382388

383-
if self.show_dimensions:
389+
if self.should_show_dimensions:
384390
self.buf.write("\n\n[%d rows x %d columns]"
385391
% (len(frame), len(frame.columns)))
386392

@@ -634,6 +640,8 @@ def __init__(self, formatter, classes=None, max_rows=None, max_cols=None):
634640

635641
self.max_rows = max_rows or len(self.fmt.frame)
636642
self.max_cols = max_cols or len(self.fmt.columns)
643+
self.show_dimensions = self.fmt.show_dimensions
644+
self.is_truncated = self.max_rows < len(self.fmt.frame) or self.max_cols < len(self.fmt.columns)
637645

638646
def write(self, s, indent=0):
639647
rs = com.pprint_thing(s)
@@ -709,7 +717,7 @@ def write_result(self, buf):
709717
indent = self._write_body(indent)
710718

711719
self.write('</table>', indent)
712-
if self.fmt.show_dimensions:
720+
if self.should_show_dimensions:
713721
by = chr(215) if compat.PY3 else unichr(215) # ×
714722
self.write(u('<p>%d rows %s %d columns</p>') %
715723
(len(frame), by, len(frame.columns)))

pandas/tests/test_format.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,26 @@ def test_to_string_line_width(self):
11971197
s = df.to_string(line_width=80)
11981198
self.assertEqual(max(len(l) for l in s.split('\n')), 80)
11991199

1200+
def test_show_dimensions(self):
1201+
df = pd.DataFrame(123, lrange(10, 15), lrange(30))
1202+
1203+
with option_context('display.max_rows', 10, 'display.max_columns', 40, 'display.width',
1204+
500, 'display.expand_frame_repr', 'info', 'display.show_dimensions', True):
1205+
self.assertTrue('5 rows' in str(df))
1206+
self.assertTrue('5 rows' in df._repr_html_())
1207+
with option_context('display.max_rows', 10, 'display.max_columns', 40, 'display.width',
1208+
500, 'display.expand_frame_repr', 'info', 'display.show_dimensions', False):
1209+
self.assertFalse('5 rows' in str(df))
1210+
self.assertFalse('5 rows' in df._repr_html_())
1211+
with option_context('display.max_rows', 2, 'display.max_columns', 2, 'display.width',
1212+
500, 'display.expand_frame_repr', 'info', 'display.show_dimensions', 'truncate'):
1213+
self.assertTrue('5 rows' in str(df))
1214+
self.assertTrue('5 rows' in df._repr_html_())
1215+
with option_context('display.max_rows', 10, 'display.max_columns', 40, 'display.width',
1216+
500, 'display.expand_frame_repr', 'info', 'display.show_dimensions', 'truncate'):
1217+
self.assertFalse('5 rows' in str(df))
1218+
self.assertFalse('5 rows' in df._repr_html_())
1219+
12001220
def test_to_html(self):
12011221
# big mixed
12021222
biggie = DataFrame({'A': randn(200),

0 commit comments

Comments
 (0)