Skip to content

Commit 2fb50b9

Browse files
YuechengWuPingviinituutti
authored andcommitted
Fix format of advanced.rst according to PEP-8 standard (pandas-dev#23893)
Closes pandas-devgh-23790
1 parent c6309de commit 2fb50b9

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

doc/source/advanced.rst

+46-45
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pandas as pd
1010
np.random.seed(123456)
1111
np.set_printoptions(precision=4, suppress=True)
12-
pd.options.display.max_rows=15
12+
pd.options.display.max_rows = 15
1313
1414
******************************
1515
MultiIndex / Advanced Indexing
@@ -188,10 +188,10 @@ highly performant. If you want to see only the used levels, you can use the
188188

189189
.. ipython:: python
190190
191-
df[['foo','qux']].columns.values
191+
df[['foo', 'qux']].columns.values
192192
193193
# for a specific level
194-
df[['foo','qux']].columns.get_level_values(0)
194+
df[['foo', 'qux']].columns.get_level_values(0)
195195
196196
To reconstruct the ``MultiIndex`` with only the used levels, the
197197
:meth:`~MultiIndex.remove_unused_levels` method may be used.
@@ -200,7 +200,7 @@ To reconstruct the ``MultiIndex`` with only the used levels, the
200200

201201
.. ipython:: python
202202
203-
df[['foo','qux']].columns.remove_unused_levels()
203+
df[['foo', 'qux']].columns.remove_unused_levels()
204204
205205
Data alignment and using ``reindex``
206206
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -235,7 +235,7 @@ keys take the form of tuples. For example, the following works as you would expe
235235
236236
df = df.T
237237
df
238-
df.loc[('bar', 'two'),]
238+
df.loc[('bar', 'two')]
239239
240240
Note that ``df.loc['bar', 'two']`` would also work in this example, but this shorthand
241241
notation can lead to ambiguity in general.
@@ -328,17 +328,18 @@ As usual, **both sides** of the slicers are included as this is label indexing.
328328
329329
.. ipython:: python
330330
331-
def mklbl(prefix,n):
332-
return ["%s%s" % (prefix,i) for i in range(n)]
331+
def mklbl(prefix, n):
332+
return ["%s%s" % (prefix, i) for i in range(n)]
333333
334-
miindex = pd.MultiIndex.from_product([mklbl('A',4),
335-
mklbl('B',2),
336-
mklbl('C',4),
337-
mklbl('D',2)])
338-
micolumns = pd.MultiIndex.from_tuples([('a','foo'),('a','bar'),
339-
('b','foo'),('b','bah')],
334+
miindex = pd.MultiIndex.from_product([mklbl('A', 4),
335+
mklbl('B', 2),
336+
mklbl('C', 4),
337+
mklbl('D', 2)])
338+
micolumns = pd.MultiIndex.from_tuples([('a', 'foo'), ('a', 'bar'),
339+
('b', 'foo'), ('b', 'bah')],
340340
names=['lvl0', 'lvl1'])
341-
dfmi = pd.DataFrame(np.arange(len(miindex)*len(micolumns)).reshape((len(miindex),len(micolumns))),
341+
dfmi = pd.DataFrame(np.arange(len(miindex) * len(micolumns))
342+
.reshape((len(miindex), len(micolumns))),
342343
index=miindex,
343344
columns=micolumns).sort_index().sort_index(axis=1)
344345
dfmi
@@ -347,7 +348,7 @@ Basic MultiIndex slicing using slices, lists, and labels.
347348

348349
.. ipython:: python
349350
350-
dfmi.loc[(slice('A1','A3'), slice(None), ['C1', 'C3']), :]
351+
dfmi.loc[(slice('A1', 'A3'), slice(None), ['C1', 'C3']), :]
351352
352353
353354
You can use :class:`pandas.IndexSlice` to facilitate a more natural syntax
@@ -412,7 +413,7 @@ selecting data at a particular level of a ``MultiIndex`` easier.
412413
.. ipython:: python
413414
414415
# using the slicers
415-
df.loc[(slice(None),'one'),:]
416+
df.loc[(slice(None), 'one'), :]
416417
417418
You can also select on the columns with ``xs``, by
418419
providing the axis argument.
@@ -425,7 +426,7 @@ providing the axis argument.
425426
.. ipython:: python
426427
427428
# using the slicers
428-
df.loc[:,(slice(None),'one')]
429+
df.loc[:, (slice(None), 'one')]
429430
430431
``xs`` also allows selection with multiple keys.
431432

@@ -436,7 +437,7 @@ providing the axis argument.
436437
.. ipython:: python
437438
438439
# using the slicers
439-
df.loc[:,('bar','one')]
440+
df.loc[:, ('bar', 'one')]
440441
441442
You can pass ``drop_level=False`` to ``xs`` to retain
442443
the level that was selected.
@@ -467,9 +468,9 @@ values across a level. For instance:
467468

468469
.. ipython:: python
469470
470-
midx = pd.MultiIndex(levels=[['zero', 'one'], ['x','y']],
471-
labels=[[1,1,0,0],[1,0,1,0]])
472-
df = pd.DataFrame(np.random.randn(4,2), index=midx)
471+
midx = pd.MultiIndex(levels=[['zero', 'one'], ['x', 'y']],
472+
labels=[[1, 1, 0, 0], [1, 0, 1, 0]])
473+
df = pd.DataFrame(np.random.randn(4, 2), index=midx)
473474
df
474475
df2 = df.mean(level=0)
475476
df2
@@ -501,7 +502,7 @@ method, allowing you to permute the hierarchical index levels in one step:
501502

502503
.. ipython:: python
503504
504-
df[:5].reorder_levels([1,0], axis=0)
505+
df[:5].reorder_levels([1, 0], axis=0)
505506
506507
.. _advanced.index_names:
507508

@@ -522,7 +523,7 @@ of the ``DataFrame``.
522523

523524
.. ipython:: python
524525
525-
df.rename(index={"one" : "two", "y" : "z"})
526+
df.rename(index={"one": "two", "y": "z"})
526527
527528
The :meth:`~DataFrame.rename_axis` method is used to rename the name of a
528529
``Index`` or ``MultiIndex``. In particular, the names of the levels of a
@@ -605,7 +606,7 @@ Furthermore, if you try to index something that is not fully lexsorted, this can
605606

606607
.. code-block:: ipython
607608
608-
In [5]: dfm.loc[(0,'y'):(1, 'z')]
609+
In [5]: dfm.loc[(0, 'y'):(1, 'z')]
609610
UnsortedIndexError: 'Key length (2) was greater than MultiIndex lexsort depth (1)'
610611
611612
The :meth:`~MultiIndex.is_lexsorted` method on a ``MultiIndex`` shows if the
@@ -627,7 +628,7 @@ And now selection works as expected.
627628

628629
.. ipython:: python
629630
630-
dfm.loc[(0,'y'):(1, 'z')]
631+
dfm.loc[(0, 'y'):(1, 'z')]
631632
632633
Take Methods
633634
------------
@@ -688,12 +689,12 @@ faster than fancy indexing.
688689
indexer = np.arange(10000)
689690
random.shuffle(indexer)
690691

691-
timeit arr[indexer]
692-
timeit arr.take(indexer, axis=0)
692+
%timeit arr[indexer]
693+
%timeit arr.take(indexer, axis=0)
693694

694695
ser = pd.Series(arr[:, 0])
695-
timeit ser.iloc[indexer]
696-
timeit ser.take(indexer)
696+
%timeit ser.iloc[indexer]
697+
%timeit ser.take(indexer)
697698

698699
.. _indexing.index_types:
699700

@@ -718,7 +719,6 @@ and allows efficient indexing and storage of an index with a large number of dup
718719
.. ipython:: python
719720
720721
from pandas.api.types import CategoricalDtype
721-
722722
df = pd.DataFrame({'A': np.arange(6),
723723
'B': list('aabbca')})
724724
df['B'] = df['B'].astype(CategoricalDtype(list('cab')))
@@ -781,16 +781,15 @@ values **not** in the categories, similarly to how you can reindex **any** panda
781781

782782
.. code-block:: ipython
783783
784-
In [9]: df3 = pd.DataFrame({'A' : np.arange(6),
785-
'B' : pd.Series(list('aabbca')).astype('category')})
784+
In [9]: df3 = pd.DataFrame({'A': np.arange(6), 'B': pd.Series(list('aabbca')).astype('category')})
786785
787-
In [11]: df3 = df3.set_index('B')
786+
In [11]: df3 = df3.set_index('B')
788787
789-
In [11]: df3.index
790-
Out[11]: CategoricalIndex([u'a', u'a', u'b', u'b', u'c', u'a'], categories=[u'a', u'b', u'c'], ordered=False, name=u'B', dtype='category')
788+
In [11]: df3.index
789+
Out[11]: CategoricalIndex([u'a', u'a', u'b', u'b', u'c', u'a'], categories=[u'a', u'b', u'c'], ordered=False, name=u'B', dtype='category')
791790
792-
In [12]: pd.concat([df2, df3]
793-
TypeError: categories must match existing categories when appending
791+
In [12]: pd.concat([df2, df3])
792+
TypeError: categories must match existing categories when appending
794793
795794
.. _indexing.rangeindex:
796795

@@ -883,11 +882,11 @@ example, be millisecond offsets.
883882

884883
.. ipython:: python
885884
886-
dfir = pd.concat([pd.DataFrame(np.random.randn(5,2),
885+
dfir = pd.concat([pd.DataFrame(np.random.randn(5, 2),
887886
index=np.arange(5) * 250.0,
888887
columns=list('AB')),
889-
pd.DataFrame(np.random.randn(6,2),
890-
index=np.arange(4,10) * 250.1,
888+
pd.DataFrame(np.random.randn(6, 2),
889+
index=np.arange(4, 10) * 250.1,
891890
columns=list('AB'))])
892891
dfir
893892
@@ -896,7 +895,7 @@ Selection operations then will always work on a value basis, for all selection o
896895
.. ipython:: python
897896
898897
dfir[0:1000.4]
899-
dfir.loc[0:1001,'A']
898+
dfir.loc[0:1001, 'A']
900899
dfir.loc[1000.4]
901900
902901
You could retrieve the first 1 second (1000 ms) of data as such:
@@ -934,7 +933,7 @@ An ``IntervalIndex`` can be used in ``Series`` and in ``DataFrame`` as the index
934933
.. ipython:: python
935934
936935
df = pd.DataFrame({'A': [1, 2, 3, 4]},
937-
index=pd.IntervalIndex.from_breaks([0, 1, 2, 3, 4]))
936+
index=pd.IntervalIndex.from_breaks([0, 1, 2, 3, 4]))
938937
df
939938
940939
Label based indexing via ``.loc`` along the edges of an interval works as you would expect,
@@ -1014,7 +1013,8 @@ in the resulting ``IntervalIndex``:
10141013
10151014
pd.interval_range(start=0, end=6, periods=4)
10161015
1017-
pd.interval_range(pd.Timestamp('2018-01-01'), pd.Timestamp('2018-02-28'), periods=3)
1016+
pd.interval_range(pd.Timestamp('2018-01-01'),
1017+
pd.Timestamp('2018-02-28'), periods=3)
10181018
10191019
Miscellaneous indexing FAQ
10201020
--------------------------
@@ -1051,7 +1051,7 @@ normal Python ``list``. Monotonicity of an index can be tested with the :meth:`~
10511051

10521052
.. ipython:: python
10531053
1054-
df = pd.DataFrame(index=[2,3,3,4,5], columns=['data'], data=list(range(5)))
1054+
df = pd.DataFrame(index=[2, 3, 3, 4, 5], columns=['data'], data=list(range(5)))
10551055
df.index.is_monotonic_increasing
10561056
10571057
# no rows 0 or 1, but still returns rows 2, 3 (both of them), and 4:
@@ -1065,7 +1065,8 @@ On the other hand, if the index is not monotonic, then both slice bounds must be
10651065

10661066
.. ipython:: python
10671067
1068-
df = pd.DataFrame(index=[2,3,1,4,3,5], columns=['data'], data=list(range(6)))
1068+
df = pd.DataFrame(index=[2, 3, 1, 4, 3, 5],
1069+
columns=['data'], data=list(range(6)))
10691070
df.index.is_monotonic_increasing
10701071
10711072
# OK because 2 and 4 are in the index

0 commit comments

Comments
 (0)