Checklist
Summary
While working with st.data_editor I noticed an issue with number columns in combination with storing the returned in st.session_state.
The issue arises when the user first inputs integer values and stores the dataframe to session state, but wants to change the values floats later on. When the st.data_editor infers the datatype of the column from the data passed to it, it sees only integer values and incorrectly assumes an integer column. This prevents the user from typing a decimal sign in the second run.
Adding a NumberColumn config with a float format does not help.
Reproducible Code Example
import pandas as pd
import streamlit as st
def btn_on_click():
st.session_state.df_input = df_result
st.session_state.setdefault('df_input', pd.DataFrame(data=[None, None], columns=['number']))
df_result = st.data_editor(
st.session_state.df_input,
column_config={'number': st.column_config.NumberColumn(format='%.1f')},
)
st.button('store df to session state', on_click=btn_on_click)
Steps To Reproduce
- Type integer values into the two fields of the data editor.
Note, that at this point you are still able to input float values!
- With the integer values in the data editor, hit the button
- Now try to input float values.
The data editor does not accept the decimal...
Expected Behavior
I would expect, that the st.data_editor returns a dataframe with floats if the column config defines a float format.
At minimum there should be a way to force a column to be considered a float column.
Current Behavior
The st.data_editor incorrectly returns a dataframe with int values and therefore infers the wrong datatype on the second run.
Is this a regression?
Debug info
- Streamlit version: 1.49.1
- Python version: 3.11.9
- Operating System: Microsoft Windows 11 Enterprise, Version 23H2, OS build 22631.5909
- Browser: Microsoft Edge, Version 141.0.3537.57
Additional Information
A possible workaround is to add a type conversion to the callback function:
def btn_on_click():
st.session_state.df_input = df_result.astype({'number': 'float'})
Checklist
Summary
While working with
st.data_editorI noticed an issue with number columns in combination with storing the returned inst.session_state.The issue arises when the user first inputs integer values and stores the dataframe to session state, but wants to change the values floats later on. When the
st.data_editorinfers the datatype of the column from the data passed to it, it sees only integer values and incorrectly assumes an integer column. This prevents the user from typing a decimal sign in the second run.Adding a
NumberColumnconfig with a float format does not help.Reproducible Code Example
Steps To Reproduce
Note, that at this point you are still able to input float values!
The data editor does not accept the decimal...
Expected Behavior
I would expect, that the
st.data_editorreturns a dataframe with floats if the column config defines a float format.At minimum there should be a way to force a column to be considered a float column.
Current Behavior
The
st.data_editorincorrectly returns a dataframe with int values and therefore infers the wrong datatype on the second run.Is this a regression?
Debug info
Additional Information
A possible workaround is to add a type conversion to the callback function: