Skip to content

Incorrect stack traceback when try except finally is used in jupyter notebooks with pandas #7242

Closed

Description

[✓] I checked the documentation and found no answer
[✓] I checked to make sure that this issue has not already been filed
[✓] I'm reporting the issue to the correct repository

Description

The stack traceback of some errors is displayed incorrectly. Below are two examples of an incorrect stacktrace and correct stacktrace. The incorrect one creates a pandas dataframe in a faulty manner, raising an exception, its stacktrace points to a command in the finally block instead of the pandas dataframe creation which is the actual issue.

The correct example shows how a non-existing key of a dictionary is called, its stacktrace does not point to a statement in the finally block but rather points to the line that actually causes the issue.

Some things i've tried or noticed:

  • the correct example contains hyperlinks to each code-line, while the incorrect one doesn't, maybe this is a hint to the cause. I therefore included the stacktrace of both examples in raw text as well as html.
  • running the'incorrect stacktrace' example in pure python (.py file) results in a correct stacktrace
  • this issue occurs when hosting jupyter notebooks locally, but also in VSCode notebooks using the ipykernel
  • Thinking that C-code extensions of Python could be a potential source, I also tried raising errors using torch (torch.tensor('invalid_type') but this results in a correct stacktrace.
  • aside of torch i also tried incorrectly initiating classes with other packages, this also lead to a correct stacktrace

incorrect stacktrace

code

import pandas as pd

try:
    pd.DataFrame(1)
except:
    print("except")
    raise
finally:
    print("finally")

stacktrace text

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/j4/hhybm8xx3pb7nnmwfptdqng00000gp/T/ipykernel_40674/1767429816.py in ?()
      5 except:
      6     print("except")
      7     raise
      8 finally:
----> 9     print("finally")

~/Documents/projects/notebook-stacktrace-bug/.venv/lib/python3.12/site-packages/pandas/core/frame.py in ?(self, data, index, columns, dtype, copy)
    871                 )
    872         # For data is scalar
    873         else:
    874             if index is None or columns is None:
--> 875                 raise ValueError("DataFrame constructor not properly called!")
    876 
    877             index = ensure_index(index)
    878             columns = ensure_index(columns)

ValueError: DataFrame constructor not properly called!

