Skip to content

Leading space in to_string(index=False), #11833 #11942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,33 @@ In addition, ``.round()`` will be available thru the ``.dt`` accessor of ``Serie

.. _whatsnew_0180.api_breaking:

- the leading whitespaces have been removed from the output of ``to_string(index=False)`` method (:issue:'11833')

Previous Behaviour for the DataFrame Objects

.. code-block:: python

In [3]: df = pd.DataFrame({'a':range(5),'b':range(5)})
In [3]: df.to_string(index=False)
Out [4]: u' a b\n 0 0\n 1 1\n 2 2\n 3 3\n 4 4'


New Behaviour for the DataFrame Objects

.. ipython:: python

df=pd.DataFrame({'a':range(5),'b':range(5)})
df.to_string(index=False)

Similar behaviour applies for the Series objects as well.

Backwards incompatible API changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- The parameter ``out`` has been removed from the ``Series.round()`` method. (:issue:`11763`)
- ``DataFrame.round()`` leaves non-numeric columns unchanged in its return, rather than raises. (:issue:`11885`)
- ``DataFrame.head(0)`` and ``DataFrame.tail(0)`` return empty frames, rather than ``self``. (:issue:`11937`)
- ``Series.head(0)`` and ``Series.tail(0)`` return empty series, rather than ``self``. (:issue:`11937`)

NaT and Timedelta operations
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def to_string(self):
if self.index:
result = self.adj.adjoin(3, *[fmt_index[1:], fmt_values])
else:
result = self.adj.adjoin(3, fmt_values)
result = self.adj.adjoin(3, fmt_values).replace('\n ','\n').strip()

if self.header and have_header:
result = fmt_index[0] + '\n' + result
Expand Down Expand Up @@ -577,7 +577,8 @@ def to_string(self):
self._chk_truncate()
strcols = self._to_str_columns()
text = self.adj.adjoin(1, *strcols)

if not self.index:
text=text.replace('\n ','\n').strip()
self.buf.writelines(text)

if self.should_show_dimensions:
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2136,18 +2136,14 @@ def head(self, n=5):
"""
Returns first n rows
"""
l = len(self)
if l == 0 or n==0:
return self
return self.iloc[:n]

def tail(self, n=5):
"""
Returns last n rows
"""
l = len(self)
if l == 0 or n == 0:
return self
if n == 0:
return self.iloc[0:0]
return self.iloc[-n:]


Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@ def test_to_string_no_index(self):
'y': [4, 5, 6]})

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

self.assertEqual(df_s, expected)

Expand Down Expand Up @@ -3109,10 +3109,10 @@ def test_to_string_without_index(self):
#GH 11729 Test index=False option
s= Series([1, 2, 3, 4])
result = s.to_string(index=False)
expected = (u(' 1\n') +
' 2\n' +
' 3\n' +
' 4')
expected = (u('1\n') +
'2\n' +
'3\n' +
'4')
self.assertEqual(result, expected)

def test_unicode_name_in_footer(self):
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5457,8 +5457,10 @@ def test_repr_column_name_unicode_truncation_bug(self):
def test_head_tail(self):
assert_frame_equal(self.frame.head(), self.frame[:5])
assert_frame_equal(self.frame.tail(), self.frame[-5:])
assert_frame_equal(self.frame.head(0), self.frame)
assert_frame_equal(self.frame.tail(0), self.frame)

assert_frame_equal(self.frame.head(0), self.frame[0:0])
assert_frame_equal(self.frame.tail(0), self.frame[0:0])

assert_frame_equal(self.frame.head(-1), self.frame[:-1])
assert_frame_equal(self.frame.tail(-1), self.frame[1:])
assert_frame_equal(self.frame.head(1), self.frame[:1])
Expand All @@ -5468,8 +5470,8 @@ def test_head_tail(self):
df.index = np.arange(len(self.frame)) + 0.1
assert_frame_equal(df.head(), df.iloc[:5])
assert_frame_equal(df.tail(), df.iloc[-5:])
assert_frame_equal(df.head(0), df)
assert_frame_equal(df.tail(0), df)
assert_frame_equal(df.head(0), df[0:0])
assert_frame_equal(df.tail(0), df[0:0])
assert_frame_equal(df.head(-1), df.iloc[:-1])
assert_frame_equal(df.tail(-1), df.iloc[1:])
#test empty dataframe
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ def test_head_tail(self):
self._compare(o.tail(), o.iloc[-5:])

# 0-len
self._compare(o.head(0), o.iloc[:])
self._compare(o.tail(0), o.iloc[0:])
self._compare(o.head(0), o.iloc[0:0])
self._compare(o.tail(0), o.iloc[0:0])

# bounded
self._compare(o.head(len(o)+1), o)
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -7642,8 +7642,9 @@ def test_sortlevel(self):

def test_head_tail(self):
assert_series_equal(self.series.head(), self.series[:5])
assert_series_equal(self.series.head(0), self.series[0:0])
assert_series_equal(self.series.tail(), self.series[-5:])

assert_series_equal(self.series.tail(0), self.series[0:0])
def test_isin(self):
s = Series(['A', 'B', 'C', 'a', 'B', 'B', 'A', 'C'])

Expand Down