Skip to content
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

BUG: Fix to_string output when using header keyword arg (#16718) #25602

Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ I/O
- Bug in :func:`read_json` for ``orient='table'`` and float index, as it infers index dtype by default, which is not applicable because index dtype is already defined in the JSON schema (:issue:`25433`)
- Bug in :func:`read_json` for ``orient='table'`` and string of float column names, as it makes a column name type conversion to Timestamp, which is not applicable because column names are already defined in the JSON schema (:issue:`25435`)
- :meth:`DataFrame.to_html` now raises ``TypeError`` when using an invalid type for the ``classes`` parameter instead of ``AsseertionError`` (:issue:`25608`)
- Bug in :meth:`DataFrame.to_string` and :meth:`DataFrame.to_latex` that would lead to incorrect output when the ``header`` keyword is used (:issue:`16718`)
-
-

Expand Down
9 changes: 4 additions & 5 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@ def _to_str_columns(self):
else:
str_columns = self._get_formatted_column_labels(frame)

if self.show_row_idx_names:
for x in str_columns:
x.append('')

stringified = []
for i, c in enumerate(frame):
cheader = str_columns[i]
Expand Down Expand Up @@ -770,11 +774,6 @@ def space_format(x, y):
need_leadsp[x] else x]
for i, (col, x) in enumerate(zip(columns,
fmt_columns))]

if self.show_row_idx_names:
for x in str_columns:
x.append('')

# self.str_columns = str_columns
return str_columns

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,14 @@ def test_to_string_header(self):
exp = '0 0\n ..\n9 9'
assert res == exp

def test_to_string_multindex_header(self):
# GH 16718
df = (pd.DataFrame({'a': [0], 'b': [1], 'c': [2], 'd': [3]})
.set_index(['a', 'b']))
res = df.to_string(header=['r1', 'r2'])
exp = ' r1 r2\na b \n0 1 2 3'
assert res == exp


def _three_digit_exp():
return '{x:.4g}'.format(x=1.7e8) == '1.7e+008'
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,3 +735,19 @@ def test_to_latex_float_format_no_fixed_width(self):
\end{tabular}
"""
assert df.to_latex(float_format='%.0f') == expected

def test_to_latex_multindex_header(self):
# GH 16718
df = (pd.DataFrame({'a': [0], 'b': [1], 'c': [2], 'd': [3]})
.set_index(['a', 'b']))
observed = df.to_latex(header=['r1', 'r2'])
expected = r"""\begin{tabular}{llrr}
\toprule
& & r1 & r2 \\
a & b & & \\
\midrule
0 & 1 & 2 & 3 \\
\bottomrule
\end{tabular}
"""
assert observed == expected