Skip to content

Commit 0d6264f

Browse files
onesandzeroesjreback
authored andcommitted
BUG: DataFrame.to_string ignores col_space when header=False
Add test case for when header=False Make col_space the minimum width of each col Update release notes
1 parent b73cfd0 commit 0d6264f

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

doc/source/v0.15.0.txt

+4-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ API changes
102102
2 6
103103
3 NaN
104104
dtype: float64
105-
105+
106106
New behavior (note final value is ``5 = sum([2, 3, NaN])``):
107107

108108
.. ipython:: python
@@ -139,7 +139,7 @@ API changes
139139
4 1.571429
140140
5 2.189189
141141
dtype: float64
142-
142+
143143
New behavior (note values start at index ``4``, the location of the 2nd (since ``min_periods=2``) non-empty value):
144144

145145
.. ipython:: python
@@ -761,3 +761,5 @@ Bug Fixes
761761

762762
- Bug in interpolation methods with the ``limit`` keyword when no values
763763
needed interpolating (:issue:`7173`).
764+
- Bug where ``col_space`` was ignored in ``DataFrame.to_string()`` when ``header=False``
765+
(:issue:`8230`).

pandas/core/format.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ def _to_str_columns(self):
391391
for i, c in enumerate(frame):
392392
formatter = self._get_formatter(i)
393393
fmt_values = self._format_col(i)
394-
fmt_values = _make_fixed_width(fmt_values, self.justify)
394+
fmt_values = _make_fixed_width(fmt_values, self.justify,
395+
minimum=(self.col_space or 0))
395396

396397
stringified.append(fmt_values)
397398

pandas/tests/test_format.py

+7
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ def test_to_string_with_col_space(self):
385385
c30 = len(df.to_string(col_space=30).split("\n")[1])
386386
self.assertTrue(c10 < c20 < c30)
387387

388+
# GH 8230
389+
# col_space wasn't being applied with header=False
390+
with_header = df.to_string(col_space=20)
391+
with_header_row1 = with_header.splitlines()[1]
392+
no_header = df.to_string(col_space=20, header=False)
393+
self.assertEqual(len(with_header_row1), len(no_header))
394+
388395
def test_to_string_truncate_indices(self):
389396
for index in [ tm.makeStringIndex, tm.makeUnicodeIndex, tm.makeIntIndex,
390397
tm.makeDateIndex, tm.makePeriodIndex ]:

0 commit comments

Comments
 (0)