Skip to content

TST: Fix doctest in Styler.to_latex #42672

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
datapythonista opened this issue Jul 22, 2021 · 2 comments · Fixed by #42718
Closed

TST: Fix doctest in Styler.to_latex #42672

datapythonista opened this issue Jul 22, 2021 · 2 comments · Fixed by #42718
Assignees
Labels
Docs good first issue Testing pandas testing functions or related to the test suite
Milestone

Comments

@datapythonista
Copy link
Member

tl;dr

Fix the next doctest error:

_____________________________________________________________ [doctest] pandas.io.formats.style.Styler.to_latex _____________________________________________________________
530         should be replaced by a LaTeX
531         ``(<command>, <options>)`` approach. Each cell will be styled individually
532         using nested LaTeX commands with their accompanied options.
533 
534         For example the following code will highlight and bold a cell in HTML-CSS:
535 
536         >>> df = pd.DataFrame([[1,2], [3,4]])
537         >>> s = df.style.highlight_max(axis=None,
538         ...                            props='background-color:red; font-weight:bold;')
539         >>> s.render()
Expected nothing
Got:
    '<style type="text/css">\n#T_d86c0_row1_col1 {\n  background-color: red;\n  font-weight: bold;\n}\n</style>\n<table id="T_d86c0_">\n  <thead>\n    <tr>\n      <th class="blank level0" >&nbsp;</th>\n      <th class="col_heading level0 col0" >0</th>\n      <th class="col_heading level0 col1" >1</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th id="T_d86c0_level0_row0" class="row_heading level0 row0" >0</th>\n      <td id="T_d86c0_row0_col0" class="data row0 col0" >1</td>\n      <td id="T_d86c0_row0_col1" class="data row0 col1" >2</td>\n    </tr>\n    <tr>\n      <th id="T_d86c0_level0_row1" class="row_heading level0 row1" >1</th>\n      <td id="T_d86c0_row1_col0" class="data row1 col0" >3</td>\n      <td id="T_d86c0_row1_col1" class="data row1 col1" >4</td>\n    </tr>\n  </tbody>\n</table>\n'

/home/mgarcia/src/pandas/pandas/io/formats/style.py:539: DocTestFailure

Detailed instructions

Python allows to have example code in the documentation, like in:

def add(num1, num2):
    """
    Computes the sum of the two numbers.

    Examples
    --------
    >>> add(2, 2)
    4
    """
    return num1 + num2

In pandas, we use this to document most elements. And there are tools, like pytest,
that can run the examples, and make sure everything is correct.

For historical reasons, we have many examples where the code fails to run, or the
actual output is different from the expected output. For example, check the next
incorrect examples:

def add(num1, num2):
    """
    Computes the sum of the two numbers.

    Examples
    --------
    >>> add(2, 2)
    5

    >>> add(2, 2
    4

    >>> add(2, number)
    4

    ...
    """
    return num1 + num2

All them will fail for different reasons. To test the docstring of an object,
the next command can be run:

python -m pytest --doctest-modules pandas/core/frame.py::pandas.core.frame.DataFrame.info

Where pandas/core/frame.py is the file where the docstring is defined, and
pandas.core.frame.DataFrame.info is the object. A whole file can also be tested
by removing the :: and the object from the command above.

In general, the errors in the examples can be fixed with things like:

  • Fixing a typo (a missing comma, an mispelled variable name...)
  • Adding an object that hasn't been defined (like, if df is used, but
    no sample dataset df has been first defined)
  • Fixing the expected output, when it's wrong
  • In exceptional cases, examples shouldn't run, since they can't work.
    For example, a function that connects to a private webservice. In
    such cases, we can add # doctest: +SKIP at the end of the lines
    that should not run

To be able to properly fix an example for the first time, the next steps
are needed:

  • Install a pandas development environment in your computer. There are
    simplified instructions in this page,
    and more detailed information in pandas official contributing page.
  • Run the doctests for the object of interest (the one in this issue),
    and make sure the examples are still broken in the master branch of
    pandas
  • Fix the file locally, and run the doctests again, to make sure the
    fix is working as expected
  • Optionally have a look and make sure that the code in the examples
    follow PEP-8, and fix the style if it doesn't
  • Commit your changes, push your branch to a fork, and open a pull
    request. Make sure you edit the line Closes #XXXX with the issue
    number you are addressing, so the issue is automatically closed,
    when the pull request is merged
  • Make sure the continuous integration of your pull request finishes
    in green. If it doesn't, check if the problem is in your changes
    (sometimes things break in master for technical problems, and in
    that case you just need to wait for a core developer to fix the
    problem)
  • Address any comment from the reviewers (just make changes locally,
    commit, and push to your branch, no need to open new pull requests)
@datapythonista datapythonista added Testing pandas testing functions or related to the test suite Docs good first issue labels Jul 22, 2021
@attack68
Copy link
Contributor

i would recommend the fix for this is to skip the test. The point of styler examples is to display the rendered output or as a comparison, not the textual output used by the render (e.g. browser or latex reader). Adding textual output will pollute the docstring.

@willie3838
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs good first issue Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants