Skip to content

Commit

Permalink
Fix deprecated numpy.NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
hugetim committed Oct 23, 2024
1 parent eeb0264 commit 67715ec
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 38 deletions.
99 changes: 84 additions & 15 deletions nbs/06_pandas.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,61 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[autoreload of fastcore.basics failed: Traceback (most recent call last):\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 276, in check\n",
" superreload(m, reload, self.old_objects)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 500, in superreload\n",
" update_generic(old_obj, new_obj)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 397, in update_generic\n",
" update(a, b)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 365, in update_class\n",
" update_instances(old, new)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 323, in update_instances\n",
" object.__setattr__(ref, \"__class__\", new)\n",
"TypeError: can't apply this __setattr__ to _InfMeta object\n",
"]\n",
"[autoreload of fastcore.foundation failed: Traceback (most recent call last):\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 276, in check\n",
" superreload(m, reload, self.old_objects)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 500, in superreload\n",
" update_generic(old_obj, new_obj)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 397, in update_generic\n",
" update(a, b)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 365, in update_class\n",
" update_instances(old, new)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 323, in update_instances\n",
" object.__setattr__(ref, \"__class__\", new)\n",
"TypeError: can't apply this __setattr__ to _L_Meta object\n",
"]\n",
"[autoreload of fastcore.meta failed: Traceback (most recent call last):\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 276, in check\n",
" superreload(m, reload, self.old_objects)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 500, in superreload\n",
" update_generic(old_obj, new_obj)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 397, in update_generic\n",
" update(a, b)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 365, in update_class\n",
" update_instances(old, new)\n",
" File \"c:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\IPython\\extensions\\autoreload.py\", line 323, in update_instances\n",
" object.__setattr__(ref, \"__class__\", new)\n",
"TypeError: can't apply this __setattr__ to PrePostInitMeta object\n",
"]\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
}
],
"source": [
"#| default_exp pandas\n",
"%load_ext autoreload\n",
Expand Down Expand Up @@ -132,7 +186,7 @@
"source": [
"#| hide\n",
"from fastcore.test import test_eq, test_fail\n",
"import numpy as np"
"from numpy import nan"
]
},
{
Expand All @@ -144,7 +198,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"(U.S. life expectancy, 1900-1940)\n"
"(US life expectancy, 19001940)\n"
]
}
],
Expand Down Expand Up @@ -207,7 +261,7 @@
"source": [
"#| hide\n",
"#| eval: False\n",
"_better_dataframe(sfi.Data, 'le', None, 'sel_var', True, np.NaN)"
"_better_dataframe(sfi.Data, 'le', None, 'sel_var', True, nan)"
]
},
{
Expand Down Expand Up @@ -257,7 +311,7 @@
"source": [
"#| hide\n",
"#| eval: False\n",
"_better_dataframe(sfi.Data, None, range(0, 10), 'sel_var', True, np.NaN)"
"_better_dataframe(sfi.Data, None, range(0, 10), 'sel_var', True, nan)"
]
},
{
Expand Down Expand Up @@ -315,7 +369,7 @@
"source": [
"#| hide\n",
"#| eval: False\n",
"_better_dataframe(sfi.Data, None, range(-31, -1), 'sel_var', True, np.NaN)"
"_better_dataframe(sfi.Data, None, range(-31, -1), 'sel_var', True, nan)"
]
},
{
Expand Down Expand Up @@ -480,7 +534,7 @@
"source": [
"#| export\n",
"def better_dataframe_from_stata(stfr, var, obs, selectvar, valuelabel, missingval, sformat):\n",
" import numpy as np\n",
" from numpy import nan\n",
" import pandas as pd\n",
" import sfi\n",
" hdl = sfi.Data if stfr is None else sfi.Frame.connect(stfr)\n",
Expand All @@ -493,11 +547,11 @@
" df = _better_dataframe(hdl, var, obs, selectvar, valuelabel, missingval)\n",
" if sformat:\n",
" for v in list(df.columns):\n",
" if hdl.isVarTypeString(v) or (valuelabel and missingval==np.NaN\n",
" if hdl.isVarTypeString(v) or (valuelabel and missingval==nan\n",
" and not pd.api.types.is_numeric_dtype(df[v])):\n",
" continue\n",
" v_format = hdl.getVarFormat(v)\n",
" if missingval != np.NaN and not pd.api.types.is_numeric_dtype(df[v]):\n",
" if missingval != nan and not pd.api.types.is_numeric_dtype(df[v]):\n",
" def format_value(x):\n",
" return stata_formatted(x, v_format).lstrip() if type(x)!=str else x\n",
" else:\n",
Expand All @@ -515,9 +569,9 @@
"source": [
"#| export\n",
"def better_pdataframe_from_data(var=None, obs=None, selectvar=None, valuelabel=False, missingval=None, sformat=False):\n",
" import numpy as np\n",
" from numpy import nan\n",
" if missingval is None:\n",
" missingval = np.NaN\n",
" missingval = nan\n",
" return better_dataframe_from_stata(None, var, obs, selectvar, valuelabel, missingval, sformat)"
]
},
Expand All @@ -529,9 +583,9 @@
"source": [
"#| export\n",
"def better_pdataframe_from_frame(stfr, var=None, obs=None, selectvar=None, valuelabel=False, missingval=None, sformat=False):\n",
" import numpy as np\n",
" from numpy import nan\n",
" if missingval is None:\n",
" missingval = np.NaN\n",
" missingval = nan\n",
" return better_dataframe_from_stata(stfr, var, obs, selectvar, valuelabel, missingval, sformat)"
]
},
Expand All @@ -544,7 +598,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"(U.S. life expectancy, 1900-1940)\n",
"(US life expectancy, 19001940)\n",
"(1 real change made, 1 to missing)\n",
"(1 real change made)\n",
"(1 missing value generated)\n"
Expand Down Expand Up @@ -1250,7 +1304,22 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "AttributeError",
"evalue": "module 'nbdev' has no attribute 'export'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[57], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m#| hide\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnbdev\u001b[39;00m; nbdev\u001b[38;5;241m.\u001b[39mnbdev_export()\n",
"File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\fastcore\\script.py:112\u001b[0m, in \u001b[0;36mcall_parse.<locals>._f\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 109\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(func)\n\u001b[0;32m 110\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_f\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[0;32m 111\u001b[0m mod \u001b[38;5;241m=\u001b[39m inspect\u001b[38;5;241m.\u001b[39mgetmodule(inspect\u001b[38;5;241m.\u001b[39mcurrentframe()\u001b[38;5;241m.\u001b[39mf_back)\n\u001b[1;32m--> 112\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m mod: \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 113\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m SCRIPT_INFO\u001b[38;5;241m.\u001b[39mfunc \u001b[38;5;129;01mand\u001b[39;00m mod\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;241m==\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m: SCRIPT_INFO\u001b[38;5;241m.\u001b[39mfunc \u001b[38;5;241m=\u001b[39m func\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\n\u001b[0;32m 114\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(sys\u001b[38;5;241m.\u001b[39margv)\u001b[38;5;241m>\u001b[39m\u001b[38;5;241m1\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m sys\u001b[38;5;241m.\u001b[39margv[\u001b[38;5;241m1\u001b[39m]\u001b[38;5;241m==\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m: sys\u001b[38;5;241m.\u001b[39margv\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;241m1\u001b[39m)\n",
"File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\nbdev\\doclinks.py:149\u001b[0m, in \u001b[0;36mnbdev_export\u001b[1;34m(path, procs, **kwargs)\u001b[0m\n\u001b[0;32m 147\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m procs:\n\u001b[0;32m 148\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnbdev\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexport\u001b[39;00m\n\u001b[1;32m--> 149\u001b[0m procs \u001b[38;5;241m=\u001b[39m \u001b[43m[\u001b[49m\u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mnbdev\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexport\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mp\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mp\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mL\u001b[49m\u001b[43m(\u001b[49m\u001b[43mprocs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\n\u001b[0;32m 150\u001b[0m files \u001b[38;5;241m=\u001b[39m nbglob(path\u001b[38;5;241m=\u001b[39mpath, as_path\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\u001b[38;5;241m.\u001b[39msorted(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 151\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m f \u001b[38;5;129;01min\u001b[39;00m files: nb_export(f, procs\u001b[38;5;241m=\u001b[39mprocs)\n",
"File \u001b[1;32mc:\\Users\\tjhuegerich\\AppData\\Local\\anaconda3\\envs\\nbstata_dev\\Lib\\site-packages\\nbdev\\doclinks.py:149\u001b[0m, in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m 147\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m procs:\n\u001b[0;32m 148\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnbdev\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexport\u001b[39;00m\n\u001b[1;32m--> 149\u001b[0m procs \u001b[38;5;241m=\u001b[39m [\u001b[38;5;28mgetattr\u001b[39m(\u001b[43mnbdev\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexport\u001b[49m, p) \u001b[38;5;28;01mfor\u001b[39;00m p \u001b[38;5;129;01min\u001b[39;00m L(procs)]\n\u001b[0;32m 150\u001b[0m files \u001b[38;5;241m=\u001b[39m nbglob(path\u001b[38;5;241m=\u001b[39mpath, as_path\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\u001b[38;5;241m.\u001b[39msorted(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mname\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 151\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m f \u001b[38;5;129;01min\u001b[39;00m files: nb_export(f, procs\u001b[38;5;241m=\u001b[39mprocs)\n",
"\u001b[1;31mAttributeError\u001b[0m: module 'nbdev' has no attribute 'export'"
]
}
],
"source": [
"#| hide\n",
"import nbdev; nbdev.nbdev_export()"
Expand Down
Loading

0 comments on commit 67715ec

Please sign in to comment.