Skip to content

Commit e4cb0f8

Browse files
tiagoantaojreback
authored andcommitted
Applying max_colwidth to the DataFrame index (#7856)
justification to comply with tests
1 parent c68fec2 commit e4cb0f8

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v0.16.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Bug Fixes
190190
- Bug in ``read_sql_table`` error when reading postgres table with timezone (:issue:`7139`)
191191
- Bug in ``DataFrame`` slicing may not retain metadata (:issue:`9776`)
192192
- Bug where ``TimdeltaIndex`` were not properly serialized in fixed ``HDFStore`` (:issue:`9635`)
193+
- Bug in ``DataFrameFormatter._get_formatted_index`` with not applying ``max_colwidth`` to the ``DataFrame`` index (:issue:`7856`)
193194

194195
- Bug in ``groupby.apply()`` that would raise if a passed user defined function either returned only ``None`` (for all input). (:issue:`9685`)
195196

pandas/core/format.py

+3
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,9 @@ def _get_formatted_index(self, frame):
776776
formatter=fmt)
777777
else:
778778
fmt_index = [index.format(name=show_index_names, formatter=fmt)]
779+
fmt_index = [tuple(_make_fixed_width(
780+
list(x), justify='left', minimum=(self.col_space or 0)))
781+
for x in fmt_index]
779782

780783
adjoined = adjoin(1, *fmt_index).split('\n')
781784

pandas/tests/test_format.py

+15
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,21 @@ def mkframe(n):
298298
com.pprint_thing(df._repr_fits_horizontal_())
299299
self.assertTrue(has_expanded_repr(df))
300300

301+
def test_str_max_colwidth(self):
302+
# GH 7856
303+
df = pd.DataFrame([{'a': 'foo', 'b': 'bar',
304+
'c': 'uncomfortably long line with lots of stuff',
305+
'd': 1},
306+
{'a': 'foo', 'b': 'bar', 'c': 'stuff', 'd': 1}])
307+
df.set_index(['a', 'b', 'c'])
308+
self.assertTrue(str(df) == ' a b c d\n'
309+
'0 foo bar uncomfortably long line with lots of stuff 1\n'
310+
'1 foo bar stuff 1')
311+
with option_context('max_colwidth', 20):
312+
self.assertTrue(str(df) == ' a b c d\n'
313+
'0 foo bar uncomfortably lo... 1\n'
314+
'1 foo bar stuff 1')
315+
301316
def test_auto_detect(self):
302317
term_width, term_height = get_terminal_size()
303318
fac = 1.05 # Arbitrary large factor to exceed term widht

0 commit comments

Comments
 (0)