Skip to content

Commit 3d5aa6a

Browse files
authored
Merge pull request #199 from realpython/pandas-intro-MNL-fix
pandas-intro: fix MNL example
2 parents a95c5ec + 6a8afc3 commit 3d5aa6a

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

pandas-intro/pandas_intro.ipynb

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
"cell_type": "markdown",
2626
"metadata": {},
2727
"source": [
28-
"### Installing Pandas"
28+
"### Installing Dependencies"
2929
]
3030
},
3131
{
3232
"cell_type": "markdown",
3333
"metadata": {},
3434
"source": [
3535
"```console\n",
36-
"$ python -m pip install pandas\n",
36+
"$ python3 -m pip install requests pandas matplotlib\n",
3737
"```"
3838
]
3939
},
@@ -42,7 +42,7 @@
4242
"metadata": {},
4343
"source": [
4444
"```console\n",
45-
"$ conda install pandas\n",
45+
"$ conda install requests pandas matplotlib\n",
4646
"```"
4747
]
4848
},
@@ -197,7 +197,7 @@
197197
"outputs": [],
198198
"source": [
199199
"import numpy as np\n",
200-
"nba.describe(include=np.object)"
200+
"nba.describe(include=object)"
201201
]
202202
},
203203
{
@@ -230,12 +230,11 @@
230230
"source": [
231231
"nba[\"fran_id\"].value_counts()\n",
232232
"# Expected:\n",
233-
"# Name: team_id, Length: 104, dtype: int64\n",
234233
"# Lakers 6024\n",
235234
"# Celtics 5997\n",
236235
"# Knicks 5769\n",
237236
"\n",
238-
"# Huskies 60\n",
237+
"# Falcons 60\n",
239238
"# Name: fran_id, dtype: int64"
240239
]
241240
},
@@ -258,9 +257,18 @@
258257
"metadata": {},
259258
"outputs": [],
260259
"source": [
261-
"nba.loc[nba[\"team_id\"] == \"MNL\", \"date_game\"].min()\n",
260+
"nba[\"date_played\"] = pd.to_datetime(nba[\"date_game\"])"
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": null,
266+
"metadata": {},
267+
"outputs": [],
268+
"source": [
269+
"nba.loc[nba[\"team_id\"] == \"MNL\", \"date_played\"].min()\n",
262270
"# Expected:\n",
263-
"# '1/1/1949'"
271+
"# Timestamp('1948-11-04 00:00:00')"
264272
]
265273
},
266274
{
@@ -269,9 +277,9 @@
269277
"metadata": {},
270278
"outputs": [],
271279
"source": [
272-
"nba.loc[nba[\"team_id\"] == \"MNL\", \"date_game\"].max()\n",
280+
"nba.loc[nba[\"team_id\"] == \"MNL\", \"date_played\"].max()\n",
273281
"# Expected:\n",
274-
"# '4/9/1959'"
282+
"# Timestamp('1960-03-26 00:00:00')"
275283
]
276284
},
277285
{
@@ -280,11 +288,11 @@
280288
"metadata": {},
281289
"outputs": [],
282290
"source": [
283-
"nba.loc[nba[\"team_id\"] == \"MNL\", \"date_game\"].agg((\"min\", \"max\"))\n",
291+
"nba.loc[nba[\"team_id\"] == \"MNL\", \"date_played\"].agg((\"min\", \"max\"))\n",
284292
"# Expected:\n",
285-
"# min 1/1/1949\n",
286-
"# max 4/9/1959\n",
287-
"# Name: date_game, dtype: object"
293+
"# min 1948-11-04\n",
294+
"# max 1960-03-26\n",
295+
"# Name: date_played, dtype: datetime64[ns]"
288296
]
289297
},
290298
{
@@ -475,8 +483,8 @@
475483
"city_data.values\n",
476484
"# Expected:\n",
477485
"# array([[4.2e+03, 5.0e+00],\n",
478-
"# [6.5e+03, 8.0e+00],\n",
479-
"# [8.0e+03, nan]])"
486+
"# [6.5e+03, 8.0e+00],\n",
487+
"# [8.0e+03, nan]])"
480488
]
481489
},
482490
{
@@ -913,7 +921,7 @@
913921
"current_decade = nba[nba[\"year_id\"] > 2010]\n",
914922
"current_decade.shape\n",
915923
"# Expected:\n",
916-
"# (12658, 23)"
924+
"# (12658, 24)"
917925
]
918926
},
919927
{
@@ -925,7 +933,7 @@
925933
"games_with_notes = nba[nba[\"notes\"].notnull()]\n",
926934
"games_with_notes.shape\n",
927935
"# Expected:\n",
928-
"# (5424, 23)"
936+
"# (5424, 24)"
929937
]
930938
},
931939
{
@@ -937,7 +945,7 @@
937945
"ers = nba[nba[\"fran_id\"].str.endswith(\"ers\")]\n",
938946
"ers.shape\n",
939947
"# Expected:\n",
940-
"# (27797, 23)"
948+
"# (27797, 24)"
941949
]
942950
},
943951
{
@@ -1102,7 +1110,7 @@
11021110
"df = nba.copy()\n",
11031111
"df.shape\n",
11041112
"# Expected:\n",
1105-
"# (126314, 23)"
1113+
"# (126314, 24)"
11061114
]
11071115
},
11081116
{
@@ -1121,7 +1129,7 @@
11211129
"df[\"difference\"] = df.pts - df.opp_pts\n",
11221130
"df.shape\n",
11231131
"# Expected:\n",
1124-
"# (126314, 24)"
1132+
"# (126314, 25)"
11251133
]
11261134
},
11271135
{
@@ -1155,16 +1163,17 @@
11551163
"# Expected:\n",
11561164
"# <class 'pandas.core.frame.DataFrame'>\n",
11571165
"# RangeIndex: 126314 entries, 0 to 126313\n",
1158-
"# Data columns (total 24 columns):\n",
1166+
"# Data columns (total 25 columns):\n",
11591167
"# gameorder 126314 non-null int64\n",
11601168
"\n",
11611169
"# location 126314 non-null object\n",
11621170
"# result 126314 non-null object\n",
11631171
"# forecast 126314 non-null float64\n",
11641172
"# notes 5424 non-null object\n",
1173+
"# date_played 126314 non-null datetime64[ns]\n",
11651174
"# difference 126314 non-null int64\n",
1166-
"# dtypes: float64(6), int64(8), object(10)\n",
1167-
"# memory usage: 23.1+ MB"
1175+
"# dtypes: datetime64[ns](1), float64(6), int64(8), object(10)\n",
1176+
"# memory usage: 24.1+ MB"
11681177
]
11691178
},
11701179
{
@@ -1182,7 +1191,7 @@
11821191
"source": [
11831192
"df.shape\n",
11841193
"# Expected:\n",
1185-
"# (126314, 24)"
1194+
"# (126314, 25)"
11861195
]
11871196
},
11881197
{
@@ -1195,7 +1204,7 @@
11951204
"df.drop(elo_columns, inplace=True, axis=1)\n",
11961205
"df.shape\n",
11971206
"# Expected:\n",
1198-
"# (126314, 20)"
1207+
"# (126314, 21)"
11991208
]
12001209
},
12011210
{
@@ -1301,7 +1310,7 @@
13011310
"rows_without_missing_data = nba.dropna()\n",
13021311
"rows_without_missing_data.shape\n",
13031312
"# Expected:\n",
1304-
"# (5424, 23)"
1313+
"# (5424, 24)"
13051314
]
13061315
},
13071316
{
@@ -1313,7 +1322,7 @@
13131322
"data_without_missing_columns = nba.dropna(axis=1)\n",
13141323
"data_without_missing_columns.shape\n",
13151324
"# Expected:\n",
1316-
"# (126314, 22)"
1325+
"# (126314, 23)"
13171326
]
13181327
},
13191328
{
@@ -1541,22 +1550,24 @@
15411550
],
15421551
"metadata": {
15431552
"kernelspec": {
1544-
"argv": [
1545-
"/home/reka/anaconda3/bin/python",
1546-
"-m",
1547-
"ipykernel_launcher",
1548-
"-f",
1549-
"{connection_file}"
1550-
],
15511553
"display_name": "Python 3",
1552-
"env": {},
1553-
"interrupt_mode": "signal",
15541554
"language": "python",
1555-
"metadata": {},
15561555
"name": "python3"
15571556
},
1558-
"language": "python"
1557+
"language": "python",
1558+
"language_info": {
1559+
"codemirror_mode": {
1560+
"name": "ipython",
1561+
"version": 3
1562+
},
1563+
"file_extension": ".py",
1564+
"mimetype": "text/x-python",
1565+
"name": "python",
1566+
"nbconvert_exporter": "python",
1567+
"pygments_lexer": "ipython3",
1568+
"version": "3.9.2"
1569+
}
15591570
},
15601571
"nbformat": 4,
1561-
"nbformat_minor": 2
1572+
"nbformat_minor": 4
15621573
}

0 commit comments

Comments
 (0)