Skip to content

Commit bc0a166

Browse files
Vutsuak16jreback
authored andcommitted
API: leading whitespaces have been removed for to_string(index=False), #11833
1 parent 35457b1 commit bc0a166

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

doc/source/whatsnew/v0.18.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ In addition, ``.round()`` will be available thru the ``.dt`` accessor of ``Serie
161161

162162
.. _whatsnew_0180.api_breaking:
163163

164+
- the leading whitespaces have been removed from the output of ``.to_string(index=False)`` method (:issue:`11833`)
165+
164166
Backwards incompatible API changes
165167
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166168

pandas/core/format.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ def to_string(self):
245245
if self.index:
246246
result = self.adj.adjoin(3, *[fmt_index[1:], fmt_values])
247247
else:
248-
result = self.adj.adjoin(3, fmt_values)
248+
result = self.adj.adjoin(3, fmt_values).replace('\n ',
249+
'\n').strip()
249250

250251
if self.header and have_header:
251252
result = fmt_index[0] + '\n' + result
@@ -577,7 +578,8 @@ def to_string(self):
577578
self._chk_truncate()
578579
strcols = self._to_str_columns()
579580
text = self.adj.adjoin(1, *strcols)
580-
581+
if not self.index:
582+
text = text.replace('\n ', '\n').strip()
581583
self.buf.writelines(text)
582584

583585
if self.should_show_dimensions:

pandas/tests/test_format.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -*- coding: utf-8 -*-
1+
# -*- coding: utf-8 -*-
22
from __future__ import print_function
33
from distutils.version import LooseVersion
44
import re
@@ -1725,7 +1725,7 @@ def test_to_string_no_index(self):
17251725
'y': [4, 5, 6]})
17261726

17271727
df_s = df.to_string(index=False)
1728-
expected = " x y\n 1 4\n 2 5\n 3 6"
1728+
expected = "x y\n1 4\n2 5\n3 6"
17291729

17301730
self.assertEqual(df_s, expected)
17311731

@@ -3106,13 +3106,13 @@ def test_to_string_float_na_spacing(self):
31063106
self.assertEqual(result, expected)
31073107

31083108
def test_to_string_without_index(self):
3109-
#GH 11729 Test index=False option
3110-
s= Series([1, 2, 3, 4])
3109+
# GH 11729 Test index=False option
3110+
s = Series([1, 2, 3, 4])
31113111
result = s.to_string(index=False)
3112-
expected = (u(' 1\n') +
3113-
' 2\n' +
3114-
' 3\n' +
3115-
' 4')
3112+
expected = (u('1\n') +
3113+
'2\n' +
3114+
'3\n' +
3115+
'4')
31163116
self.assertEqual(result, expected)
31173117

31183118
def test_unicode_name_in_footer(self):

0 commit comments

Comments
 (0)