Repository: pandas-dev/pandas

Issue:
DOC: Validate that the docstrings do not import pandas or numpy
`scripts/validate_docstrings.py` validates that the content of a docstring follows our standards. There are still some of those standards that the script does not validate, and this gives the users the wrong impression that the docstring is all right, when it's not really the case. All the missing validations are listed in #20298.

One of those standards is that we do not show the `import numpy as np` or `import pandas as pd` in the examples section, but we assume it's there. We should automatically validate that these imports do not exist in the examples.

For this issue is required:
- Change `scripts/validate_docstings.py` to give an error if the imports are found
- Add tests in `scripts/tests/test_validate_docstrings.py`
- Run the script for all docstrings (i.e. ./scripts/validate_docstings.py) and check in the resulting report how many of the docstrings in pandas have these imports. If any, remove them in the same PR.


Discussion:
@datapythonista I am a first time contributor. Can I work on this issue? Thanks.
Please do it @thoo, that's why I created the issue and labeled as "good first issue" :)
@datapythonista  Thanks!!!
@datapythonista  I don't find any import with `numpy` or `pandas`. 

Resolving patch:
diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py
--- a/scripts/validate_docstrings.py
+++ b/scripts/validate_docstrings.py
@@ -402,6 +402,11 @@ def examples_errors(self):
             error_msgs += f.getvalue()
         return error_msgs
 
+    @property
+    def examples_source_code(self):
+        lines = doctest.DocTestParser().get_examples(self.raw_doc)
+        return [line.source for line in lines]
+
 
 def validate_one(func_name):
     """
@@ -531,6 +536,13 @@ def validate_one(func_name):
         examples_errs = doc.examples_errors
         if examples_errs:
             errs.append('Examples do not pass tests')
+        examples_source_code = ''.join(doc.examples_source_code)
+        if 'import numpy' in examples_source_code:
+            errs.append("numpy does not need to be imported in the examples, "
+                        "as it's assumed to be already imported as np")
+        if 'import pandas' in examples_source_code:
+            errs.append("pandas does not need to be imported in the examples, "
+                        "as it's assumed to be already imported as pd")
 
     return {'type': doc.type,
             'docstring': doc.clean_doc,


Test patch:
diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py
--- a/scripts/tests/test_validate_docstrings.py
+++ b/scripts/tests/test_validate_docstrings.py
@@ -218,6 +218,18 @@ def mode(self, axis, numeric_only):
         """
         pass
 
+    def good_imports(self):
+        """
+        Ensure import other than numpy and pandas are fine.
+
+        Examples
+        --------
+        This example does not import pandas or import numpy.
+        >>> import time
+        >>> import datetime
+        """
+        pass
+
 
 class BadGenericDocStrings(object):
     """Everything here has a bad docstring
@@ -700,6 +712,11 @@ def test_bad_generic_functions(self, func):
                      marks=pytest.mark.xfail),
         pytest.param('BadReturns', 'no_punctuation', ('foo',),
                      marks=pytest.mark.xfail),
+        # Examples tests
+        ('BadGenericDocStrings', 'method',
+         ('numpy does not need to be imported in the examples,')),
+        ('BadGenericDocStrings', 'method',
+         ('pandas does not need to be imported in the examples,')),
         # See Also tests
         ('BadSeeAlso', 'prefix_pandas',
          ('pandas.Series.rename in `See Also` section '