File tree Expand file tree Collapse file tree 4 files changed +11
-9
lines changed Expand file tree Collapse file tree 4 files changed +11
-9
lines changed Original file line number Diff line number Diff line change 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
1717if 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
5857class 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.
Original file line number Diff line number Diff line change 55from narwhals ._plan import _parse
66from narwhals ._plan ._expansion import expand_selector_irs_names , prepare_projection
77from narwhals ._plan .common import ensure_seq_str , temp
8+ from narwhals ._plan .exceptions import group_by_no_keys_error
89from narwhals ._plan .group_by import GroupBy , Grouped
910from narwhals ._plan .options import SortMultipleOptions
1011from 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
Original file line number Diff line number Diff 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} \n Hint: 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 )
Original file line number Diff line number Diff 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- )
136132def test_partition_by_fully_empty_selector (data : Data ) -> None :
137133 df = dataframe (data )
138134 with pytest .raises (
You can’t perform that action at this time.
0 commit comments