Skip to content

Commit 2e20e60

Browse files
committed
issue-11833 solved , docs updated as well
modifies test_format squashed
1 parent cb97503 commit 2e20e60

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

doc/source/whatsnew/v0.18.0.txt

+20
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ 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+
166+
Previous Behaviour for the DataFrame Objects
167+
168+
.. code-block:: python
169+
170+
In [3]: df = pd.DataFrame({'a':range(5),'b':range(5)})
171+
In [3]: df.to_string(index=False)
172+
Out [4]: u' a b\n 0 0\n 1 1\n 2 2\n 3 3\n 4 4'
173+
174+
175+
New Behaviour for the DataFrame Objects
176+
177+
.. ipython:: python
178+
179+
df=pd.DataFrame({'a':range(5),'b':range(5)})
180+
df.to_string(index=False)
181+
182+
Similar behaviour applies for the Series objects as well.
183+
164184
Backwards incompatible API changes
165185
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166186

pandas/core/format.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ 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 ','\n').strip()
249249

250250
if self.header and have_header:
251251
result = fmt_index[0] + '\n' + result
@@ -577,7 +577,8 @@ def to_string(self):
577577
self._chk_truncate()
578578
strcols = self._to_str_columns()
579579
text = self.adj.adjoin(1, *strcols)
580-
580+
if not self.index:
581+
text=text.replace('\n ','\n').strip()
581582
self.buf.writelines(text)
582583

583584
if self.should_show_dimensions:

pandas/tests/test_format.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -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

@@ -3109,10 +3109,10 @@ def test_to_string_without_index(self):
31093109
#GH 11729 Test index=False option
31103110
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)