Skip to content

BUG: Fixes dataFrame to_string(header=False) #49738

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

Merged
merged 6 commits into from
Nov 17, 2022
Merged
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,9 @@ I/O
- Improved error message in :func:`read_excel` by including the offending sheet name when an exception is raised while reading a file (:issue:`48706`)
- Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`)
- Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`)
- 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`)
- Fixed memory leak which stemmed from the initialization of the internal JSON module (:issue:`49222`)
-

Period
^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def adjoin(space: int, *lists: list[str], **kwargs) -> str:
maxLen = max(map(len, lists))
for i, lst in enumerate(lists):
nl = justfunc(lst, lengths[i], mode="left")
nl.extend([" " * lengths[i]] * (maxLen - len(lst)))
nl = ([" " * lengths[i]] * (maxLen - len(lst))) + nl
newLists.append(nl)
toJoin = zip(*newLists)
for lines in toJoin:
Expand Down
22 changes: 18 additions & 4 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,25 +1411,25 @@ def test_to_string_no_index(self):
assert df_s == expected

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

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

assert df_s == expected

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

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

assert df_s == expected

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

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

assert df_s == expected

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

def test_to_string_header_false(self):
# GH 49230
df = DataFrame([1, 2])
df.index.name = "a"
s = df.to_string(header=False)
expected = "a \n0 1\n1 2"
assert s == expected

df = DataFrame([[1, 2], [3, 4]])
df.index.name = "a"
s = df.to_string(header=False)
expected = "a \n0 1 2\n1 3 4"
assert s == expected

def test_show_dimensions(self):
df = DataFrame(123, index=range(10, 15), columns=range(30))

Expand Down