Skip to content

BUG: Parameter col_space of to_html method not working with multi-level columns #54015

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

Merged
merged 17 commits into from
Jul 18, 2023
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/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ I/O
- Bug in :func:`read_html`, tail texts were removed together with elements containing ``display:none`` style (:issue:`51629`)
- Bug in :func:`read_sql` when reading multiple timezone aware columns with the same column name (:issue:`44421`)
- Bug in :func:`read_xml` stripping whitespace in string data (:issue:`53811`)
- Bug in :meth:`DataFrame.to_html` where ``colspace`` was incorrectly applied in case of multi index columns (:issue:`53885`)
- Bug when writing and reading empty Stata dta files where dtype information was lost (:issue:`46240`)
- Bug where ``bz2`` was treated as a hard requirement (:issue:`53857`)

Expand Down
14 changes: 10 additions & 4 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ def __init__(
self.table_id = table_id
self.render_links = render_links

self.col_space = {
column: f"{value}px" if isinstance(value, int) else value
for column, value in self.fmt.col_space.items()
}
self.col_space = {}
is_multi_index = isinstance(self.columns, MultiIndex)
for column, value in self.fmt.col_space.items():
col_space_value = f"{value}px" if isinstance(value, int) else value
self.col_space[column] = col_space_value
# GH 53885: Handling case where column is index
# Flatten the data in the multi index and add in the map
if is_multi_index and isinstance(column, tuple):
for column_index in column:
self.col_space[str(column_index)] = col_space_value

def to_string(self) -> str:
lines = self.render()
Expand Down
56 changes: 56 additions & 0 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,3 +896,59 @@ def test_to_html_float_format_object_col(datapath):
result = df.to_html(float_format=lambda x: f"{x:,.0f}")
expected = expected_html(datapath, "gh40024_expected_output")
assert result == expected


def test_to_html_multiindex_col_with_colspace():
# GH#53885
df = DataFrame([[1, 2]])
df.columns = MultiIndex.from_tuples([(1, 1), (2, 1)])
result = df.to_html(col_space=100)
expected = (
'<table border="1" class="dataframe">\n'
" <thead>\n"
" <tr>\n"
' <th style="min-width: 100px;"></th>\n'
' <th style="min-width: 100px;">1</th>\n'
' <th style="min-width: 100px;">2</th>\n'
" </tr>\n"
" <tr>\n"
' <th style="min-width: 100px;"></th>\n'
' <th style="min-width: 100px;">1</th>\n'
' <th style="min-width: 100px;">1</th>\n'
" </tr>\n"
" </thead>\n"
" <tbody>\n"
" <tr>\n"
" <th>0</th>\n"
" <td>1</td>\n"
" <td>2</td>\n"
" </tr>\n"
" </tbody>\n"
"</table>"
)
assert result == expected


def test_to_html_tuple_col_with_colspace():
# GH#53885
df = DataFrame({("a", "b"): [1], "b": [2]})
result = df.to_html(col_space=100)
expected = (
'<table border="1" class="dataframe">\n'
" <thead>\n"
' <tr style="text-align: right;">\n'
' <th style="min-width: 100px;"></th>\n'
' <th style="min-width: 100px;">(a, b)</th>\n'
' <th style="min-width: 100px;">b</th>\n'
" </tr>\n"
" </thead>\n"
" <tbody>\n"
" <tr>\n"
" <th>0</th>\n"
" <td>1</td>\n"
" <td>2</td>\n"
" </tr>\n"
" </tbody>\n"
"</table>"
)
assert result == expected