stacktrace html

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[/var/folders/j4/hhybm8xx3pb7nnmwfptdqng00000gp/T/ipykernel_40674/1767429816.py](https://file+.vscode-resource.vscode-cdn.net/var/folders/j4/hhybm8xx3pb7nnmwfptdqng00000gp/T/ipykernel_40674/1767429816.py) in ?()
      5 except:
      6     print("except")
      7     raise
      8 finally:
----> 9     print("finally")

[~/Documents/projects/notebook-stacktrace-bug/.venv/lib/python3.12/site-packages/pandas/core/frame.py](https://file+.vscode-resource.vscode-cdn.net/Users/dvorst/Documents/projects/notebook-stacktrace-bug/~/Documents/projects/notebook-stacktrace-bug/.venv/lib/python3.12/site-packages/pandas/core/frame.py) in ?(self, data, index, columns, dtype, copy)
    871                 )
    872         # For data is scalar
    873         else:
    874             if index is None or columns is None:
--> 875                 raise ValueError("DataFrame constructor not properly called!")
    876 
    877             index = ensure_index(index)
    878             columns = ensure_index(columns)

ValueError: DataFrame constructor not properly called!

correct stacktrace

code

some_dict = {}
try:
    some_dict['non-existent-key']
except:
    print("except")
    raise
finally:
    print("finally")

stacktrace text

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[6], line 3
      1 some_dict = {}
      2 try:
----> 3     some_dict['non-existent-key']
      4 except:
      5     print("except")

KeyError: 'non-existent-key'

stacktrace html

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[6], [line 3](vscode-notebook-cell:?execution_count=6&line=3)
      [1](vscode-notebook-cell:?execution_count=6&line=1) some_dict = {}
      [2](vscode-notebook-cell:?execution_count=6&line=2) try:
----> [3](vscode-notebook-cell:?execution_count=6&line=3)     some_dict['non-existent-key']
      [4](vscode-notebook-cell:?execution_count=6&line=4) except:
      [5](vscode-notebook-cell:?execution_count=6&line=5)     print("except")

KeyError: 'non-existent-key'

Context

This occurs in the browser with notebook 7.0.7, but also in VSCode notebooks with ipykernel 6.29.2

  • Operating System and version: macOS 14.2
  • Browser and version: Firefox 122.0.1 though this shouldn't matter as the issue also occurs in VSCode

IPython : 8.21.0
ipykernel : 6.29.2
ipywidgets : not installed
jupyter_client : 8.6.0
jupyter_core : 5.7.1
jupyter_server : 2.12.5
jupyterlab : 4.1.0
nbclient : 0.9.0
nbconvert : 7.16.0
nbformat : 5.9.2
notebook : 7.0.7
qtconsole : not installed
traitlets : 5.14.1

pandas : 2.2.0
vscode. : 1.86.0

Troubleshoot Output

[notice] A new release of pip is available: 23.3.2 -> 24.0
[notice] To update, run: pip install --upgrade pip
$PATH:
/Users/dvorst/Documents/projects/notebook-stacktrace-bug/.venv/bin
/opt/homebrew/bin
/opt/homebrew/sbin
/usr/local/bin
/System/Cryptexes/App/usr/bin
/usr/bin
/bin
/usr/sbin
/sbin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin
/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
/opt/homebrew/bin
/opt/homebrew/sbin
/Users/dvorst/.local/bin
/Users/dvorst/.local/bin

sys.path:
/Users/dvorst/Documents/projects/notebook-stacktrace-bug/.venv/bin
/opt/homebrew/Cellar/python@3.12/3.12.1/Frameworks/Python.framework/Versions/3.12/lib/python312.zip
/opt/homebrew/Cellar/python@3.12/3.12.1/Frameworks/Python.framework/Versions/3.12/lib/python3.12
/opt/homebrew/Cellar/python@3.12/3.12.1/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload
/Users/dvorst/Documents/projects/notebook-stacktrace-bug/.venv/lib/python3.12/site-packages

sys.executable:
/Users/dvorst/Documents/projects/notebook-stacktrace-bug/.venv/bin/python

sys.version:
3.12.1 (main, Dec 7 2023, 20:45:44) [Clang 15.0.0 (clang-1500.0.40.1)]

platform.platform():
macOS-14.2-arm64-arm-64bit

which -a jupyter:
/Users/dvorst/Documents/projects/notebook-stacktrace-bug/.venv/bin/jupyter

pip list:
Package Version
------------------------- ---------------
anyio 4.2.0
appnope 0.1.4
argon2-cffi 23.1.0
argon2-cffi-bindings 21.2.0
arrow 1.3.0
asttokens 2.4.1
async-lru 2.0.4
attrs 23.2.0
Babel 2.14.0
beautifulsoup4 4.12.3
bleach 6.1.0
certifi 2024.2.2
cffi 1.16.0
charset-normalizer 3.3.2
comm 0.2.1
debugpy 1.8.0
decorator 5.1.1
defusedxml 0.7.1
executing 2.0.1
fastjsonschema 2.19.1
fqdn 1.5.1
h11 0.14.0
httpcore 1.0.2
httpx 0.26.0
idna 3.6
ipykernel 6.29.2
ipython 8.21.0
isoduration 20.11.0
jedi 0.19.1
Jinja2 3.1.3
json5 0.9.14
jsonpointer 2.4
jsonschema 4.21.1
jsonschema-specifications 2023.12.1
jupyter_client 8.6.0
jupyter_core 5.7.1
jupyter-events 0.9.0
jupyter-lsp 2.2.2
jupyter_server 2.12.5
jupyter_server_terminals 0.5.2
jupyterlab 4.1.0
jupyterlab_pygments 0.3.0
jupyterlab_server 2.25.2
MarkupSafe 2.1.5
matplotlib-inline 0.1.6
mistune 3.0.2
nbclient 0.9.0
nbconvert 7.16.0
nbformat 5.9.2
nest-asyncio 1.6.0
notebook 7.0.7
notebook_shim 0.2.3
numpy 1.26.4
overrides 7.7.0
packaging 23.2
pandas 2.2.0
pandocfilters 1.5.1
parso 0.8.3
pexpect 4.9.0
pip 23.3.2
platformdirs 4.2.0
prometheus-client 0.19.0
prompt-toolkit 3.0.43
psutil 5.9.8
ptyprocess 0.7.0
pure-eval 0.2.2
pycparser 2.21
Pygments 2.17.2
python-dateutil 2.8.2
python-json-logger 2.0.7
pytz 2024.1
PyYAML 6.0.1
pyzmq 25.1.2
referencing 0.33.0
requests 2.31.0
rfc3339-validator 0.1.4
rfc3986-validator 0.1.1
rpds-py 0.17.1
Send2Trash 1.8.2
six 1.16.0
sniffio 1.3.0
soupsieve 2.5
stack-data 0.6.3
terminado 0.18.0
tinycss2 1.2.1
tornado 6.4
traitlets 5.14.1
types-python-dateutil 2.8.19.20240106
tzdata 2023.4
uri-template 1.3.0
urllib3 2.2.0
wcwidth 0.2.13
webcolors 1.13
webencodings 0.5.1
websocket-client 1.7.0

I did not include the command line output as this caused the maximum amount of characters allowed to be exceeded.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions