Skip to content

Commit 67121af

Browse files
committed
BUG: DataFrame column formatting issue in length-truncated column close pandas-dev#1906
1 parent 82b0d2a commit 67121af

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ pandas 0.9.0
160160
window (#1884)
161161
- Fix unicode sheet name failure in to_excel (#1828)
162162
- Override DatetimeIndex.min/max to return Timestamp objects (#1895)
163+
- Fix column name formatting issue in length-truncated column (#1906)
163164

164165
pandas 0.8.1
165166
============

pandas/core/format.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,20 @@ def _to_str_columns(self, force_unicode=False):
219219
if self.header:
220220
fmt_values = self._format_col(i)
221221
cheader = str_columns[i]
222+
223+
max_colwidth = max(_strlen(x) for x in cheader)
224+
225+
fmt_values = _make_fixed_width(fmt_values, self.justify,
226+
minimum=max_colwidth)
227+
222228
max_len = max(max(_strlen(x) for x in fmt_values),
223-
max(len(x) for x in cheader))
229+
max_colwidth)
224230
if self.justify == 'left':
225231
cheader = [x.ljust(max_len) for x in cheader]
226232
else:
227233
cheader = [x.rjust(max_len) for x in cheader]
228-
fmt_values = cheader + fmt_values
229-
stringified.append(_make_fixed_width(fmt_values,
230-
self.justify))
234+
235+
stringified.append(cheader + fmt_values)
231236
else:
232237
stringified = [_make_fixed_width(self._format_col(i),
233238
self.justify)
@@ -704,11 +709,15 @@ def _format_datetime64(x, tz=None):
704709
return stamp._repr_base
705710

706711

707-
def _make_fixed_width(strings, justify='right'):
712+
def _make_fixed_width(strings, justify='right', minimum=None):
708713
if len(strings) == 0:
709714
return strings
710715

711716
max_len = max(_strlen(x) for x in strings)
717+
718+
if minimum is not None:
719+
max_len = max(minimum, max_len)
720+
712721
conf_max = print_config.max_colwidth
713722
if conf_max is not None and max_len > conf_max:
714723
max_len = conf_max

pandas/tests/test_frame.py

+15
Original file line numberDiff line numberDiff line change
@@ -2614,6 +2614,21 @@ def test_very_wide_info_repr(self):
26142614
columns=[tm.rands(10) for _ in xrange(20)])
26152615
repr(df)
26162616

2617+
def test_repr_column_name_unicode_truncation_bug(self):
2618+
# #1906
2619+
df = DataFrame({'Id': [7117434],
2620+
'StringCol': ('Is it possible to modify drop plot code'
2621+
' so that the output graph is displayed '
2622+
'in iphone simulator, Is it possible to '
2623+
'modify drop plot code so that the '
2624+
'output graph is \xe2\x80\xa8displayed '
2625+
'in iphone simulator.Now we are adding '
2626+
'the CSV file externally. I want to Call'
2627+
' the File through the code..')})
2628+
2629+
result = repr(df)
2630+
self.assert_('StringCol' in result)
2631+
26172632
def test_head_tail(self):
26182633
assert_frame_equal(self.frame.head(), self.frame[:5])
26192634
assert_frame_equal(self.frame.tail(), self.frame[-5:])

pandas/tseries/tests/test_timeseries.py

+1
Original file line numberDiff line numberDiff line change
@@ -2151,3 +2151,4 @@ def test_hash_equivalent(self):
21512151
if __name__ == '__main__':
21522152
nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
21532153
exit=False)
2154+

0 commit comments

Comments
 (0)