Skip to content

TST: Fix doctest in _parse_latex_header_span #42673

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 · 5 comments · Fixed by #42700
Closed

TST: Fix doctest in _parse_latex_header_span #42673

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

Comments

@datapythonista
Copy link
Member

tl;dr

Fix the next doctest error:

_____________________________________________________ [doctest] pandas.io.formats.style_render._parse_latex_header_span _____________________________________________________
1329     ``wrap`` is used to enclose the `display_value` in braces which is needed for
1330     column headers using an siunitx package.
1331 
1332     Requires the package {multirow}, whereas multicol support is usually built in
1333     to the {tabular} environment.
1334 
1335     Examples
1336     --------
1337     >>> cell = {'display_vale':'text', 'attributes': 'colspan="3"'}
1338     >>> _parse_latex_header_span(cell, 't', 'c')
UNEXPECTED EXCEPTION: KeyError('display_value')
Traceback (most recent call last):
  File "/home/mgarcia/miniconda3/envs/pandas-dev/lib/python3.8/doctest.py", line 1336, in __run
    exec(compile(example.source, filename, "single",
  File "<doctest pandas.io.formats.style_render._parse_latex_header_span[1]>", line 1, in <module>
  File "/home/mgarcia/src/pandas/pandas/io/formats/style_render.py", line 1347, in _parse_latex_header_span
    f"\\multicolumn{{{colspan}}}{{{multicol_align}}}"
KeyError: 'display_value'
/home/mgarcia/src/pandas/pandas/io/formats/style_render.py:1338: UnexpectedException

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
@aneesh98
Copy link
Contributor

take

@aneesh98
Copy link
Contributor

aneesh98 commented Jul 24, 2021

Is an entry in whatsnew/v1.4.0.rst -> Styler section required, regarding the doctest fix?

@datapythonista
Copy link
Member Author

Is an entry in whatsnew/v1.4.0.rst -> Styler section required, regarding the doctest fix?

I don't think it's relevant enough to be in the release notes. And we have many of those fixes, so it would be too much noise. Maybe at the end of fixing all them a note to say that all doctests are fixed could make sense, but not one for each.

@aneesh98
Copy link
Contributor

Okay. Have created a pull request.

@attack68
Copy link
Contributor

this is a private method. the documentation is not strictly necessary but included to help other devs. many pandas private methods do not have complete doc strings. the changes are not public facing so no release note needed.

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.

3 participants