From 7994fbf03c3fd1669be199ae6d608fc5d2dd4609 Mon Sep 17 00:00:00 2001 From: Wouter Overmeire Date: Thu, 13 Sep 2012 21:35:11 +0200 Subject: [PATCH] ENH: indicate DataFrame repr truncation, close #1854 --- pandas/core/format.py | 6 +++++- pandas/tests/test_format.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pandas/core/format.py b/pandas/core/format.py index d21e412ab2d64..8f4d1b592904d 100644 --- a/pandas/core/format.py +++ b/pandas/core/format.py @@ -733,7 +733,11 @@ def just(x): except UnicodeError: eff_len = max_len - return justfunc(x[:eff_len], eff_len) + if conf_max is not None: + if (conf_max > 3) & (_strlen(x) > max_len): + x = x[:eff_len - 3] + '...' + + return justfunc(x, eff_len) return [just(x) for x in strings] diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index b4ab3edc14db2..571cc50f18716 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -61,6 +61,26 @@ def test_repr_tuples(self): repr(df) df.to_string(col_space=10, buf=buf) + def test_repr_truncation(self): + max_len = 20 + fmt.print_config.max_colwidth = max_len + df = DataFrame({'A': np.random.randn(10), + 'B': [tm.rands(np.random.randint(max_len - 1, + max_len + 1)) for i in range(10)]}) + r = repr(df) + r = r[r.find('\n') + 1:] + for line, value in zip(r.split('\n'), df['B']): + if fmt._strlen(value) + 1 > max_len: + self.assert_('...' in line) + else: + self.assert_('...' not in line) + + fmt.print_config.max_colwidth = None + self.assert_('...' not in repr(df)) + + fmt.print_config.max_colwidth = max_len + 2 + self.assert_('...' not in repr(df)) + def test_to_string_repr_unicode(self): buf = StringIO()