Skip to content

Commit

Permalink
Update notebooks for pandas 2.0.0, close wesm#170
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Apr 12, 2023
1 parent f1757b8 commit c9f8f20
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion ch06.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@
"source": [
"writer = pd.ExcelWriter(\"examples/ex2.xlsx\")\n",
"frame.to_excel(writer, \"Sheet1\")\n",
"writer.save()"
"writer.close()"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions ch07.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@
"df = pd.DataFrame({\"key\": [\"b\", \"b\", \"a\", \"c\", \"a\", \"b\"],\n",
" \"data1\": range(6)})\n",
"df\n",
"pd.get_dummies(df[\"key\"])"
"pd.get_dummies(df[\"key\"], dtype=float)"
]
},
{
Expand All @@ -565,7 +565,7 @@
"metadata": {},
"outputs": [],
"source": [
"dummies = pd.get_dummies(df[\"key\"], prefix=\"key\")\n",
"dummies = pd.get_dummies(df[\"key\"], prefix=\"key\", dtype=float)\n",
"df_with_dummy = df[[\"data1\"]].join(dummies)\n",
"df_with_dummy"
]
Expand Down Expand Up @@ -1268,7 +1268,7 @@
"metadata": {},
"outputs": [],
"source": [
"pd.get_dummies(cat_s)"
"pd.get_dummies(cat_s, dtype=float)"
]
},
{
Expand Down
27 changes: 18 additions & 9 deletions ch08.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -737,14 +737,23 @@
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"long_data.index.name = None"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"long_data[\"value2\"] = np.random.standard_normal(len(long_data))\n",
"long_data[:10]"
]
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -755,7 +764,7 @@
},
{
"cell_type": "code",
"execution_count": 68,
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -765,14 +774,14 @@
},
{
"cell_type": "code",
"execution_count": 69,
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 70,
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -785,7 +794,7 @@
},
{
"cell_type": "code",
"execution_count": 71,
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -795,7 +804,7 @@
},
{
"cell_type": "code",
"execution_count": 72,
"execution_count": 73,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -806,7 +815,7 @@
},
{
"cell_type": "code",
"execution_count": 73,
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -815,7 +824,7 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -824,7 +833,7 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
Expand Down
4 changes: 2 additions & 2 deletions ch09.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
"ax.set_xlim([\"1/1/2007\", \"1/1/2011\"])\n",
"ax.set_ylim([600, 1800])\n",
"\n",
"ax.set_title(\"Important dates in the 2008-2009 financial crisis\")"
"ax.set_title(\"Important dates in the 2008\u20132009 financial crisis\")"
]
},
{
Expand Down Expand Up @@ -550,7 +550,7 @@
"outputs": [],
"source": [
"ax = sns.regplot(x=\"m1\", y=\"unemp\", data=trans_data)\n",
"ax.title(\"Changes in log(m1) versus log(unemp)\")"
"ax.set_title(\"Changes in log(m1) versus log(unemp)\")"
]
},
{
Expand Down
11 changes: 7 additions & 4 deletions ch10.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"outputs": [],
"source": [
"df = pd.DataFrame({\"key1\" : [\"a\", \"a\", None, \"b\", \"b\", \"a\", None],\n",
" \"key2\" : pd.Series([1, 2, 1, 2, 1, None, 1], dtype=\"Int64\"),\n",
" \"key2\" : pd.Series([1, 2, 1, 2, 1, None, 1],\n",
" dtype=\"Int64\"),\n",
" \"data1\" : np.random.standard_normal(7),\n",
" \"data2\" : np.random.standard_normal(7)})\n",
"df"
Expand Down Expand Up @@ -97,7 +98,7 @@
"outputs": [],
"source": [
"df.groupby(\"key1\").mean()\n",
"df.groupby(\"key2\").mean()\n",
"df.groupby(\"key2\").mean(numeric_only=True)\n",
"df.groupby([\"key1\", \"key2\"]).mean()"
]
},
Expand Down Expand Up @@ -422,7 +423,8 @@
"metadata": {},
"outputs": [],
"source": [
"tips.groupby([\"day\", \"smoker\"], as_index=False).mean()"
"grouped = tips.groupby([\"day\", \"smoker\"], as_index=False)\n",
"grouped.mean(numeric_only=True)"
]
},
{
Expand Down Expand Up @@ -852,7 +854,8 @@
"outputs": [],
"source": [
"tips.head()\n",
"tips.pivot_table(index=[\"day\", \"smoker\"])"
"tips.pivot_table(index=[\"day\", \"smoker\"],\n",
" values=[\"size\", \"tip\", \"tip_pct\", \"total_bill\"])"
]
},
{
Expand Down
5 changes: 3 additions & 2 deletions ch12.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
"metadata": {},
"outputs": [],
"source": [
"dummies = pd.get_dummies(data.category, prefix='category')\n",
"dummies = pd.get_dummies(data.category, prefix='category',\n",
" dtype=float)\n",
"data_with_dummies = data.drop('category', axis=1).join(dummies)\n",
"data_with_dummies"
]
Expand Down Expand Up @@ -137,7 +138,7 @@
"metadata": {},
"outputs": [],
"source": [
"coef, resid, _, _ = np.linalg.lstsq(X, y)"
"coef, resid, _, _ = np.linalg.lstsq(X, y, rcond=None)"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions ch13.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@
"def add_prop(group):\n",
" group[\"prop\"] = group[\"births\"] / group[\"births\"].sum()\n",
" return group\n",
"names = names.groupby([\"year\", \"sex\"]).apply(add_prop)"
"names = names.groupby([\"year\", \"sex\"], group_keys=False).apply(add_prop)"
]
},
{
Expand Down Expand Up @@ -1213,7 +1213,7 @@
" # If no mapping provided, return x\n",
" return emp_mapping.get(x, x)\n",
"\n",
"fec[\"contbr_employer\"] = fec[\"contbr_employer\"].map(f)"
"fec[\"contbr_employer\"] = fec[\"contbr_employer\"].map(get_emp)"
]
},
{
Expand Down

0 comments on commit c9f8f20

Please sign in to comment.