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 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
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
2 changes: 1 addition & 1 deletion pandas/tests/arrays/categorical/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_empty_print(self):
def test_print_none_width(self):
# GH10087
a = Series(Categorical([1, 2, 3, 4]))
exp = u("0 1\n1 2\n2 3\n3 4\n" +
exp = u("0 1\n1 2\n2 3\n3 4\n" +
"dtype: category\nCategories (4, int64): [1, 2, 3, 4]")

with option_context("display.width", None):
Expand Down
Loading