Skip to content

Commit 262bc81

Browse files
andrewchen1216MarcoGorelli
authored and
MarcoGorelli
committed
BUG: Fixes dataFrame to_string(header=False) (pandas-dev#49738)
* BUG: GH49230 to_string(header=False) fix * TST:Updated and Added Test Cases for GH49230 * DOC: Added release notes documentation * DOC:Updated release notes
1 parent a8e6c08 commit 262bc81

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

doc/source/whatsnew/v2.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,9 @@ I/O
695695
- Improved error message in :func:`read_excel` by including the offending sheet name when an exception is raised while reading a file (:issue:`48706`)
696696
- Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`)
697697
- Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`)
698+
- Bug in :func:`DataFrame.to_string` with ``header=False`` that printed the index name on the same line as the first row of the data (:issue:`49230`)
698699
- Fixed memory leak which stemmed from the initialization of the internal JSON module (:issue:`49222`)
700+
-
699701

700702
Period
701703
^^^^^^

pandas/io/formats/printing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def adjoin(space: int, *lists: list[str], **kwargs) -> str:
5151
maxLen = max(map(len, lists))
5252
for i, lst in enumerate(lists):
5353
nl = justfunc(lst, lengths[i], mode="left")
54-
nl.extend([" " * lengths[i]] * (maxLen - len(lst)))
54+
nl = ([" " * lengths[i]] * (maxLen - len(lst))) + nl
5555
newLists.append(nl)
5656
toJoin = zip(*newLists)
5757
for lines in toJoin:

pandas/tests/io/formats/test_format.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -1411,25 +1411,25 @@ def test_to_string_no_index(self):
14111411
assert df_s == expected
14121412

14131413
def test_to_string_line_width_no_index(self):
1414-
# GH 13998, GH 22505
1414+
# GH 13998, GH 22505, # GH 49230
14151415
df = DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})
14161416

14171417
df_s = df.to_string(line_width=1, index=False)
1418-
expected = " x \\\n 1 \n 2 \n 3 \n\n y \n 4 \n 5 \n 6 "
1418+
expected = " x \n 1 \\\n 2 \n 3 \n\n y \n 4 \n 5 \n 6 "
14191419

14201420
assert df_s == expected
14211421

14221422
df = DataFrame({"x": [11, 22, 33], "y": [4, 5, 6]})
14231423

14241424
df_s = df.to_string(line_width=1, index=False)
1425-
expected = " x \\\n11 \n22 \n33 \n\n y \n 4 \n 5 \n 6 "
1425+
expected = " x \n11 \\\n22 \n33 \n\n y \n 4 \n 5 \n 6 "
14261426

14271427
assert df_s == expected
14281428

14291429
df = DataFrame({"x": [11, 22, -33], "y": [4, 5, -6]})
14301430

14311431
df_s = df.to_string(line_width=1, index=False)
1432-
expected = " x \\\n 11 \n 22 \n-33 \n\n y \n 4 \n 5 \n-6 "
1432+
expected = " x \n 11 \\\n 22 \n-33 \n\n y \n 4 \n 5 \n-6 "
14331433

14341434
assert df_s == expected
14351435

@@ -1683,6 +1683,20 @@ def test_to_string_line_width(self):
16831683
s = df.to_string(line_width=80)
16841684
assert max(len(line) for line in s.split("\n")) == 80
16851685

1686+
def test_to_string_header_false(self):
1687+
# GH 49230
1688+
df = DataFrame([1, 2])
1689+
df.index.name = "a"
1690+
s = df.to_string(header=False)
1691+
expected = "a \n0 1\n1 2"
1692+
assert s == expected
1693+
1694+
df = DataFrame([[1, 2], [3, 4]])
1695+
df.index.name = "a"
1696+
s = df.to_string(header=False)
1697+
expected = "a \n0 1 2\n1 3 4"
1698+
assert s == expected
1699+
16861700
def test_show_dimensions(self):
16871701
df = DataFrame(123, index=range(10, 15), columns=range(30))
16881702

0 commit comments

Comments
 (0)