#17026·streamlit

Keyed `st.dataframe` / `st.data_editor` keep positional selection and edit state across a data change, so actions land on the wrong row

Author: codingthatCreated Sep 18, 2026Updated Sep 28, 2026
Labelstype:enhancementfeature:st.dataframearea:widgetsfeature:st.data_editor

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

A keyed st.dataframe or st.data_editor excludes its data from the element identity: key_as_main_identity={"selection_mode", "is_selection_activated"} at arrow.py lines 1280-1284. The element id therefore survives a data refresh, and the browser resends grid state that was computed against the previous frame. event.selection.rows and edited_rows are positional, so after a rerun that reorders or inserts rows those positions address different records, and the app receives a selection or an edit for a row the user never touched. There is no error and no warning. The comment directly above that call site acknowledges that selections "can become orphaned when the data changes" and that resetting them "might be a nice enhancement"; the effect on an app that acts on the selection is a wrong-row write, not a cosmetic orphan.

Reproducible Code Example

python
import pandas as pd
import streamlit as st

st.session_state.setdefault("gen", 0)
st.session_state.setdefault("closed", [])

# Simulates a queue whose contents change between reruns (a new ticket arrived).
rows = ["TICK-101", "TICK-102", "TICK-103", "TICK-104"]
if st.session_state["gen"]:
    rows = ["TICK-100", *rows]
df = pd.DataFrame({"ticket": rows})

event = st.dataframe(df, key="queue", on_select="rerun", selection_mode="single-row")

if st.button("Simulate new ticket arriving", key="refresh"):
    st.session_state["gen"] += 1
    st.rerun()

if st.button("Close selected", key="close") and event.selection.rows:
    position = event.selection.rows[0]
    st.session_state["closed"].append(df.iloc[position]["ticket"])

st.write("Selected positions:", event.selection.rows)
st.write("Closed:", st.session_state["closed"])

Steps To Reproduce

  1. streamlit run (or uv run streamlit run) the snippet.
  2. Click the row showing TICK-102 (position 1). The app shows Selected positions: [1].
  3. Click "Simulate new ticket arriving". TICK-102 gets bumped down to position 2, but the highlighted row and event.selection.rows are still [1], which is now TICK-101.
  4. Click "Close selected". You get Closed: ['TICK-101'] even though you had initially selected TICK-102.

The same holds for st.data_editor with the default num_rows="fixed": a pending edited_rows entry is re-applied at the same position after the frame changes.

The identity part can be shown without a browser, using AppTest:

python
from streamlit.testing.v1 import AppTest

def script():
    import pandas as pd
    import streamlit as st
    st.session_state.setdefault("gen", 0)
    if st.button("refresh", key="refresh"):
        st.session_state["gen"] += 1
    rows = ["TICK-101", "TICK-102", "TICK-103", "TICK-104"]
    if st.session_state["gen"]:
        rows = ["TICK-100", *rows]
    df = pd.DataFrame({"ticket": rows})
    st.dataframe(df, key="queue", on_select="rerun", selection_mode="single-row")
    st.dataframe(df, on_select="rerun", selection_mode="single-row")

at = AppTest.from_function(script).run()
keyed, unkeyed = at.dataframe[0].proto.id, at.dataframe[1].proto.id
at.button(key="refresh").click().run()
print(keyed == at.dataframe[0].proto.id)    # True  -> keyed id survives the data change
print(unkeyed == at.dataframe[1].proto.id)  # False -> unkeyed id does not

Expected Behavior

When the data behind a keyed grid changes, stale selection and edit state should be reset (or the state should be keyed by row identity rather than position), so an app never receives positions computed against a frame it is no longer showing. At minimum the docs for key= on st.dataframe / st.data_editor should say that selection and edit state is positional and survives data changes.

Current Behavior

Image

Positional selection and edit state is preserved across data changes for keyed grids. Dropping key= makes the id data-dependent and the stale state orphans to empty, which is safe but discards every pending selection or edit on any data change, and contradicts the general advice to always give widgets a key.

Is this a regression?

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

Not a recent regression, at least. The row retarget in steps 1-4 was observed in Chromium on 1.60.0. The AppTest demonstration above prints True / False on both 1.63.0 and 1.64.0 (the latest release at the time of writing), and the identity code is unchanged at the current tip of develop. On 1.60.0 and later the st.data_editor case is wider than on 1.59.x: a keyed editor with num_rows="fixed" re-applies a pending edit at the same position after a reorder, where 1.59.x discarded it.

Debug info

  • Streamlit version: 1.63.0 and 1.64.0, both reproduced
  • Python version: 3.14.6
  • Operating System: Linux Mint 22.3 Zena 64-bit
  • Browser: Version 152.0.7977.82 (Official Build) for Linux Mint (x86_64)

Additional Information

Related: _apply_dataframe_edits (data_editor.py#L560-L564) receives no disabled argument, so disabled=["_index"] is enforced only in the browser; a client-sent index edit is applied server-side. Apps that act on grid state currently have to pin the frame they rendered in st.session_state, resolve positions against that frame, and reject anything they did not serve.