Skip to content

Commit 5c341dc

Browse files
tomneepTomAugspurger
authored andcommitted
BUG: Fix to_string output when using header (#16718) (#25602)
Also affects to_latex midrule position Tests added for both to_string and to_latex Whatsnew added for v0.25.0
1 parent 26d991f commit 5c341dc

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ I/O
215215
- 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`)
216216
- 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`)
217217
- :meth:`DataFrame.to_html` now raises ``TypeError`` when using an invalid type for the ``classes`` parameter instead of ``AsseertionError`` (:issue:`25608`)
218+
- 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`)
218219
-
219220
-
220221

pandas/io/formats/format.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,10 @@ def _to_str_columns(self):
528528
else:
529529
str_columns = self._get_formatted_column_labels(frame)
530530

531+
if self.show_row_idx_names:
532+
for x in str_columns:
533+
x.append('')
534+
531535
stringified = []
532536
for i, c in enumerate(frame):
533537
cheader = str_columns[i]
@@ -770,11 +774,6 @@ def space_format(x, y):
770774
need_leadsp[x] else x]
771775
for i, (col, x) in enumerate(zip(columns,
772776
fmt_columns))]
773-
774-
if self.show_row_idx_names:
775-
for x in str_columns:
776-
x.append('')
777-
778777
# self.str_columns = str_columns
779778
return str_columns
780779

pandas/tests/io/formats/test_format.py

+8
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,14 @@ def test_to_string_header(self):
23802380
exp = '0 0\n ..\n9 9'
23812381
assert res == exp
23822382

2383+
def test_to_string_multindex_header(self):
2384+
# GH 16718
2385+
df = (pd.DataFrame({'a': [0], 'b': [1], 'c': [2], 'd': [3]})
2386+
.set_index(['a', 'b']))
2387+
res = df.to_string(header=['r1', 'r2'])
2388+
exp = ' r1 r2\na b \n0 1 2 3'
2389+
assert res == exp
2390+
23832391

23842392
def _three_digit_exp():
23852393
return '{x:.4g}'.format(x=1.7e8) == '1.7e+008'

pandas/tests/io/formats/test_to_latex.py

+16
Original file line numberDiff line numberDiff line change
@@ -735,3 +735,19 @@ def test_to_latex_float_format_no_fixed_width(self):
735735
\end{tabular}
736736
"""
737737
assert df.to_latex(float_format='%.0f') == expected
738+
739+
def test_to_latex_multindex_header(self):
740+
# GH 16718
741+
df = (pd.DataFrame({'a': [0], 'b': [1], 'c': [2], 'd': [3]})
742+
.set_index(['a', 'b']))
743+
observed = df.to_latex(header=['r1', 'r2'])
744+
expected = r"""\begin{tabular}{llrr}
745+
\toprule
746+
& & r1 & r2 \\
747+
a & b & & \\
748+
\midrule
749+
0 & 1 & 2 & 3 \\
750+
\bottomrule
751+
\end{tabular}
752+
"""
753+
assert observed == expected

0 commit comments

Comments
 (0)