Repository: pandas-dev/pandas
Pull request: DOC: Fix dsintro.rst flake8 issues
- [x] closes #24175 
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`


Problem statement:
DOC: Fix flake8 issues in doc/source/dsintro.rst
We didn't start validating the format of PEP8 and other code standards in the documentation examples until recently. We still have some files with errors, that we need to skip, and that we should fix, so we can start validating them.

The first step of this issue would be edit `setup.cfg` in the pandas home, and in the `flake8-rst` section, remove from the `exclude` list the file `doc/source/dsintro.rst`

After that, running the next command will report the errors in the file (note that syntax error usually prevent to validate other errors, and the list of errors to fix can become much longer when the syntax error is fixed (please make sure that you are using `flake8-rst` version `0.7.0` or higher):

```
$ flake8-rst doc/source/dsintro.rst 
doc/source/dsintro.rst:74:12: E203 whitespace before ':'
doc/source/dsintro.rst:74:21: E203 whitespace before ':'
doc/source/dsintro.rst:74:30: E203 whitespace before ':'
doc/source/dsintro.rst:95:12: E203 whitespace before ':'
doc/source/dsintro.rst:95:22: E203 whitespace before ':'
doc/source/dsintro.rst:95:32: E203 whitespace before ':'
doc/source/dsintro.rst:307:15: E203 whitespace before ':'
doc/source/dsintro.rst:308:15: E203 whitespace before ':'
doc/source/dsintro.rst:337:14: E203 whitespace before ':'
doc/source/dsintro.rst:338:14: E203 whitespace before ':'
doc/source/dsintro.rst:349:44: E231 missing whitespace after ','
doc/source/dsintro.rst:349:56: E231 missing whitespace after ','
doc/source/dsintro.rst:350:17: E231 missing whitespace after ','
doc/source/dsintro.rst:350:20: E231 missing whitespace after ','
doc/source/dsintro.rst:350:33: E231 missing whitespace after ','
doc/source/dsintro.rst:350:36: E231 missing whitespace after ','
doc/source/dsintro.rst:511:28: E251 unexpected spaces around keyword / parameter equals
doc/source/dsintro.rst:511:30: E251 unexpected spaces around keyword / parameter equals
doc/source/dsintro.rst:519:27: E251 unexpected spaces around keyword / parameter equals
doc/source/dsintro.rst:519:29: E251 unexpected spaces around keyword / parameter equals
doc/source/dsintro.rst:519:41: W504 line break after binary operator
doc/source/dsintro.rst:535:27: E251 unexpected spaces around keyword / parameter equals
doc/source/dsintro.rst:535:29: E251 unexpected spaces around keyword / parameter equals
doc/source/dsintro.rst:536:27: E251 unexpected spaces around keyword / parameter equals
doc/source/dsintro.rst:536:29: E251 unexpected spaces around keyword / parameter equals
doc/source/dsintro.rst:708:27: E203 whitespace before ':'
doc/source/dsintro.rst:708:44: E203 whitespace before ':'
doc/source/dsintro.rst:708:56: E202 whitespace before '}'
doc/source/dsintro.rst:709:27: E203 whitespace before ':'
doc/source/dsintro.rst:709:44: E203 whitespace before ':'
doc/source/dsintro.rst:709:56: E202 whitespace before '}'
doc/source/dsintro.rst:749:30: E231 missing whitespace after ','
doc/source/dsintro.rst:780:13: W605 invalid escape sequence '\.'
doc/source/dsintro.rst:801:38: E261 at least two spaces before inline comment
doc/source/dsintro.rst:809:12: E225 missing whitespace around operator
doc/source/dsintro.rst:809:40: E231 missing whitespace after ','
doc/source/dsintro.rst:813:40: E231 missing whitespace after ','
doc/source/dsintro.rst:816:40: E231 missing whitespace after ','
doc/source/dsintro.rst:836:29: E203 whitespace before ':'
doc/source/dsintro.rst:837:29: E203 whitespace before ':'
doc/source/dsintro.rst:846:18: E225 missing whitespace around operator
doc/source/dsintro.rst:846:22: E225 missing whitespace around operator
doc/source/dsintro.rst:846:22: E999 SyntaxError: invalid syntax
doc/source/dsintro.rst:893:19: E203 whitespace before ':'
doc/source/dsintro.rst:894:19: E203 whitespace before ':'
doc/source/dsintro.rst:950:53: E231 missing whitespace after ','
doc/source/dsintro.rst:950:70: E231 missing whitespace after ','
doc/source/dsintro.rst:950:72: E231 missing whitespace after ','
doc/source/dsintro.rst:950:74: E231 missing whitespace after ','
doc/source/dsintro.rst:950:77: E231 missing whitespace after ','
doc/source/dsintro.rst:950:80: E231 missing whitespace after ','
doc/source/dsintro.rst:950:82: E231 missing whitespace after ','
doc/source/dsintro.rst:950:83: E501 line too long (85 > 79 characters)
doc/source/dsintro.rst:950:84: E231 missing whitespace after ','
doc/source/dsintro.rst:951:26: E203 whitespace before ':'
```

Once all the errors are addressed, please open a pull request with the fixes in the file, and removing the file from `setup.cfg`. If you need to do something that feels wrong to fix an error, please ask in a comment to this issue. Please avoid other unrelated changes, which can be addressed in a separate pull request.

Hints from issue discussion:

Commit message: Repair doc/source/dsintro.rst flake8 issues, remove exclusion

Patch to review:
diff --git a/doc/source/dsintro.rst b/doc/source/dsintro.rst
index 8bdb0005de53c..029824522a18e 100644
--- a/doc/source/dsintro.rst
+++ b/doc/source/dsintro.rst
@@ -71,7 +71,7 @@ Series can be instantiated from dicts:
 
 .. ipython:: python
 
-   d = {'b' : 1, 'a' : 0, 'c' : 2}
+   d = {'b': 1, 'a': 0, 'c': 2}
    pd.Series(d)
 
 .. note::
@@ -92,7 +92,7 @@ index will be pulled out.
 
 .. ipython:: python
 
-   d = {'a' : 0., 'b' : 1., 'c' : 2.}
+   d = {'a': 0., 'b': 1., 'c': 2.}
    pd.Series(d)
    pd.Series(d, index=['b', 'c', 'd', 'a'])
 
@@ -304,8 +304,8 @@ keys.
 
 .. ipython:: python
 
-    d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
-         'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
+    d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
+         'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
     df = pd.DataFrame(d)
     df
 
@@ -334,8 +334,8 @@ result will be ``range(n)``, where ``n`` is the array length.
 
 .. ipython:: python
 
-   d = {'one' : [1., 2., 3., 4.],
-        'two' : [4., 3., 2., 1.]}
+   d = {'one': [1., 2., 3., 4.],
+        'two': [4., 3., 2., 1.]}
    pd.DataFrame(d)
    pd.DataFrame(d, index=['a', 'b', 'c', 'd'])
 
@@ -346,8 +346,8 @@ This case is handled identically to a dict of arrays.
 
 .. ipython:: python
 
-   data = np.zeros((2,), dtype=[('A', 'i4'),('B', 'f4'),('C', 'a10')])
-   data[:] = [(1,2.,'Hello'), (2,3.,"World")]
+   data = np.zeros((2, ), dtype=[('A', 'i4'), ('B', 'f4'), ('C', 'a10')])
+   data[:] = [(1, 2., 'Hello'), (2, 3., "World")]
 
    pd.DataFrame(data)
    pd.DataFrame(data, index=['first', 'second'])
@@ -507,8 +507,7 @@ derived from existing columns.
 
    iris = pd.read_csv('data/iris.data')
    iris.head()
-
-   (iris.assign(sepal_ratio = iris['SepalWidth'] / iris['SepalLength'])
+   (iris.assign(sepal_ratio=iris['SepalWidth'] / iris['SepalLength'])
         .head())
 
 In the example above, we inserted a precomputed value. We can also pass in
@@ -516,8 +515,7 @@ a function of one argument to be evaluated on the DataFrame being assigned to.
 
 .. ipython:: python
 
-   iris.assign(sepal_ratio = lambda x: (x['SepalWidth'] /
-                                        x['SepalLength'])).head()
+   iris.assign(sepal_ratio=lambda x: (x['SepalWidth'] / x['SepalLength'])).head()
 
 ``assign`` **always** returns a copy of the data, leaving the original
 DataFrame untouched.
@@ -532,8 +530,8 @@ greater than 5, calculate the ratio, and plot:
 
    @savefig basics_assign.png
    (iris.query('SepalLength > 5')
-        .assign(SepalRatio = lambda x: x.SepalWidth / x.SepalLength,
-                PetalRatio = lambda x: x.PetalWidth / x.PetalLength)
+        .assign(SepalRatio=lambda x: x.SepalWidth / x.SepalLength,
+                PetalRatio=lambda x: x.PetalWidth / x.PetalLength)
         .plot(kind='scatter', x='SepalRatio', y='PetalRatio'))
 
 Since a function is passed in, the function is computed on the DataFrame
@@ -705,8 +703,8 @@ Boolean operators work as well:
 
 .. ipython:: python
 
-   df1 = pd.DataFrame({'a' : [1, 0, 1], 'b' : [0, 1, 1] }, dtype=bool)
-   df2 = pd.DataFrame({'a' : [0, 1, 1], 'b' : [1, 1, 0] }, dtype=bool)
+   df1 = pd.DataFrame({'a': [1, 0, 1], 'b': [0, 1, 1]}, dtype=bool)
+   df2 = pd.DataFrame({'a': [0, 1, 1], 'b': [1, 1, 0]}, dtype=bool)
    df1 & df2
    df1 | df2
    df1 ^ df2
@@ -746,7 +744,7 @@ Similarly, the dot method on Series implements dot product:
 
 .. ipython:: python
 
-   s1 = pd.Series(np.arange(5,10))
+   s1 = pd.Series(np.arange(5, 10))
    s1.dot(s1)
 
 DataFrame is not intended to be a drop-in replacement for ndarray as its
@@ -777,7 +775,7 @@ R package):
    :okwarning:
 
    # restore GlobalPrintConfig
-   pd.reset_option('^display\.')
+   pd.reset_option('^display\.')  # noqa: W605
 
 However, using ``to_string`` will return a string representation of the
 DataFrame in tabular form, though it won't always fit the console width:
@@ -798,7 +796,7 @@ option:
 
 .. ipython:: python
 
-   pd.set_option('display.width', 40) # default is 80
+   pd.set_option('display.width', 40)  # default is 80
 
    pd.DataFrame(np.random.randn(3, 12))
 
@@ -806,14 +804,14 @@ You can adjust the max width of the individual columns by setting ``display.max_
 
 .. ipython:: python
 
-   datafile={'filename': ['filename_01','filename_02'],
-             'path': ["media/user_name/storage/folder_01/filename_01",
-                      "media/user_name/storage/folder_02/filename_02"]}
+   datafile = {'filename': ['filename_01', 'filename_02'],
+               'path': ["media/user_name/storage/folder_01/filename_01",
+                        "media/user_name/storage/folder_02/filename_02"]}
 
-   pd.set_option('display.max_colwidth',30)
+   pd.set_option('display.max_colwidth', 30)
    pd.DataFrame(datafile)
 
-   pd.set_option('display.max_colwidth',100)
+   pd.set_option('display.max_colwidth', 100)
    pd.DataFrame(datafile)
 
 .. ipython:: python
@@ -833,8 +831,8 @@ accessed like an attribute:
 
 .. ipython:: python
 
-   df = pd.DataFrame({'foo1' : np.random.randn(5),
-                      'foo2' : np.random.randn(5)})
+   df = pd.DataFrame({'foo1': np.random.randn(5),
+                      'foo2': np.random.randn(5)})
    df
    df.foo1
 
@@ -843,7 +841,7 @@ completion mechanism so they can be tab-completed:
 
 .. code-block:: ipython
 
-    In [5]: df.fo<TAB>
+    In [5]: df.fo<TAB>  # noqa: E225, E999
     df.foo1  df.foo2
 
 .. _basics.panel:
@@ -890,8 +888,8 @@ From dict of DataFrame objects
 .. ipython:: python
    :okwarning:
 
-   data = {'Item1' : pd.DataFrame(np.random.randn(4, 3)),
-           'Item2' : pd.DataFrame(np.random.randn(4, 2))}
+   data = {'Item1': pd.DataFrame(np.random.randn(4, 3)),
+           'Item2': pd.DataFrame(np.random.randn(4, 2))}
    pd.Panel(data)
 
 Note that the values in the dict need only be **convertible to
@@ -947,8 +945,9 @@ From DataFrame using ``to_panel`` method
 .. ipython:: python
    :okwarning:
 
-   midx = pd.MultiIndex(levels=[['one', 'two'], ['x','y']], codes=[[1,1,0,0],[1,0,1,0]])
-   df = pd.DataFrame({'A' : [1, 2, 3, 4], 'B': [5, 6, 7, 8]}, index=midx)
+   midx = pd.MultiIndex(levels=[['one', 'two'], ['x', 'y']],
+                        codes=[[1, 1, 0, 0], [1, 0, 1, 0]])
+   df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]}, index=midx)
    df.to_panel()
 
 .. _dsintro.panel_item_selection:
diff --git a/setup.cfg b/setup.cfg
index 73a3a6c136b53..e21836cb28e72 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -79,7 +79,6 @@ exclude =
     doc/source/10min.rst
     doc/source/basics.rst
     doc/source/contributing_docstring.rst
-    doc/source/dsintro.rst
     doc/source/enhancingperf.rst
     doc/source/groupby.rst
     doc/source/indexing.rst


Code review comments:
diff_hunk: @@ -777,7 +775,7 @@ R package):
    :okwarning:
 
    # restore GlobalPrintConfig
-   pd.reset_option('^display\.')
+   pd.reset_option('^display\.')  # noqa: W605
path: doc/source/dsintro.rst
text: @user1:
```suggestion
   pd.reset_option(r'^display\.')
```

@author:
Done