Skip to content

TST: Fix doctest in read_stata #42670

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 · 1 comment · Fixed by #42701
Closed

TST: Fix doctest in read_stata #42670

datapythonista opened this issue Jul 22, 2021 · 1 comment · Fixed by #42701
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.stata.read_stata ____________________________________________________________________
2044 Categorical variables read through an iterator may not have the same
2045 categories and dtype. This occurs when  a variable stored in a DTA
2046 file is associated to an incomplete set of value labels that only
2047 label a strict subset of the values.
2048 
2049 Examples
2050 --------
2051 Read a Stata dta file:
2052 
2053 >>> df = pd.read_stata('filename.dta')
UNEXPECTED EXCEPTION: FileNotFoundError(2, 'No such file or directory')
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.stata.read_stata[0]>", line 1, in <module>
  File "/home/mgarcia/src/pandas/pandas/io/stata.py", line 1946, in read_stata
    reader = StataReader(
  File "/home/mgarcia/src/pandas/pandas/io/stata.py", line 1091, in __init__
    with get_handle(
  File "/home/mgarcia/src/pandas/pandas/io/common.py", line 710, in get_handle
    handle = open(handle, ioargs.mode)
FileNotFoundError: [Errno 2] No such file or directory: 'filename.dta'
/home/mgarcia/src/pandas/pandas/io/stata.py:2053: 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 24, 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 26, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 26, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 26, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 26, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 26, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
KrishnaSai2020 pushed a commit to KrishnaSai2020/pandas that referenced this issue Jul 27, 2021
bashtage pushed a commit that referenced this issue Jul 31, 2021
* doctest fix for #42670
* added read_stata docstring into the doctests

Co-authored-by: KrishnaSai2020 <[email protected]>
feefladder pushed a commit to feefladder/pandas that referenced this issue Sep 7, 2021
* doctest fix for pandas-dev#42670
* added read_stata docstring into the doctests

Co-authored-by: KrishnaSai2020 <[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.

2 participants