From 7e6864dcef0afe8776f84f727ef1254d7639663f Mon Sep 17 00:00:00 2001 From: thoo Date: Sun, 14 Oct 2018 01:48:59 -0400 Subject: [PATCH 1/6] Remove blank lines-->fix cookbook rendering issue --- doc/source/cookbook.rst | 6 ------ 1 file changed, 6 deletions(-) diff --git a/doc/source/cookbook.rst b/doc/source/cookbook.rst index 21c8ab4128188..df0048c8a74b4 100644 --- a/doc/source/cookbook.rst +++ b/doc/source/cookbook.rst @@ -1234,25 +1234,19 @@ The `method` argument within `DataFrame.corr` can accept a callable in addition n = len(x) a = np.zeros(shape=(n, n)) b = np.zeros(shape=(n, n)) - for i in range(n): for j in range(i + 1, n): a[i, j] = abs(x[i] - x[j]) b[i, j] = abs(y[i] - y[j]) - a += a.T b += b.T - a_bar = np.vstack([np.nanmean(a, axis=0)] * n) b_bar = np.vstack([np.nanmean(b, axis=0)] * n) - A = a - a_bar - a_bar.T + np.full(shape=(n, n), fill_value=a_bar.mean()) B = b - b_bar - b_bar.T + np.full(shape=(n, n), fill_value=b_bar.mean()) - cov_ab = np.sqrt(np.nansum(A * B)) / n std_a = np.sqrt(np.sqrt(np.nansum(A**2)) / n) std_b = np.sqrt(np.sqrt(np.nansum(B**2)) / n) - return cov_ab / std_a / std_b df = pd.DataFrame(np.random.normal(size=(100, 3))) From bc5f082e48cd3870cfcceb507177f3e377da99eb Mon Sep 17 00:00:00 2001 From: thoo Date: Sun, 14 Oct 2018 10:15:39 -0400 Subject: [PATCH 2/6] Give Warning on prefix pandas --- scripts/tests/test_validate_docstrings.py | 11 +++++++++++ scripts/validate_docstrings.py | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index 27c63e3ba3a79..e084562057091 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -334,6 +334,14 @@ def method(self, foo=None, bar=None): pass +class BadSeeAlso(object): + + def prefix_pandas(self): + """ + Return prefix with `pandas` from See Also sec + """ + pass + class BadSummaries(object): def wrong_line(self): @@ -564,6 +572,9 @@ def test_bad_generic_functions(self, func): assert errors @pytest.mark.parametrize("klass,func,msgs", [ + #SeeAlso tests + ('BadSeeAlso', 'prefix_pandas', + ('Should not start with pandas',)), # Summary tests ('BadSummaries', 'wrong_line', ('should start in the line immediately after the opening quotes',)), diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index 6588522331433..d6ad442687066 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -499,7 +499,8 @@ def validate_one(func_name): if not rel_desc: errs.append('Missing description for ' 'See Also "{}" reference'.format(rel_name)) - + if rel_name[:7].lower() == 'pandas.': + errs.append('{} should not have prefix `pandas`'.format(rel_name)) for line in doc.raw_doc.splitlines(): if re.match("^ *\t", line): errs.append('Tabs found at the start of line "{}", ' From 49719378350c03da13f7db2ce5681ee1010bb082 Mon Sep 17 00:00:00 2001 From: thoo Date: Sun, 14 Oct 2018 12:58:25 -0400 Subject: [PATCH 3/6] Update error message and test validator --- scripts/tests/test_validate_docstrings.py | 24 +++++++++++------------ scripts/validate_docstrings.py | 5 +++-- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index e084562057091..44fdd4ca0a3bf 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -333,15 +333,6 @@ def method(self, foo=None, bar=None): """ pass - -class BadSeeAlso(object): - - def prefix_pandas(self): - """ - Return prefix with `pandas` from See Also sec - """ - pass - class BadSummaries(object): def wrong_line(self): @@ -509,6 +500,13 @@ def no_punctuation(self): """ return "Hello world!" +class BadSeeAlso(object): + + def prefix_pandas(self): + """ + Return prefix with `pandas` from See Also sec + """ + pass class TestValidator(object): @@ -572,9 +570,6 @@ def test_bad_generic_functions(self, func): assert errors @pytest.mark.parametrize("klass,func,msgs", [ - #SeeAlso tests - ('BadSeeAlso', 'prefix_pandas', - ('Should not start with pandas',)), # Summary tests ('BadSummaries', 'wrong_line', ('should start in the line immediately after the opening quotes',)), @@ -611,7 +606,10 @@ def test_bad_generic_functions(self, func): pytest.param('BadReturns', 'no_description', ('foo',), marks=pytest.mark.xfail), pytest.param('BadReturns', 'no_punctuation', ('foo',), - marks=pytest.mark.xfail) + marks=pytest.mark.xfail), + # SeeAlso tests + ('BadSeeAlso', 'prefix_pandas', + ('Should not start with pandas',)), ]) def test_bad_examples(self, capsys, klass, func, msgs): result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821 diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index d6ad442687066..7b2b54330831c 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -499,8 +499,9 @@ def validate_one(func_name): if not rel_desc: errs.append('Missing description for ' 'See Also "{}" reference'.format(rel_name)) - if rel_name[:7].lower() == 'pandas.': - errs.append('{} should not have prefix `pandas`'.format(rel_name)) + if rel_name.startswith('pandas.'): + errs.append('{} in the `See Also` section does not need the `pandas` prefix, ' + 'use {} instead.'.format(rel_name,rel_name.replace('pandas.',''))) for line in doc.raw_doc.splitlines(): if re.match("^ *\t", line): errs.append('Tabs found at the start of line "{}", ' From 744115a862aead2dfe60d7ee001b0800b8d2ad8a Mon Sep 17 00:00:00 2001 From: thoo Date: Sun, 14 Oct 2018 19:27:38 -0400 Subject: [PATCH 4/6] Fix string prasing and test validator --- scripts/tests/test_validate_docstrings.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index 44fdd4ca0a3bf..b3d78b3558cef 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -505,6 +505,10 @@ class BadSeeAlso(object): def prefix_pandas(self): """ Return prefix with `pandas` from See Also sec + + See Also + -------- + pandas.Series.rename : Alter Series index labels or name """ pass @@ -609,7 +613,7 @@ def test_bad_generic_functions(self, func): marks=pytest.mark.xfail), # SeeAlso tests ('BadSeeAlso', 'prefix_pandas', - ('Should not start with pandas',)), + ('section does not need the `pandas` prefix',)), ]) def test_bad_examples(self, capsys, klass, func, msgs): result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821 From bcb33b848a08a82e410c9e98cf6ca9bd1531a659 Mon Sep 17 00:00:00 2001 From: thoo Date: Sun, 14 Oct 2018 21:07:13 -0400 Subject: [PATCH 5/6] PEP8 compliant fixes --- scripts/tests/test_validate_docstrings.py | 5 ++++- scripts/validate_docstrings.py | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index b3d78b3558cef..042f974690ad4 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -333,6 +333,7 @@ def method(self, foo=None, bar=None): """ pass + class BadSummaries(object): def wrong_line(self): @@ -500,6 +501,7 @@ def no_punctuation(self): """ return "Hello world!" + class BadSeeAlso(object): def prefix_pandas(self): @@ -512,6 +514,7 @@ def prefix_pandas(self): """ pass + class TestValidator(object): def _import_path(self, klass=None, func=None): @@ -613,7 +616,7 @@ def test_bad_generic_functions(self, func): marks=pytest.mark.xfail), # SeeAlso tests ('BadSeeAlso', 'prefix_pandas', - ('section does not need the `pandas` prefix',)), + ('section does not need `pandas` prefix',)), ]) def test_bad_examples(self, capsys, klass, func, msgs): result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821 diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index 7b2b54330831c..82927db6170a3 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -500,8 +500,9 @@ def validate_one(func_name): errs.append('Missing description for ' 'See Also "{}" reference'.format(rel_name)) if rel_name.startswith('pandas.'): - errs.append('{} in the `See Also` section does not need the `pandas` prefix, ' - 'use {} instead.'.format(rel_name,rel_name.replace('pandas.',''))) + errs.append('{} in `See Also` section does not ' + 'need `pandas` prefix, use {} instead.' + .format(rel_name, rel_name.replace('pandas.', ''))) for line in doc.raw_doc.splitlines(): if re.match("^ *\t", line): errs.append('Tabs found at the start of line "{}", ' From 5c2f7611879ac93a058994b0be077c0210e34b2d Mon Sep 17 00:00:00 2001 From: thoo Date: Sun, 14 Oct 2018 22:59:47 -0400 Subject: [PATCH 6/6] Check numpy and pandas import in Examples/untrack cookbook.rst --- doc/source/cookbook.rst | 6 ++++++ scripts/tests/test_validate_docstrings.py | 18 ++++++++++-------- scripts/validate_docstrings.py | 11 +++++++---- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/doc/source/cookbook.rst b/doc/source/cookbook.rst index df0048c8a74b4..21c8ab4128188 100644 --- a/doc/source/cookbook.rst +++ b/doc/source/cookbook.rst @@ -1234,19 +1234,25 @@ The `method` argument within `DataFrame.corr` can accept a callable in addition n = len(x) a = np.zeros(shape=(n, n)) b = np.zeros(shape=(n, n)) + for i in range(n): for j in range(i + 1, n): a[i, j] = abs(x[i] - x[j]) b[i, j] = abs(y[i] - y[j]) + a += a.T b += b.T + a_bar = np.vstack([np.nanmean(a, axis=0)] * n) b_bar = np.vstack([np.nanmean(b, axis=0)] * n) + A = a - a_bar - a_bar.T + np.full(shape=(n, n), fill_value=a_bar.mean()) B = b - b_bar - b_bar.T + np.full(shape=(n, n), fill_value=b_bar.mean()) + cov_ab = np.sqrt(np.nansum(A * B)) / n std_a = np.sqrt(np.sqrt(np.nansum(A**2)) / n) std_b = np.sqrt(np.sqrt(np.nansum(B**2)) / n) + return cov_ab / std_a / std_b df = pd.DataFrame(np.random.normal(size=(100, 3))) diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index 042f974690ad4..01d7b0b4bd3bb 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -502,15 +502,16 @@ def no_punctuation(self): return "Hello world!" -class BadSeeAlso(object): +class BadExamples(object): - def prefix_pandas(self): + def npPd_import(self): """ - Return prefix with `pandas` from See Also sec + Provide example with numpy and pandas import - See Also + Examples -------- - pandas.Series.rename : Alter Series index labels or name + import numpy as np + import pandas as pd """ pass @@ -614,9 +615,10 @@ def test_bad_generic_functions(self, func): marks=pytest.mark.xfail), pytest.param('BadReturns', 'no_punctuation', ('foo',), marks=pytest.mark.xfail), - # SeeAlso tests - ('BadSeeAlso', 'prefix_pandas', - ('section does not need `pandas` prefix',)), + # Examples + ('BadExamples', 'npPd_import', + ('Examples should not have `import pandas as pd` ', + 'Examples should not have `import numpy as np` ',)) ]) def test_bad_examples(self, capsys, klass, func, msgs): result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821 diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index 82927db6170a3..b58be4961595a 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -499,10 +499,7 @@ def validate_one(func_name): if not rel_desc: errs.append('Missing description for ' 'See Also "{}" reference'.format(rel_name)) - if rel_name.startswith('pandas.'): - errs.append('{} in `See Also` section does not ' - 'need `pandas` prefix, use {} instead.' - .format(rel_name, rel_name.replace('pandas.', ''))) + for line in doc.raw_doc.splitlines(): if re.match("^ *\t", line): errs.append('Tabs found at the start of line "{}", ' @@ -512,6 +509,12 @@ def validate_one(func_name): if not doc.examples: wrns.append('No examples section found') else: + if 'import numpy as np' in ' '.join(doc.examples): + errs.append(' Examples should not have ' + '`import numpy as np` ') + if 'import pandas as pd' in ' '.join(doc.examples): + errs.append(' Examples should not have ' + '`import pandas as pd` ') examples_errs = doc.examples_errors if examples_errs: errs.append('Examples do not pass tests')