feat(python/sedonadb): restrict DataFrame __getitem__ to single-column lookup#852
Open
jiayuasu wants to merge 1 commit into
Open
feat(python/sedonadb): restrict DataFrame __getitem__ to single-column lookup#852jiayuasu wants to merge 1 commit into
jiayuasu wants to merge 1 commit into
Conversation
Address post-merge feedback on apache#846. - Restrict `DataFrame.__getitem__` to `(key: Union[str, int]) -> Expr`. Drops the polymorphic list-projection and bool-Expr-filter forms that apache#846 added. Reasons mirror Ibis's deprecation of the same forms: a single return type keeps IDE/type-checker inference on `df["x"].<method>` clean, and the multi-column / filter cases have obvious named entry points (`select`, `filter`). - Integer indexing now resolves a column by 0-based position, with negative indexing supported. Out-of-range integers raise `IndexError`; unknown string names raise `KeyError` listing the available columns. `bool` is rejected explicitly so a stray `df[True]` doesn't silently mean `df[1]` (bool is a subclass of int in Python). List, slice, and Expr keys raise `TypeError` with messages pointing at `select` / `filter`. - Move the lazy `Expr` / `Literal` / `col` / `_to_expr` imports inside `__getitem__`, `select`, and `filter` to module level (combined). Per project policy in this module, lazy imports are reserved for optional dependencies like pyarrow. The fix while here keeps the import discipline consistent across the three Expr-aware methods rather than churning them separately later. - Update annotations on `select` and `filter` to use the new module-level names directly (no more string forward references). Tests rewritten for the new shape: 11 cases covering name and position lookup, negative indexing, operator composition, and the `KeyError` / `IndexError` / `TypeError` error paths. cc @paleolimbot
6f6c081 to
1a319e1
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR narrows Python DataFrame.__getitem__ to a single-column lookup API returning Expr, aligning bracket access with predictable typing while directing projection/filtering to explicit methods.
Changes:
- Restricts
df[key]to string column names and integer column positions, including negative indices. - Adds explicit error handling for unknown names, out-of-range indices, bools, lists, slices, and
Exprkeys. - Updates tests to cover the revised lookup behavior and rejected legacy shortcut forms.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
python/sedonadb/python/sedonadb/dataframe.py |
Updates __getitem__, import placement, annotations, and related select/filter type checks. |
python/sedonadb/tests/expr/test_dataframe_getitem.py |
Replaces polymorphic getitem tests with single-column lookup and error-path coverage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Post-merge follow-up to #846 addressing @paleolimbot's review feedback.
What changes
DataFrame.__getitem__is now strictly single-column lookup:Previously the same method also handled
df[["x","y"]](list projection) anddf[bool_expr](filter). Those are dropped. Users go through the explicitdf.select(...)anddf.filter(...)entry points instead.The motivation, mirroring Ibis's same deprecation: a single return type (
Expr) lets IDEs and type checkers resolvedf["x"].<method>cleanly. With the old polymorphic shape, the return type wasDataFrame | Expr, which broke autocomplete on the common case.Error paths
KeyError, message lists available columnsIndexErrorboolTypeError(guarded explicitly; bool is a subclass of int in Python)list,slice,Expr, anything elseTypeErrorwith a message pointing atselect/filterWhile here — import-discipline cleanup
__getitem__,select, andfilterpreviously had lazy in-function imports ofExpr,Literal,col, and_to_expr. Per the policy in this module ("lazy imports are reserved for optional dependencies like pyarrow"), those move to module level (combined) and the method annotations switch from string forward references to the runtime-imported names.Test plan
tests/expr/test_dataframe_getitem.pycover name / positive / negative / first / out-of-range / unknown / bool / list / slice / Expr keys plus the operator-composition path ondf["x"].repr() == ...for the positive cases, per the test-policy convention locked in earlier PRs.test_dataframe_select.pyandtest_dataframe_filter.pystill pass (35 expr-dataframe tests total).pytest --doctest-modules dataframe.pypasses (17 doctests including the updated__getitem__example).ruff checkandruff formatclean.