Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b0ecef

Browse files
committed
Add Testing with Asserts notes
1 parent c3c46d2 commit 1b0ecef

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

‎Cleaning_Data_in_Python/Cleaning_Data_for_Analysis.ipynb

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,11 +854,80 @@
854854
"df.tail()"
855855
]
856856
},
857+
{
858+
"cell_type": "markdown",
859+
"metadata": {},
860+
"source": [
861+
"## Testing with Asserts\n",
862+
"* Use Asserts to Programmatically vs visually checking\n",
863+
"* If we drop or fill NaNs, we expect 0 missing values\n",
864+
"* We can write an assert statement to verify this\n",
865+
"* We can detect early warnings and errors\n",
866+
"* This gives us confidence that our code is running correctly\n",
867+
"\n",
868+
"### How Asserts work"
869+
]
870+
},
871+
{
872+
"cell_type": "code",
873+
"execution_count": 15,
874+
"metadata": {},
875+
"outputs": [
876+
{
877+
"ename": "AssertionError",
878+
"evalue": "",
879+
"output_type": "error",
880+
"traceback": [
881+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
882+
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
883+
"\u001b[1;32m<ipython-input-15-ca525b181386>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32massert\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;31m# If false, returns an error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
884+
"\u001b[1;31mAssertionError\u001b[0m: "
885+
]
886+
}
887+
],
888+
"source": [
889+
"# If true, nothing is returned\n",
890+
"assert 1 == 1\n",
891+
"# If false, returns an error\n",
892+
"assert 1 == 2"
893+
]
894+
},
857895
{
858896
"cell_type": "code",
859-
"execution_count": null,
897+
"execution_count": 16,
898+
"metadata": {},
899+
"outputs": [
900+
{
901+
"ename": "AssertionError",
902+
"evalue": "",
903+
"output_type": "error",
904+
"traceback": [
905+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
906+
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
907+
"\u001b[1;32m<ipython-input-16-0e3e9719daf4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;31m# 1st .all() to return True or False for each column\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;31m# 2nd .alll() to return a single True or False\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnotnull\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
908+
"\u001b[1;31mAssertionError\u001b[0m: "
909+
]
910+
}
911+
],
912+
"source": [
913+
"# Note: Need to chain two .all(), \n",
914+
"# 1st .all() to return True or False for each column\n",
915+
"# 2nd .alll() to return a single True or False\n",
916+
"assert df.notnull().all().all()"
917+
]
918+
},
919+
{
920+
"cell_type": "code",
921+
"execution_count": 17,
860922
"metadata": {},
861923
"outputs": [],
924+
"source": [
925+
"assert (df['Age'] >=0).all().all()"
926+
]
927+
},
928+
{
929+
"cell_type": "markdown",
930+
"metadata": {},
862931
"source": []
863932
}
864933
],

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /