-
Notifications
You must be signed in to change notification settings - Fork 4k
Fix data editor when adding and deleting rows #11183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🎉 Snyk checks have passed. No issues have been found so far.✅ security/snyk check is complete. No issues have been found. (View Details) ✅ license/snyk check is complete. No issues have been found. (View Details) |
✅ PR preview is ready!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes an issue in the data editor where adding rows fails after row deletions change the dataframe index type, by updating the auto-increment logic.
- Added comprehensive tests covering various index types (range, non‐contiguous integer, datetime, and empty dataframes).
- Updated _apply_row_additions to correctly handle different index types and log warnings when necessary.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| lib/tests/streamlit/elements/data_editor_test.py | New tests verifying row additions for various index scenarios and the correct warning for unsupported cases |
| lib/streamlit/elements/widgets/data_editor.py | Refactored row addition logic to support auto-increment behavior for range and integer indexes and to prioritize explicit index values for non-range types |
|
|
||
| # This is only used if the dataframe has a range or integer index that can be | ||
| # auto incremented: | ||
| index_stop: int | None = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this logic 308 - 363 would actually be simplified by having two function for each case and conditionally calling one based on the isInstance checks.
Then, maybe you could create a little function for creating the new row that could be shared between the functions. It could also return the current index even though that is only used by case 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I extracted one part of the function into a dedicated function for simplification. But I think the rest is a bit more complicated to move into dedicated functions since it is a bit entangled. For example, if it's a pd.Index with integer type, we want to use the auto-increment used by range index, but only if the user hasn't set an index value. And if it is a range index, we always want to use auto-increment, even if the UI has provided an index value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit easier to read, thanks! I guess the rest is reflective of all these different scenarios.
Describe your changes
Fix a bug (regression?) with data editor that prevents adding added rows to the result dataframe if existing rows got deleted. The root cause is that deleting rows might cause the index to be changed from range index to integer index, breaking our automatic increment logic when adding new rows.
GitHub Issue Link (if applicable)
Testing Plan
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.