Data_editor error with int or float column names

Summary

Hi! I’m trying to use the data_editor on a dataframe with column names which are integers. Upon editing the dataframe in the app, a key error is thrown. It seems like the problem is that st.session_state stores the column name as a string rather than an int, though it’s entirely possible I’m doing something foolish.

Steps to reproduce

names = [‘John’,‘Sarah’,‘Jane’]
years = list(range(1,4))
distances = pd.DataFrame(0,names,years)
new_distances = st.data_editor(distances,key=‘df’)

Expected behavior:

Editable dataframe in app

Actual behavior:

Upon editing a cell in the dataframe in the app the following error is thrown:

File “C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py”, line 552, in _run_script
exec(code, module.dict)
File “X:\st_test\test_interface.py”, line 20, in
new_distances = st.data_editor(distances,key=‘df’)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\runtime\metrics_util.py”, line 356, in wrapped_func
result = non_optional_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\elements\data_editor.py”, line 836, in data_editor
_apply_dataframe_edits(data_df, widget_state.value, dataframe_schema)
File “C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\elements\data_editor.py”, line 359, in _apply_dataframe_edits
_apply_cell_edits(df, data_editor_state[“edited_rows”], dataframe_schema)
File “C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\streamlit\elements\data_editor.py”, line 260, in _apply_cell_edits
col_pos = df.columns.get_loc(col_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\indexes\base.py”, line 3654, in get_loc
raise KeyError(key) from err

Debug info

  • Streamlit version: 1.24.0
  • Python version: 3.11.4

Hi @jcadams :wave:

Thank you for reporting this issue! I was able to repro the behavior and it seems like a bug to me. As such, I’ve submitted an issue on Streamlit’s GitHub repo:

Please upvote/thumbs up the issue if you’d like to see a fix prioritized.

Happy Streamlit-ing! :balloon:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.