Skip to content
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

fix(python): Address incorrect selector & col expansion #19742

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions py-polars/polars/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,6 @@ def __and__(self, other: Any) -> Expr: ...
def __and__(self, other: Any) -> SelectorType | Expr:
if is_column(other):
colname = other.meta.output_name()
if self._attrs["name"] == "by_name" and (
params := self._attrs["params"]
).get("require_all", True):
return by_name(*params["*names"], colname)
other = by_name(colname)
if is_selector(other):
return _selector_proxy_(
Expand All @@ -399,6 +395,12 @@ def __and__(self, other: Any) -> SelectorType | Expr:
else:
return self.as_expr().__and__(other)

def __rand__(self, other: Any) -> Expr:
if is_column(other):
colname = other.meta.output_name()
return by_name(colname) & self
return self.as_expr().__rand__(other)

@overload
def __or__(self, other: SelectorType) -> SelectorType: ...

Expand All @@ -417,6 +419,11 @@ def __or__(self, other: Any) -> SelectorType | Expr:
else:
return self.as_expr().__or__(other)

def __ror__(self, other: Any) -> Expr:
if is_column(other):
other = by_name(other.meta.output_name())
return self.as_expr().__ror__(other)

@overload
def __xor__(self, other: SelectorType) -> SelectorType: ...

Expand All @@ -435,21 +442,6 @@ def __xor__(self, other: Any) -> SelectorType | Expr:
else:
return self.as_expr().__or__(other)

def __rand__(self, other: Any) -> Expr:
if is_column(other):
colname = other.meta.output_name()
if self._attrs["name"] == "by_name" and (
params := self._attrs["params"]
).get("require_all", True):
return by_name(colname, *params["*names"])
other = by_name(colname)
return self.as_expr().__rand__(other)

def __ror__(self, other: Any) -> Expr:
if is_column(other):
other = by_name(other.meta.output_name())
return self.as_expr().__ror__(other)

def __rxor__(self, other: Any) -> Expr:
if is_column(other):
other = by_name(other.meta.output_name())
Expand Down
10 changes: 8 additions & 2 deletions py-polars/tests/unit/test_selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,17 @@ def test_selector_by_name(df: pl.DataFrame) -> None:

# check "by_name & col"
for selector_expr, expected in (
(cs.by_name("abc", "cde") & pl.col("ghi"), ["abc", "cde", "ghi"]),
(pl.col("ghi") & cs.by_name("cde", "abc"), ["ghi", "cde", "abc"]),
(cs.by_name("abc", "cde") & pl.col("ghi"), []),
(cs.by_name("abc", "cde") & pl.col("cde"), ["cde"]),
(pl.col("cde") & cs.by_name("cde", "abc"), ["cde"]),
):
assert df.select(selector_expr).columns == expected

# check "by_name & by_name"
assert df.select(
cs.by_name("abc", "cde", "def", "eee") & cs.by_name("cde", "eee", "fgg")
).columns == ["cde", "eee"]

# expected errors
with pytest.raises(ColumnNotFoundError, match="xxx"):
df.select(cs.by_name("xxx", "fgg", "!!!"))
Expand Down
Loading