Skip to content

Commit 6d219f4

Browse files
committed
fix: raise on empty by
1 parent 9810b73 commit 6d219f4

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

narwhals/_plan/compliant/group_by.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
FrameT_co,
1313
ResolverT_co,
1414
)
15-
from narwhals.exceptions import ComputeError
15+
from narwhals._plan.exceptions import group_by_no_keys_error
1616

1717
if TYPE_CHECKING:
1818
from collections.abc import Iterator
@@ -51,8 +51,7 @@ def keys(self) -> Seq[NamedIR]:
5151
def key_names(self) -> Seq[str]:
5252
if names := self._key_names:
5353
return names
54-
msg = "at least one key is required in a group_by operation"
55-
raise ComputeError(msg)
54+
raise group_by_no_keys_error()
5655

5756

5857
class EagerDataFrameGroupBy(DataFrameGroupBy[EagerDataFrameT], Protocol[EagerDataFrameT]):
@@ -163,8 +162,7 @@ def key_names(self) -> Seq[str]:
163162
return names
164163
if keys := self.keys:
165164
return tuple(e.name for e in keys)
166-
msg = "at least one key is required in a group_by operation"
167-
raise ComputeError(msg)
165+
raise group_by_no_keys_error()
168166

169167
def requires_projection(self, *, allow_aliasing: bool = False) -> bool:
170168
"""Return True is group keys contain anything that is not a column selection.

narwhals/_plan/dataframe.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from narwhals._plan import _parse
66
from narwhals._plan._expansion import expand_selector_irs_names, prepare_projection
77
from narwhals._plan.common import ensure_seq_str, temp
8+
from narwhals._plan.exceptions import group_by_no_keys_error
89
from narwhals._plan.group_by import GroupBy, Grouped
910
from narwhals._plan.options import SortMultipleOptions
1011
from narwhals._plan.series import Series
@@ -266,6 +267,8 @@ def partition_by(
266267
) -> list[Self]:
267268
by_selectors = _parse.parse_into_seq_of_selector_ir(by, *more_by)
268269
names = expand_selector_irs_names(by_selectors, schema=self)
270+
if not names:
271+
raise group_by_no_keys_error()
269272
partitions = self._compliant.partition_by(names, include_key=include_key)
270273
return [self._with_compliant(p) for p in partitions]
271274

narwhals/_plan/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,8 @@ def column_index_error(
210210
max_nth = f"`nth({n_names - 1})`" if index >= 0 else f"`nth(-{n_names})`"
211211
msg = f"Invalid column index {index!r}\nHint: The schema's last column is {max_nth}"
212212
return ComputeError(msg)
213+
214+
215+
def group_by_no_keys_error() -> ComputeError:
216+
msg = "at least one key is required in a group_by operation"
217+
return ComputeError(msg)

tests/plan/frame_partition_by_test.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,6 @@ def test_partition_by_missing_names(data: Data) -> None: # pragma: no cover
129129
df.partition_by("c", "e")
130130

131131

132-
# TODO @dangotbanned: Stricter selectors (easier task)
133-
@pytest.mark.xfail(
134-
reason="TODO: Guard against empty names returned from `_expansion.expand_selector_irs_names`."
135-
)
136132
def test_partition_by_fully_empty_selector(data: Data) -> None:
137133
df = dataframe(data)
138134
with pytest.raises(

0 commit comments

Comments
 (0)