Skip to content

st.data_editor infers datatype int but float is expected #12745

@michue-work

Description

@michue-work

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

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

  1. Type integer values into the two fields of the data editor.
    Note, that at this point you are still able to input float values!
  2. With the integer values in the data editor, hit the button
  3. 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?

  • Yes, this used to work in a previous version.

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'})

Metadata

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