Skip to content

TST: Fix doctest in EngFormatter #42671

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 · 4 comments · Fixed by #42705
Closed

TST: Fix doctest in EngFormatter #42671

datapythonista opened this issue Jul 22, 2021 · 4 comments · Fixed by #42705
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.format.EngFormatter.__call__ __________________________________________________________
1956 
1957         Formats a number in engineering notation, appending a letter
1958         representing the power of 1000 of the original number. Some examples:
1959 
1960         >>> format_eng(0)       # for self.accuracy = 0
UNEXPECTED EXCEPTION: NameError("name 'format_eng' is not defined")
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.format.EngFormatter.__call__[0]>", line 1, in <module>
NameError: name 'format_eng' is not defined
/home/mgarcia/src/pandas/pandas/io/formats/format.py:1960: 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
@KrishnaSai2020
Copy link
Contributor

take

KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 25, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 25, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 25, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 25, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 25, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 25, 2021
@jreback jreback added this to the 1.4 milestone Jul 28, 2021
@KrishnaSai2020
Copy link
Contributor

@hrishikeshnikam2000 I'm sorry I have already made the fix for this issue just waiting for the pr to get merged. Please see #42705

@hrishikeshnikam2000
Copy link

@KrishnaSai2020 sorry about that. I saw that later and that's why I was deleted my comment. But I was still assigned to this issue.

@KrishnaSai2020
Copy link
Contributor

Ah I see, sorry for that!

attack68 added a commit that referenced this issue Jul 31, 2021
* TST: Fix doctests for pandas.io.formats.style

* Modified: pandas/io/formats/style.py

* Added some expected results

* Skipped some tests

* TST: Add link to redirect to Table Visualization user guide

* Modified style.py

* Updated the doctest of the apply()

* Updated the doctest of the applymap()

* Updated the doctest of the set_table_styles()

* Updated the doctest of the set_properties()

* TST: Add image to pipe function result

* Modified style.py

* Updated the doctest of the pipe()

* TST: Remove unnecessary outputs

* Modified pandas/io/formats/style.py

* Updated the doctests of the set_tooltips()

* Updated the doctests of the to_latex()

* Updated the doctests of the set_td_classes()

* Updated the doctests of the set_table_attributes()

* TST: Add the output to the Styler.format doctest in to_latex()

* REG: DataFrame.agg where func returns lists and axis=1 (#42762)

* Fix typing issues for CI (#42770)

* BUG: groupby.shift returns different columns when fill_value is specified (#41858)

* PERF: extract_array earlier in DataFrame construction (#42774)

* ENH: `sparse_columns` and `sparse_index` added to `Styler.to_html`  (#41946)

* TYP: Fix typing for searchsorted (#42788)

* DOC GH42756 Update documentation for pandas.DataFrame.drop to clarify tuples. (#42789)

* CI: Fix doctests (#42790)

* REGR: nanosecond timestamp comparisons to OOB datetimes (#42796)

* COMPAT: MPL 3.4.0 (#42803)

* Delete duplicates and unused code from reshape tests (#42802)

* REGR: ValueError raised when both prefix and names are set to None (#42690)

* REGR: ValueError raised when both prefix and names are set to None

* Update readers.py

* whitespace

* Update v1.3.1.rst

* Update v1.3.2.rst

* Update readers.py

* Update readers.py

Co-authored-by: Jeff Reback <[email protected]>

* TST: Add style.py to the doctest check

* TST: fixed eng_formatter doctest for #42671 (#42705)

* TST: Revert x and y position in some doctests

* Updated the doctest of the hide_columns()

Co-authored-by: Richard Shadrach <[email protected]>
Co-authored-by: Irv Lustig <[email protected]>
Co-authored-by: Thomas Smith <[email protected]>
Co-authored-by: jbrockmendel <[email protected]>
Co-authored-by: attack68 <[email protected]>
Co-authored-by: Mike Phung <[email protected]>
Co-authored-by: Matthew Zeitlin <[email protected]>
Co-authored-by: Thomas Li <[email protected]>
Co-authored-by: Patrick Hoefler <[email protected]>
Co-authored-by: Jeff Reback <[email protected]>
Co-authored-by: Krishna Chivukula <[email protected]>
feefladder pushed a commit to feefladder/pandas that referenced this issue Sep 7, 2021
* TST: Fix doctests for pandas.io.formats.style

* Modified: pandas/io/formats/style.py

* Added some expected results

* Skipped some tests

* TST: Add link to redirect to Table Visualization user guide

* Modified style.py

* Updated the doctest of the apply()

* Updated the doctest of the applymap()

* Updated the doctest of the set_table_styles()

* Updated the doctest of the set_properties()

* TST: Add image to pipe function result

* Modified style.py

* Updated the doctest of the pipe()

* TST: Remove unnecessary outputs

* Modified pandas/io/formats/style.py

* Updated the doctests of the set_tooltips()

* Updated the doctests of the to_latex()

* Updated the doctests of the set_td_classes()

* Updated the doctests of the set_table_attributes()

* TST: Add the output to the Styler.format doctest in to_latex()

* REG: DataFrame.agg where func returns lists and axis=1 (pandas-dev#42762)

* Fix typing issues for CI (pandas-dev#42770)

* BUG: groupby.shift returns different columns when fill_value is specified (pandas-dev#41858)

* PERF: extract_array earlier in DataFrame construction (pandas-dev#42774)

* ENH: `sparse_columns` and `sparse_index` added to `Styler.to_html`  (pandas-dev#41946)

* TYP: Fix typing for searchsorted (pandas-dev#42788)

* DOC GH42756 Update documentation for pandas.DataFrame.drop to clarify tuples. (pandas-dev#42789)

* CI: Fix doctests (pandas-dev#42790)

* REGR: nanosecond timestamp comparisons to OOB datetimes (pandas-dev#42796)

* COMPAT: MPL 3.4.0 (pandas-dev#42803)

* Delete duplicates and unused code from reshape tests (pandas-dev#42802)

* REGR: ValueError raised when both prefix and names are set to None (pandas-dev#42690)

* REGR: ValueError raised when both prefix and names are set to None

* Update readers.py

* whitespace

* Update v1.3.1.rst

* Update v1.3.2.rst

* Update readers.py

* Update readers.py

Co-authored-by: Jeff Reback <[email protected]>

* TST: Add style.py to the doctest check

* TST: fixed eng_formatter doctest for pandas-dev#42671 (pandas-dev#42705)

* TST: Revert x and y position in some doctests

* Updated the doctest of the hide_columns()

Co-authored-by: Richard Shadrach <[email protected]>
Co-authored-by: Irv Lustig <[email protected]>
Co-authored-by: Thomas Smith <[email protected]>
Co-authored-by: jbrockmendel <[email protected]>
Co-authored-by: attack68 <[email protected]>
Co-authored-by: Mike Phung <[email protected]>
Co-authored-by: Matthew Zeitlin <[email protected]>
Co-authored-by: Thomas Li <[email protected]>
Co-authored-by: Patrick Hoefler <[email protected]>
Co-authored-by: Jeff Reback <[email protected]>
Co-authored-by: Krishna Chivukula <[email protected]>
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.

4 participants