@@ -2209,9 +2209,18 @@ def group_by(
2209
2209
│ c ┆ 3 ┆ 1 │
2210
2210
└─────┴─────┴─────┘
2211
2211
"""
2212
+ from narwhals .expr import Expr
2212
2213
from narwhals .group_by import GroupBy
2214
+ from narwhals .series import Series
2213
2215
2214
- return GroupBy (self , * flatten (keys ), drop_null_keys = drop_null_keys )
2216
+ flat_keys = flatten (keys )
2217
+ if any (isinstance (x , (Expr , Series )) for x in flat_keys ):
2218
+ msg = (
2219
+ "`group_by` with expression or Series keys is not (yet?) supported.\n \n "
2220
+ "Hint: instead of `df.group_by(nw.col('a'))`, use `df.group_by('a')`."
2221
+ )
2222
+ raise NotImplementedError (msg )
2223
+ return GroupBy (self , * flat_keys , drop_null_keys = drop_null_keys )
2215
2224
2216
2225
def sort (
2217
2226
self ,
@@ -4444,9 +4453,18 @@ def group_by(
4444
4453
│ c ┆ 3 ┆ 1 │
4445
4454
└─────┴─────┴─────┘
4446
4455
"""
4456
+ from narwhals .expr import Expr
4447
4457
from narwhals .group_by import LazyGroupBy
4458
+ from narwhals .series import Series
4448
4459
4449
- return LazyGroupBy (self , * flatten (keys ), drop_null_keys = drop_null_keys )
4460
+ flat_keys = flatten (keys )
4461
+ if any (isinstance (x , (Expr , Series )) for x in flat_keys ):
4462
+ msg = (
4463
+ "`group_by` with expression or Series keys is not (yet?) supported.\n \n "
4464
+ "Hint: instead of `df.group_by(nw.col('a'))`, use `df.group_by('a')`."
4465
+ )
4466
+ raise NotImplementedError (msg )
4467
+ return LazyGroupBy (self , * flat_keys , drop_null_keys = drop_null_keys )
4450
4468
4451
4469
def sort (
4452
4470
self ,
0 commit comments