Skip to content

Commit e30dea1

Browse files
committed
ipython build imports in rst files
1 parent c71cc03 commit e30dea1

22 files changed

+62
-62
lines changed

doc/source/getting_started/comparison/comparison_with_sas.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ SAS provides ``PROC IMPORT`` to read csv data into a data set.
110110
The pandas method is :func:`read_csv`, which works similarly.
111111

112112
.. ipython:: python
113-
113+
import pandas as pd
114114
url = (
115115
"https://raw.githubusercontent.com/pandas-dev/"
116116
"pandas/main/pandas/tests/io/data/csv/tips.csv"
@@ -523,7 +523,7 @@ the first entry for each.
523523
In pandas this would be written as:
524524

525525
.. ipython:: python
526-
526+
import pandas as pd
527527
tips.groupby(["sex", "smoker"]).first()
528528
529529

doc/source/getting_started/comparison/comparison_with_spreadsheets.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ dataset from the pandas tests, which is a CSV file. In Excel, you would download
9898
In pandas, you pass the URL or local path of the CSV file to :func:`~pandas.read_csv`:
9999

100100
.. ipython:: python
101-
101+
import pandas as pd
102102
url = (
103103
"https://raw.githubusercontent.com/pandas-dev"
104104
"/pandas/main/pandas/tests/io/data/csv/tips.csv"
@@ -379,7 +379,7 @@ entering the first two or three values and then dragging.
379379
This can be achieved by creating a series and assigning it to the desired cells.
380380

381381
.. ipython:: python
382-
382+
import pandas as pd
383383
df = pd.DataFrame({"AAA": [1] * 8, "BBB": list(range(0, 8))})
384384
df
385385
@@ -397,7 +397,7 @@ Excel has built-in functionality for `removing duplicate values <https://support
397397
This is supported in pandas via :meth:`~DataFrame.drop_duplicates`.
398398

399399
.. ipython:: python
400-
400+
import pandas as pd
401401
df = pd.DataFrame(
402402
{
403403
"class": ["A", "A", "A", "B", "C", "D"],
@@ -426,7 +426,7 @@ In Excel, we use the following configuration for the PivotTable:
426426
The equivalent in pandas:
427427

428428
.. ipython:: python
429-
429+
import pandas as pd
430430
pd.pivot_table(
431431
tips, values="tip", index=["size"], columns=["sex"], aggfunc=np.average
432432
)
@@ -438,7 +438,7 @@ Adding a row
438438
Assuming we are using a :class:`~pandas.RangeIndex` (numbered ``0``, ``1``, etc.), we can use :func:`concat` to add a row to the bottom of a ``DataFrame``.
439439

440440
.. ipython:: python
441-
441+
import pandas as pd
442442
df
443443
new_row = pd.DataFrame([["E", 51, True]],
444444
columns=["class", "student_count", "all_pass"])
@@ -453,13 +453,13 @@ takes you to cells that match, one by one. In pandas, this operation is generall
453453
entire column or ``DataFrame`` at once through :ref:`conditional expressions <10min_tut_03_subset.rows_and_columns>`.
454454

455455
.. ipython:: python
456-
456+
import pandas as pd
457457
tips
458458
tips == "Sun"
459459
tips["day"].str.contains("S")
460460
461461
pandas' :meth:`~DataFrame.replace` is comparable to Excel's ``Replace All``.
462462

463463
.. ipython:: python
464-
464+
import pandas as pd
465465
tips.replace("Thu", "Thursday")

doc/source/getting_started/comparison/comparison_with_sql.rst

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ the data into a DataFrame called ``tips`` and assume we have a database table of
1515
structure.
1616

1717
.. ipython:: python
18-
18+
import pandas as pd
1919
url = (
2020
"https://raw.githubusercontent.com/pandas-dev"
2121
"/pandas/main/pandas/tests/io/data/csv/tips.csv"
@@ -43,7 +43,7 @@ to select all columns):
4343
With pandas, column selection is done by passing a list of column names to your DataFrame:
4444

4545
.. ipython:: python
46-
46+
import pandas as pd
4747
tips[["total_bill", "tip", "smoker", "time"]]
4848
4949
Calling the DataFrame without the list of column names would display all columns (akin to SQL's
@@ -59,7 +59,7 @@ In SQL, you can add a calculated column:
5959
With pandas, you can use the :meth:`DataFrame.assign` method of a DataFrame to append a new column:
6060

6161
.. ipython:: python
62-
62+
import pandas as pd
6363
tips.assign(tip_rate=tips["tip"] / tips["total_bill"])
6464
6565
WHERE
@@ -86,7 +86,7 @@ Tips of more than $5 at Dinner meals:
8686
WHERE time = 'Dinner' AND tip > 5.00;
8787
8888
.. ipython:: python
89-
89+
import pandas as pd
9090
tips[(tips["time"] == "Dinner") & (tips["tip"] > 5.00)]
9191
9292
Tips by parties of at least 5 diners OR bill total was more than $45:
@@ -98,14 +98,14 @@ Tips by parties of at least 5 diners OR bill total was more than $45:
9898
WHERE size >= 5 OR total_bill > 45;
9999
100100
.. ipython:: python
101-
101+
import pandas as pd
102102
tips[(tips["size"] >= 5) | (tips["total_bill"] > 45)]
103103
104104
NULL checking is done using the :meth:`~pandas.Series.notna` and :meth:`~pandas.Series.isna`
105105
methods.
106106

107107
.. ipython:: python
108-
108+
import pandas as pd
109109
frame = pd.DataFrame(
110110
{"col1": ["A", "B", np.nan, "C", "D"], "col2": ["F", np.nan, "G", "H", "I"]}
111111
)
@@ -133,7 +133,7 @@ Getting items where ``col1`` IS NOT NULL can be done with :meth:`~pandas.Series.
133133
WHERE col1 IS NOT NULL;
134134
135135
.. ipython:: python
136-
136+
import pandas as pd
137137
frame[frame["col1"].notna()]
138138
139139
@@ -170,14 +170,14 @@ Notice that in the pandas code we used :meth:`.DataFrameGroupBy.size` and not
170170
the number of ``NOT NULL`` records within each.
171171

172172
.. ipython:: python
173-
173+
import pandas as pd
174174
tips.groupby("sex").count()
175175
176176
Alternatively, we could have applied the :meth:`.DataFrameGroupBy.count` method
177177
to an individual column:
178178

179179
.. ipython:: python
180-
180+
import pandas as pd
181181
tips.groupby("sex")["total_bill"].count()
182182
183183
Multiple functions can also be applied at once. For instance, say we'd like to see how tip amount
@@ -197,7 +197,7 @@ to your grouped DataFrame, indicating which functions to apply to specific colum
197197
*/
198198
199199
.. ipython:: python
200-
200+
import pandas as pd
201201
tips.groupby("day").agg({"tip": "mean", "day": "size"})
202202
203203
Grouping by more than one column is done by passing a list of columns to the
@@ -221,7 +221,7 @@ Grouping by more than one column is done by passing a list of columns to the
221221
*/
222222
223223
.. ipython:: python
224-
224+
import pandas as pd
225225
tips.groupby(["smoker", "day"]).agg({"tip": ["size", "mean"]})
226226
227227
.. _compare_with_sql.join:
@@ -240,7 +240,7 @@ parameters allowing you to specify the type of join to perform (``LEFT``, ``RIGH
240240
join behaviour and can lead to unexpected results.
241241

242242
.. ipython:: python
243-
243+
import pandas as pd
244244
df1 = pd.DataFrame({"key": ["A", "B", "C", "D"], "value": np.random.randn(4)})
245245
df2 = pd.DataFrame({"key": ["B", "D", "D", "E"], "value": np.random.randn(4)})
246246
@@ -258,15 +258,15 @@ INNER JOIN
258258
ON df1.key = df2.key;
259259
260260
.. ipython:: python
261-
261+
import pandas as pd
262262
# merge performs an INNER JOIN by default
263263
pd.merge(df1, df2, on="key")
264264
265265
:meth:`~pandas.merge` also offers parameters for cases when you'd like to join one DataFrame's
266266
column with another DataFrame's index.
267267

268268
.. ipython:: python
269-
269+
import pandas as pd
270270
indexed_df2 = df2.set_index("key")
271271
pd.merge(df1, indexed_df2, left_on="key", right_index=True)
272272
@@ -283,7 +283,7 @@ Show all records from ``df1``.
283283
ON df1.key = df2.key;
284284
285285
.. ipython:: python
286-
286+
import pandas as pd
287287
pd.merge(df1, df2, on="key", how="left")
288288
289289
RIGHT JOIN
@@ -299,7 +299,7 @@ Show all records from ``df2``.
299299
ON df1.key = df2.key;
300300
301301
.. ipython:: python
302-
302+
import pandas as pd
303303
pd.merge(df1, df2, on="key", how="right")
304304
305305
FULL JOIN
@@ -327,7 +327,7 @@ UNION
327327
``UNION ALL`` can be performed using :meth:`~pandas.concat`.
328328

329329
.. ipython:: python
330-
330+
import pandas as pd
331331
df1 = pd.DataFrame(
332332
{"city": ["Chicago", "San Francisco", "New York City"], "rank": range(1, 4)}
333333
)
@@ -353,7 +353,7 @@ UNION
353353
*/
354354
355355
.. ipython:: python
356-
356+
import pandas as pd
357357
pd.concat([df1, df2])
358358
359359
SQL's ``UNION`` is similar to ``UNION ALL``, however ``UNION`` will remove duplicate rows.
@@ -379,7 +379,7 @@ In pandas, you can use :meth:`~pandas.concat` in conjunction with
379379
:meth:`~pandas.DataFrame.drop_duplicates`.
380380

381381
.. ipython:: python
382-
382+
import pandas as pd
383383
pd.concat([df1, df2]).drop_duplicates()
384384
385385
@@ -392,7 +392,7 @@ LIMIT
392392
LIMIT 10;
393393
394394
.. ipython:: python
395-
395+
import pandas as pd
396396
tips.head(10)
397397
398398
@@ -410,7 +410,7 @@ Top n rows with offset
410410
LIMIT 10 OFFSET 5;
411411
412412
.. ipython:: python
413-
413+
import pandas as pd
414414
tips.nlargest(10 + 5, columns="tip").tail(10)
415415
416416
Top n rows per group
@@ -430,7 +430,7 @@ Top n rows per group
430430
431431
432432
.. ipython:: python
433-
433+
import pandas as pd
434434
(
435435
tips.assign(
436436
rn=tips.sort_values(["total_bill"], ascending=False)
@@ -445,7 +445,7 @@ Top n rows per group
445445
the same using ``rank(method='first')`` function
446446

447447
.. ipython:: python
448-
448+
import pandas as pd
449449
(
450450
tips.assign(
451451
rnk=tips.groupby(["day"])["total_bill"].rank(
@@ -475,7 +475,7 @@ Notice that when using ``rank(method='min')`` function
475475
(as Oracle's ``RANK()`` function)
476476

477477
.. ipython:: python
478-
478+
import pandas as pd
479479
(
480480
tips[tips["tip"] < 2]
481481
.assign(rnk_min=tips.groupby(["sex"])["tip"].rank(method="min"))
@@ -494,7 +494,7 @@ UPDATE
494494
WHERE tip < 2;
495495
496496
.. ipython:: python
497-
497+
import pandas as pd
498498
tips.loc[tips["tip"] < 2, "tip"] *= 2
499499
500500
DELETE
@@ -508,5 +508,5 @@ DELETE
508508
In pandas we select the rows that should remain instead of deleting the rows that should be removed:
509509

510510
.. ipython:: python
511-
511+
import pandas as pd
512512
tips = tips.loc[tips["tip"] <= 9]

doc/source/getting_started/comparison/includes/case.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ The equivalent pandas methods are :meth:`Series.str.upper`, :meth:`Series.str.lo
22
:meth:`Series.str.title`.
33

44
.. ipython:: python
5-
5+
import pandas as pd
66
firstlast = pd.DataFrame({"string": ["John Smith", "Jane Cook"]})
77
firstlast["upper"] = firstlast["string"].str.upper()
88
firstlast["lower"] = firstlast["string"].str.lower()

doc/source/getting_started/comparison/includes/column_operations.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pandas provides vectorized operations by specifying the individual ``Series`` in
33
a column from the ``DataFrame``.
44

55
.. ipython:: python
6-
6+
import pandas as pd
77
tips["total_bill"] = tips["total_bill"] - 2
88
tips["new_bill"] = tips["total_bill"] / 2
99
tips

doc/source/getting_started/comparison/includes/column_selection.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ Keep certain columns
44
''''''''''''''''''''
55

66
.. ipython:: python
7-
7+
import pandas as pd
88
tips[["sex", "total_bill", "tip"]]
99
1010
Drop a column
1111
'''''''''''''
1212

1313
.. ipython:: python
14-
14+
import pandas as pd
1515
tips.drop("sex", axis=1)
1616
1717
Rename a column
1818
'''''''''''''''
1919

2020
.. ipython:: python
21-
21+
import pandas as pd
2222
tips.rename(columns={"total_bill": "total_bill_2"})

doc/source/getting_started/comparison/includes/construct_dataframe.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ a Python dictionary, where the keys are the column names
44
and the values are the data.
55

66
.. ipython:: python
7-
7+
import pandas as pd
88
df = pd.DataFrame({"x": [1, 3, 5], "y": [2, 4, 6]})
99
df

doc/source/getting_started/comparison/includes/extract_substring.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ from a string by position locations. Keep in mind that Python
33
indexes are zero-based.
44

55
.. ipython:: python
6-
6+
import pandas as pd
77
tips["sex"].str[0:1]

doc/source/getting_started/comparison/includes/filtering.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ DataFrames can be filtered in multiple ways; the most intuitive of which is usin
22
:ref:`boolean indexing <indexing.boolean>`.
33

44
.. ipython:: python
5-
5+
import pandas as pd
66
tips[tips["total_bill"] > 10]
77
88
The above statement is simply passing a ``Series`` of ``True``/``False`` objects to the DataFrame,
99
returning all rows with ``True``.
1010

1111
.. ipython:: python
12-
12+
import pandas as pd
1313
is_dinner = tips["time"] == "Dinner"
1414
is_dinner
1515
is_dinner.value_counts()

doc/source/getting_started/comparison/includes/find_substring.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ method returns its position. If not found, it returns ``-1``. Keep in mind that
44
zero-based.
55

66
.. ipython:: python
7-
7+
import pandas as pd
88
tips["sex"].str.find("ale")

0 commit comments

Comments
 (0)