Skip to content

Commit 4aa267a

Browse files
committed
more code formatting
1 parent ad7bb1d commit 4aa267a

File tree

2 files changed

+66
-45
lines changed

2 files changed

+66
-45
lines changed

doc/source/user_guide/10min.rst

+47-29
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ Creating a :class:`DataFrame` by passing a dict of objects that can be converted
4343

4444
.. ipython:: python
4545
46-
df2 = pd.DataFrame({'A': 1.,
47-
'B': pd.Timestamp('20130102'),
48-
'C': pd.Series(1, index=list(range(4)), dtype='float32'),
49-
'D': np.array([3] * 4, dtype='int32'),
50-
'E': pd.Categorical(["test", "train", "test", "train"]),
51-
'F': 'foo'})
46+
df2 = pd.DataFrame(
47+
{
48+
"A": 1.0,
49+
"B": pd.Timestamp("20130102"),
50+
"C": pd.Series(1, index=list(range(4)), dtype="float32"),
51+
"D": np.array([3] * 4, dtype="int32"),
52+
"E": pd.Categorical(["test", "train", "test", "train"]),
53+
"F": "foo",
54+
}
55+
)
5256
df2
5357
5458
The columns of the resulting :class:`DataFrame` have different
@@ -512,12 +516,14 @@ See the :ref:`Grouping section <groupby>`.
512516

513517
.. ipython:: python
514518
515-
df = pd.DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
516-
'foo', 'bar', 'foo', 'foo'],
517-
'B': ['one', 'one', 'two', 'three',
518-
'two', 'two', 'one', 'three'],
519-
'C': np.random.randn(8),
520-
'D': np.random.randn(8)})
519+
df = pd.DataFrame(
520+
{
521+
"A": ["foo", "bar", "foo", "bar", "foo", "bar", "foo", "foo"],
522+
"B": ["one", "one", "two", "three", "two", "two", "one", "three"],
523+
"C": np.random.randn(8),
524+
"D": np.random.randn(8),
525+
}
526+
)
521527
df
522528
523529
Grouping and then applying the :meth:`~pandas.core.groupby.GroupBy.sum` function to the resulting
@@ -545,10 +551,14 @@ Stack
545551

