Skip to content

to_html(col_space=<int>) does not set min-width style correctly. #25941

Closed
@simonjayhawkins

Description

@simonjayhawkins

Code Sample, a copy-pastable example if possible

import pandas as pd
import pandas.util.testing as tm
df = pd.DataFrame(index = tm.makeMultiIndex())
print(df.to_html(col_space=100))
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th style="min-width: 100;"></th>
      <th style="min-width: 100;"></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th rowspan="2" valign="top"style="min-width: 100;">foo</th>
      <th style="min-width: 100;">1</th>
    </tr>
    <tr>
      <th style="min-width: 100;">2</th>
    </tr>
    <tr>
      <th rowspan="2" valign="top"style="min-width: 100;">bar</th>
      <th style="min-width: 100;">1</th>
    </tr>
    <tr>
      <th style="min-width: 100;">2</th>
    </tr>
  </tbody>
</table>

Problem description

The col_space parameter expects an int..

col_space : int, optional
The minimum width of each column.

it should be either px of % for HTML but this raises an error if you try.

print(df.to_html(col_space='100px'))

gives TypeError: '>' not supported between instances of 'str' and 'int'

The <th style="min-width: 100;"> is also applied needlessly to the <th> tags within <tbody>

Expected Output

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th style="min-width: 100px;"></th>
      <th style="min-width: 100px;"></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th rowspan="2" valign="top">foo</th>
      <th>1</th>
    </tr>
    <tr>
      <th>2</th>
    </tr>
    <tr>
      <th rowspan="2" valign="top">bar</th>
      <th>1</th>
    </tr>
    <tr>
      <th>2</th>
    </tr>
  </tbody>
</table>

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO HTMLread_html, to_html, Styler.apply, Styler.applymap

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions