Skip to content

Commit 6fc17fa

Browse files
authored
DOC: Fix code style in documentation (pandas-dev#36780)
1 parent a3da05a commit 6fc17fa

File tree

7 files changed

+94
-70
lines changed

7 files changed

+94
-70
lines changed

doc/source/getting_started/intro_tutorials/06_calculate_statistics.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ aggregating statistics for given columns can be defined using the
122122

123123
.. ipython:: python
124124
125-
titanic.agg({'Age': ['min', 'max', 'median', 'skew'],
126-
'Fare': ['min', 'max', 'median', 'mean']})
125+
titanic.agg(
126+
{"Age": ["min", "max", "median", "skew"], "Fare": ["min", "max", "median", "mean"]}
127+
)
127128
128129
.. raw:: html
129130

doc/source/getting_started/intro_tutorials/07_reshape_table_layout.rst

+19-13
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ measurement.
101101

102102
.. ipython:: python
103103
104-
air_quality = pd.read_csv("data/air_quality_long.csv",
105-
index_col="date.utc", parse_dates=True)
104+
air_quality = pd.read_csv(
105+
"data/air_quality_long.csv", index_col="date.utc", parse_dates=True
106+
)
106107
air_quality.head()
107108
108109
.. raw:: html
@@ -247,8 +248,9 @@ I want the mean concentrations for :math:`NO_2` and :math:`PM_{2.5}` in each of
247248

248249
.. ipython:: python
249250
250-
air_quality.pivot_table(values="value", index="location",
251-
columns="parameter", aggfunc="mean")
251+
air_quality.pivot_table(
252+
values="value", index="location", columns="parameter", aggfunc="mean"
253+
)
252254
253255
In the case of :meth:`~DataFrame.pivot`, the data is only rearranged. When multiple
254256
values need to be aggregated (in this specific case, the values on
@@ -266,9 +268,13 @@ the ``margin`` parameter to ``True``:
266268

267269
.. ipython:: python
268270
269-
air_quality.pivot_table(values="value", index="location",
270-
columns="parameter", aggfunc="mean",
271-
margins=True)
271+
air_quality.pivot_table(
272+
values="value",
273+
index="location",
274+
columns="parameter",
275+
aggfunc="mean",
276+
margins=True,
277+
)
272278
273279
.. raw:: html
274280

@@ -345,12 +351,12 @@ The :func:`pandas.melt` method can be defined in more detail:
345351

346352
.. ipython:: python
347353
348-
no_2 = no2_pivoted.melt(id_vars="date.utc",
349-
value_vars=["BETR801",
350-
"FR04014",
351-
"London Westminster"],
352-
value_name="NO_2",
353-
var_name="id_location")
354+
no_2 = no2_pivoted.melt(
355+
id_vars="date.utc",
356+
value_vars=["BETR801", "FR04014", "London Westminster"],
357+
value_name="NO_2",
358+
var_name="id_location",
359+
)
354360
no_2.head()
355361
356362
The result in the same, but in more detail defined:

doc/source/getting_started/intro_tutorials/08_combine_dataframes.rst

+2-4
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ index. For example:
155155

156156
.. ipython:: python
157157
158-
air_quality_ = pd.concat([air_quality_pm25, air_quality_no2],
159-
keys=["PM25", "NO2"])
158+
air_quality_ = pd.concat([air_quality_pm25, air_quality_no2], keys=["PM25", "NO2"])
160159
161160
.. ipython:: python
162161
@@ -233,8 +232,7 @@ Add the station coordinates, provided by the stations metadata table, to the cor
233232
234233
.. ipython:: python
235234
236-
air_quality = pd.merge(air_quality, stations_coord,
237-
how='left', on='location')
235+
air_quality = pd.merge(air_quality, stations_coord, how="left", on="location")
238236
air_quality.head()
239237
240238
Using the :meth:`~pandas.merge` function, for each of the rows in the

doc/source/getting_started/intro_tutorials/09_timeseries.rst

+3-4
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,9 @@ Plot the typical :math:`NO_2` pattern during the day of our time series of all s
204204
.. ipython:: python
205205
206206
fig, axs = plt.subplots(figsize=(12, 4))
207-
air_quality.groupby(
208-
air_quality["datetime"].dt.hour)["value"].mean().plot(kind='bar',
209-
rot=0,
210-
ax=axs)
207+
air_quality.groupby(air_quality["datetime"].dt.hour)["value"].mean().plot(
208+
kind='bar', rot=0, ax=axs
209+
)
211210
plt.xlabel("Hour of the day"); # custom x label using matplotlib
212211
@savefig 09_bar_chart.png
213212
plt.ylabel("$NO_2 (µg/m^3)$");

doc/source/getting_started/intro_tutorials/10_text_data.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ In the "Sex" column, replace values of "male" by "M" and values of "female" by "
224224

225225
.. ipython:: python
226226
227-
titanic["Sex_short"] = titanic["Sex"].replace({"male": "M",
228-
"female": "F"})
227+
titanic["Sex_short"] = titanic["Sex"].replace({"male": "M", "female": "F"})
229228
titanic["Sex_short"]
230229
231230
Whereas :meth:`~Series.replace` is not a string method, it provides a convenient way

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)