Skip to content

Fix DataFrame.to_string() justification #22437

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 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
10 changes: 6 additions & 4 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,6 @@ 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 Expand Up @@ -681,6 +679,10 @@ def _join_multiline(self, *strcols):
st = 0
for i, ed in enumerate(col_bins):
row = strcols[st:ed]
if i > 0 and not self.index:
has_leading_sp = all(e[0] == ' ' for e in row[0])
if has_leading_sp:
row[0] = [e[1:] for e in row[0]]
if self.index:
row.insert(0, idx)
if nbins > 1:
Expand Down Expand Up @@ -790,7 +792,7 @@ def space_format(x, y):
dtypes = self.frame.dtypes
need_leadsp = dict(zip(fmt_columns, map(is_numeric_dtype, dtypes)))
str_columns = [[' ' + x if not self._get_formatter(i) and
need_leadsp[x] else x]
need_leadsp[x] and (self.index or i > 0) else x]
for i, (col, x) in enumerate(zip(columns,
fmt_columns))]

Expand Down Expand Up @@ -1106,7 +1108,7 @@ def _format_strings(self):
class IntArrayFormatter(GenericArrayFormatter):

def _format_strings(self):
formatter = self.formatter or (lambda x: '{x: d}'.format(x=x))
formatter = self.formatter or (lambda x: '{x:d}'.format(x=x))
fmt_values = [formatter(x) for x in self.values]
return fmt_values

Expand Down
52 changes: 47 additions & 5 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,18 +1269,60 @@ def test_to_string_specified_header(self):
df.to_string(header=['X'])

def test_to_string_no_index(self):
df = DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})

df_s = df.to_string(index=False)
expected = "x y\n1 4\n2 5\n3 6"
dfs = [

assert df_s == expected
# ints
DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]}),
DataFrame({'x': [11, 22, 33], 'y': [4, 5, 6]}),
DataFrame({'x': [11, 22, -33], 'y': [4, 5, 6]}),
DataFrame({'x': [11, 22, -33], 'y': [4, 5, -6]}),
DataFrame({'x': [11, 22, -33], 'y': [44, 55, -66]}),

# floats
DataFrame({'x': [0.1, 0.2, -0.3], 'y': [4, 5, 6]}),
DataFrame({'x': [0.1, 0.2, -0.3], 'y': [0.4, 0.5, 0.6]}),
DataFrame({'x': [0.1, 0.2, -0.3], 'y': [0.4, 0.5, -0.6]}),
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In cases like this is preferred to use pytest parametrization.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix if/when general direction of this PR is accepted.


exs = [

# ints
"x y\n1 4\n2 5\n3 6",
" x y\n11 4\n22 5\n33 6",
" x y\n 11 4\n 22 5\n-33 6",
" x y\n 11 4\n 22 5\n-33 -6",
" x y\n 11 44\n 22 55\n-33 -66",

# floats
" x y\n 0.1 4\n 0.2 5\n-0.3 6",
" x y\n 0.1 0.4\n 0.2 0.5\n-0.3 0.6",
" x y\n 0.1 0.4\n 0.2 0.5\n-0.3 -0.6",
]

for df, expected in zip(dfs, exs):
df_s = df.to_string(index=False)
assert df_s == expected

def test_to_string_line_width_no_index(self):
df = DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})

df_s = df.to_string(line_width=1, index=False)
expected = "x \\\n1 \n2 \n3 \n\ny \n4 \n5 \n6"
expected = "x \\\n1 \n2 \n3 \n\ny \n4 \n5 \n6 "

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\ny \n4 \n5 \n6 "

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 "

assert df_s == expected

Expand Down