Skip to content

CLN: Consistency of arguments in to_html and to_string #23612

Closed
@datapythonista

Description

@datapythonista

The methods to_html and to_string share most of the parameters, together with DataFrameFormatter, but there are some differences in the order, and in some parameters that probably should be the same.

Those are the common parameters:

buf=None,
columns=None,
col_space=None,
header=True,
index=True,
na_rep='NaN',
formatters=None,
float_format=None,
sparsify=None,
index_names=True,
justify=None,
max_rows=None,
max_cols=None,
show_dimensions=False

But after formatters they are in different order, which adds complexity (for example in reusing docstrings, or in switching from one function to another when using positional arguments).

Then, those are the parameters that are not shared among the 3:

DataFrameFormatter
--
frame,  # (positional, at the beginning)
line_width=None,
decimal='.',
table_id=None,

to_string
--
line_width=None,

to_html
--
decimal='.',
table_id=None
bold_rows=True,
classes=None,
escape=True,
notebook=False,
border=None,

While that looks mostly ok, it feels like probably to_string should also have a decimal argument (and not sure what DataFrameFormatter should have.

What I propose is:

  • Reorder the arguments so the common ones are located first, and in the same order across all methods.
  • Add decimal to to_string, and check if any other addition (or deletion) makes sense.
  • Add a note in whatsnew, but don't do any deprecation (which would make things quite tricky in our code). Hopefully no users are using positional arguments for the last arguments in a method with around 15.
  • Make sure the docstrings have the parameters in the right order.
  • Looks like in pandas/io/formats/format.py there is a related docstring_to_string variable that is not being used, so we should remove it.

@jreback happy to make these changes?

CC @thoo

Metadata

Metadata

Assignees

No one assigned

    Labels

    CleanIO HTMLread_html, to_html, Styler.apply, Styler.applymapNeeds DiscussionRequires discussion from core team before further actiongood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions