Skip to content

Commit f553668

Browse files
lodagrowesm
authored andcommitted
ENH: indicate DataFrame repr truncation, close #1854
1 parent 70a8c98 commit f553668

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pandas/core/format.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,11 @@ def just(x):
733733
except UnicodeError:
734734
eff_len = max_len
735735

736-
return justfunc(x[:eff_len], eff_len)
736+
if conf_max is not None:
737+
if (conf_max > 3) & (_strlen(x) > max_len):
738+
x = x[:eff_len - 3] + '...'
739+
740+
return justfunc(x, eff_len)
737741

738742
return [just(x) for x in strings]
739743

pandas/tests/test_format.py

+20
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ def test_repr_tuples(self):
6161
repr(df)
6262
df.to_string(col_space=10, buf=buf)
6363

64+
def test_repr_truncation(self):
65+
max_len = 20
66+
fmt.print_config.max_colwidth = max_len
67+
df = DataFrame({'A': np.random.randn(10),
68+
'B': [tm.rands(np.random.randint(max_len - 1,
69+
max_len + 1)) for i in range(10)]})
70+
r = repr(df)
71+
r = r[r.find('\n') + 1:]
72+
for line, value in zip(r.split('\n'), df['B']):
73+
if fmt._strlen(value) + 1 > max_len:
74+
self.assert_('...' in line)
75+
else:
76+
self.assert_('...' not in line)
77+
78+
fmt.print_config.max_colwidth = None
79+
self.assert_('...' not in repr(df))
80+
81+
fmt.print_config.max_colwidth = max_len + 2
82+
self.assert_('...' not in repr(df))
83+
6484
def test_to_string_repr_unicode(self):
6585
buf = StringIO()
6686

0 commit comments

Comments
 (0)