Skip to content

ENH: Support for specifying col names for col_space #28929

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

Closed
Closed
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
2 changes: 2 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,7 @@ def to_html(
buf=None,
columns=None,
col_space=None,
col_space_cols=None,
header=True,
index=True,
na_rep="NaN",
Expand Down Expand Up @@ -2244,6 +2245,7 @@ def to_html(
self,
columns=columns,
col_space=col_space,
col_space_cols=col_space_cols,
na_rep=na_rep,
formatters=formatters,
float_format=float_format,
Expand Down
2 changes: 2 additions & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ def __init__(
frame: "DataFrame",
columns: Optional[Sequence[str]] = None,
col_space: Optional[Union[str, int]] = None,
col_space_cols: Optional[Sequence[str]] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than adding an additional argument, can you make the behavior similar to the formatters argument. (and reuse code would be great). i.e. also accept a list or a dict. (or Sequence or Mapping)

if a list, the number of elements in the list should be the same as the number of columns.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @simonjayhawkins , Sorry I didn't get your comment. The following statement seems confusing to me.

rather than adding an additional argument, can you make the behavior similar to the formatters argument

Did you mean to NOT add the additional argument col_space_cols and use existing formatters argument for this change or add an argument similar to formatters, which accepts a list or a dict.

Also,

if a list, the number of elements in the list should be the same as the number of columns

Not sure if I understand this, if we want to change width of only a single column, we can pass only that column's name in the list, right ?

Please let me know if I missing something here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @simonjayhawkins , did you get a chance to look at the my concern ? I think I need to make the new argument similar to the formatters argument, but would still wait for your confirmation/opinion. Thanks!

header: Union[bool, Sequence[str]] = True,
index: bool = True,
na_rep: str = "NaN",
Expand Down Expand Up @@ -575,6 +576,7 @@ def __init__(
self.na_rep = na_rep
self.decimal = decimal
self.col_space = col_space
self.col_space_cols = col_space_cols
self.header = header
self.index = index
self.line_width = line_width
Expand Down
7 changes: 4 additions & 3 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def write_th(
self, s: Any, header: bool = False, indent: int = 0, tags: Optional[str] = None
) -> None:
"""
Method for writting a formatted <th> cell.
Method for writing a formatted <th> cell.

If col_space is set on the formatter then that is used for
the value of min-width.
Expand All @@ -122,8 +122,9 @@ def write_th(
A written <th> cell.
"""
if header and self.fmt.col_space is not None:
tags = tags or ""
tags += 'style="min-width: {colspace};"'.format(colspace=self.fmt.col_space)
if self.fmt.col_space_cols is None or s in self.fmt.col_space_cols:
tags = tags or ""
tags += 'style="min-width: {colspace};"'.format(colspace=self.fmt.col_space)

self._write_cell(s, kind="th", indent=indent, tags=tags)

Expand Down