Important
friendly-pandas now needs to be run with friendly_traceback version 0.5.57.
Starting example
The content of the file example.py is as follows:
import pandas as pd
df = pd.DataFrame([[10, 20, 30], [40, 50, 60]],
index=list("ab"),
columns=list("xyz"))
Here's a sample session PRIOR TO A FEW CHANGES NOTED BELOW:
> python -m friendly
friendly-traceback: 0.5.54
friendly: 0.5.39
Python: 3.10.6
Type 'Friendly' for help on special functions/methods.
[1]: from friendly_pandas import register
[2]: from example import df
[3]: df.loc["y"]
Traceback (most recent call last):
File "LOCAL:\pandas\core\indexes\base.py", line 3629, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'y'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
Code block [3], line 1
df.loc["y"]
KeyError: 'y'
To retrieve a column, just use square brackets: df["y "]
[4]: df.loc["w"]
Traceback (most recent call last):
File "LOCAL:\pandas\core\indexes\base.py", line 3629, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'w'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
Code block [4], line 1
df.loc["w"]
KeyError: 'w'
[5]: why()
You tried to retrieve an unknown row. The valid values are: a, b.
[6]: where()
Exception raised on line 1 of code block [4].
> 1| df.loc["w"]
df: x y z
a 10 20 30
b 40 50 60
Note: since I used Friendly, the highlighting of the problematic code is done using a red background, which is not reproduced in the example above.
Instead of trying only something like df.loc[...], one can try to do an assignment; something like
It will still work.
Chained exception
Note that the traceback not only includes code from the user but also a notice that another exception was raised, this one entirely inside Pandas' code. This information is not useful to someone just learning Python (or anyone else really, except perhaps pandas developers?) and it would make sense to not include it. This was done in a later version as noted below.
Important
friendly-pandas now needs to be run with friendly_traceback version 0.5.57.
Starting example
The content of the file
example.pyis as follows:Here's a sample session PRIOR TO A FEW CHANGES NOTED BELOW:
Note: since I used Friendly, the highlighting of the problematic code is done using a red background, which is not reproduced in the example above.
Instead of trying only something like
df.loc[...], one can try to do an assignment; something likeIt will still work.
Chained exception
Note that the traceback not only includes code from the user but also a notice that another exception was raised, this one entirely inside Pandas' code. This information is not useful to someone just learning Python (or anyone else really, except perhaps pandas developers?) and it would make sense to not include it. This was done in a later version as noted below.