546552
.. ipython:: python
547553
548-
tuples = list(zip(*[['bar', 'bar', 'baz', 'baz',
549-
'foo', 'foo', 'qux', 'qux'],
550-
['one', 'two', 'one', 'two',
551-
'one', 'two', 'one', 'two']]))
554+
tuples = list(
555+
zip(
556+
*[
557+
["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
558+
["one", "two", "one", "two", "one", "two", "one", "two"],
559+
]
560+
)
561+
)
552562
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
553563
df = pd.DataFrame(np.random.randn(8, 2), index=index, columns=['A', 'B'])
554564
df2 = df[:4]
@@ -578,11 +588,15 @@ See the section on :ref:`Pivot Tables <reshaping.pivot>`.
578588

579589
.. ipython:: python
580590
581-
df = pd.DataFrame({'A': ['one', 'one', 'two', 'three'] * 3,
582-
'B': ['A', 'B', 'C'] * 4,
583-
'C': ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2,
584-
'D': np.random.randn(12),
585-
'E': np.random.randn(12)})
591+
df = pd.DataFrame(
592+
{
593+
"A": ["one", "one", "two", "three"] * 3,
594+
"B": ["A", "B", "C"] * 4,
595+
"C": ["foo", "foo", "foo", "bar", "bar", "bar"] * 2,
596+
"D": np.random.randn(12),
597+
"E": np.random.randn(12),
598+
}
599+
)
586600
df
587601
588602
We can produce pivot tables from this data very easily:
@@ -653,8 +667,10 @@ pandas can include categorical data in a :class:`DataFrame`. For full docs, see
653667

654668
.. ipython:: python
655669
656-
df = pd.DataFrame({"id": [1, 2, 3, 4, 5, 6],
657-
"raw_grade": ['a', 'b', 'b', 'a', 'a', 'e']})
670+
df = pd.DataFrame(
671+
{"id": [1, 2, 3, 4, 5, 6], "raw_grade": ["a", "b", "b", "a", "a", "e"]}
672+
)
673+
658674
659675
Convert the raw grades to a categorical data type.
660676

@@ -674,8 +690,9 @@ Reorder the categories and simultaneously add the missing categories (methods un
674690

675691
.. ipython:: python
676692
677-
df["grade"] = df["grade"].cat.set_categories(["very bad", "bad", "medium",
678-
"good", "very good"])
693+
df["grade"] = df["grade"].cat.set_categories(
694+
["very bad", "bad", "medium","good", "very good"]
695+
)
679696
df["grade"]
680697
681698
Sorting is per order in the categories, not lexical order.
@@ -705,8 +722,7 @@ We use the standard convention for referencing the matplotlib API:
705722
706723
.. ipython:: python
707724
708-
ts = pd.Series(np.random.randn(1000),
709-
index=pd.date_range('1/1/2000', periods=1000))
725+
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
710726
ts = ts.cumsum()
711727
712728
@savefig series_plot_basic.png
@@ -717,8 +733,10 @@ of the columns with labels:
717733

718734
.. ipython:: python
719735
720-
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index,
721-
columns=['A', 'B', 'C', 'D'])
736+
df = pd.DataFrame(
737+
np.random.randn(1000, 4), index=ts.index, columns=["A", "B", "C", "D"]
738+
)
739+
722740
df = df.cumsum()
723741
724742
plt.figure()

doc/source/user_guide/sparse.rst

+19-16
Original file line numberDiff line numberDiff line change
@@ -303,24 +303,28 @@ The method requires a ``MultiIndex`` with two or more levels.
303303
.. ipython:: python
304304
305305
s = pd.Series([3.0, np.nan, 1.0, 3.0, np.nan, np.nan])
306-
s.index = pd.MultiIndex.from_tuples([(1, 2, 'a', 0),
307-
(1, 2, 'a', 1),
308-
(1, 1, 'b', 0),
309-
(1, 1, 'b', 1),
310-
(2, 1, 'b', 0),
311-
(2, 1, 'b', 1)],
312-
names=['A', 'B', 'C', 'D'])
313-
s
306+
s.index = pd.MultiIndex.from_tuples(
307+
[
308+
(1, 2, "a", 0),
309+
(1, 2, "a", 1),
310+
(1, 1, "b", 0),
311+
(1, 1, "b", 1),
312+
(2, 1, "b", 0),
313+
(2, 1, "b", 1),
314+
],
315+
names=["A", "B", "C", "D"],
316+
)
314317
ss = s.astype('Sparse')
315318
ss
316319
317320
In the example below, we transform the ``Series`` to a sparse representation of a 2-d array by specifying that the first and second ``MultiIndex`` levels define labels for the rows and the third and fourth levels define labels for the columns. We also specify that the column and row labels should be sorted in the final sparse representation.
318321

319322
.. ipython:: python
320323
321-
A, rows, columns = ss.sparse.to_coo(row_levels=['A', 'B'],
322-
column_levels=['C', 'D'],
323-
sort_labels=True)
324+
A, rows, columns = ss.sparse.to_coo(
325+
row_levels=["A", "B"], column_levels=["C", "D"], sort_labels=True
326+
)
327+
324328
325329
A
326330
A.todense()
@@ -331,9 +335,9 @@ Specifying different row and column labels (and not sorting them) yields a diffe
331335

332336
.. ipython:: python
333337
334-
A, rows, columns = ss.sparse.to_coo(row_levels=['A', 'B', 'C'],
335-
column_levels=['D'],
336-
sort_labels=False)
338+
A, rows, columns = ss.sparse.to_coo(
339+
row_levels=["A", "B", "C"], column_levels=["D"], sort_labels=False
340+
)
337341
338342
A
339343
A.todense()
@@ -345,8 +349,7 @@ A convenience method :meth:`Series.sparse.from_coo` is implemented for creating
345349
.. ipython:: python
346350
347351
from scipy import sparse
348-
A = sparse.coo_matrix(([3.0, 1.0, 2.0], ([1, 0, 0], [0, 2, 3])),
349-
shape=(3, 4))
352+
A = sparse.coo_matrix(([3.0, 1.0, 2.0], ([1, 0, 0], [0, 2, 3])), shape=(3, 4))
350353
A
351354
A.todense()
352355

0 commit comments

Comments
 (0)