Skip to content

Commit 289e081

Browse files
authored
BUG: Parameter col_space of to_html method not working with multi-level columns (#54015)
* 53885: BUG: Parameter col_space of to_html method not working with multi-level columns * 53885: added testcases * 53885: added testcases * 53885: testcase review comment addressing * 53885: testcase review comment addressing * 53885: testcase review comment addressing * 53885: handling case where column is a tuple * 53885: using isinstance MultiIndex * typing error addressing * typing error addressing * updated whatsnew * updated whatsnew * updated whatsnew
1 parent 44c4168 commit 289e081

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ I/O
528528
- Bug in :func:`read_html`, tail texts were removed together with elements containing ``display:none`` style (:issue:`51629`)
529529
- Bug in :func:`read_sql` when reading multiple timezone aware columns with the same column name (:issue:`44421`)
530530
- Bug in :func:`read_xml` stripping whitespace in string data (:issue:`53811`)
531+
- Bug in :meth:`DataFrame.to_html` where ``colspace`` was incorrectly applied in case of multi index columns (:issue:`53885`)
531532
- Bug when writing and reading empty Stata dta files where dtype information was lost (:issue:`46240`)
532533
- Bug where ``bz2`` was treated as a hard requirement (:issue:`53857`)
533534

pandas/io/formats/html.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,16 @@ def __init__(
7373
self.table_id = table_id
7474
self.render_links = render_links
7575

76-
self.col_space = {
77-
column: f"{value}px" if isinstance(value, int) else value
78-
for column, value in self.fmt.col_space.items()
79-
}
76+
self.col_space = {}
77+
is_multi_index = isinstance(self.columns, MultiIndex)
78+
for column, value in self.fmt.col_space.items():
79+
col_space_value = f"{value}px" if isinstance(value, int) else value
80+
self.col_space[column] = col_space_value
81+
# GH 53885: Handling case where column is index
82+
# Flatten the data in the multi index and add in the map
83+
if is_multi_index and isinstance(column, tuple):
84+
for column_index in column:
85+
self.col_space[str(column_index)] = col_space_value
8086

8187
def to_string(self) -> str:
8288
lines = self.render()

pandas/tests/io/formats/test_to_html.py

+56
Original file line numberDiff line numberDiff line change
@@ -896,3 +896,59 @@ def test_to_html_float_format_object_col(datapath):
896896
result = df.to_html(float_format=lambda x: f"{x:,.0f}")
897897
expected = expected_html(datapath, "gh40024_expected_output")
898898
assert result == expected
899+
900+
901+
def test_to_html_multiindex_col_with_colspace():
902+
# GH#53885
903+
df = DataFrame([[1, 2]])
904+
df.columns = MultiIndex.from_tuples([(1, 1), (2, 1)])
905+
result = df.to_html(col_space=100)
906+
expected = (
907+
'<table border="1" class="dataframe">\n'
908+
" <thead>\n"
909+
" <tr>\n"
910+
' <th style="min-width: 100px;"></th>\n'
911+
' <th style="min-width: 100px;">1</th>\n'
912+
' <th style="min-width: 100px;">2</th>\n'
913+
" </tr>\n"
914+
" <tr>\n"
915+
' <th style="min-width: 100px;"></th>\n'
916+
' <th style="min-width: 100px;">1</th>\n'
917+
' <th style="min-width: 100px;">1</th>\n'
918+
" </tr>\n"
919+
" </thead>\n"
920+
" <tbody>\n"
921+
" <tr>\n"
922+
" <th>0</th>\n"
923+
" <td>1</td>\n"
924+
" <td>2</td>\n"
925+
" </tr>\n"
926+
" </tbody>\n"
927+
"</table>"
928+
)
929+
assert result == expected
930+
931+
932+
def test_to_html_tuple_col_with_colspace():
933+
# GH#53885
934+
df = DataFrame({("a", "b"): [1], "b": [2]})
935+
result = df.to_html(col_space=100)
936+
expected = (
937+
'<table border="1" class="dataframe">\n'
938+
" <thead>\n"
939+
' <tr style="text-align: right;">\n'
940+
' <th style="min-width: 100px;"></th>\n'
941+
' <th style="min-width: 100px;">(a, b)</th>\n'
942+
' <th style="min-width: 100px;">b</th>\n'
943+
" </tr>\n"
944+
" </thead>\n"
945+
" <tbody>\n"
946+
" <tr>\n"
947+
" <th>0</th>\n"
948+
" <td>1</td>\n"
949+
" <td>2</td>\n"
950+
" </tr>\n"
951+
" </tbody>\n"
952+
"</table>"
953+
)
954+
assert result == expected

0 commit comments

Comments
 (0)