File tree Expand file tree Collapse file tree 1 file changed +10
-9
lines changed Expand file tree Collapse file tree 1 file changed +10
-9
lines changed Original file line number Diff line number Diff line change @@ -423,7 +423,16 @@ def groupby(
423423 # strings is passed in, the data used for the groupby is dropped before the
424424 # groupby takes place.
425425 drop = False
426-
426+ # Check that there is no ambiguity in the parameter we were given.
427+ _by_check = by if is_list_like (by ) else [by ]
428+ for k in _by_check :
429+ if k in self .index .names and k in self .axes [axis ]:
430+ level_name , index_name = "an index" , "a column"
431+ if axis == 1 :
432+ level_name , index_name = index_name , level_name
433+ raise ValueError (
434+ f"{ k } is both { level_name } level and { index_name } label, which is ambiguous."
435+ )
427436 if (
428437 not isinstance (by , (pandas .Series , Series ))
429438 and is_list_like (by )
@@ -447,14 +456,6 @@ def groupby(
447456 idx_name = by .name
448457 by = by ._query_compiler
449458 elif is_list_like (by ):
450- for k in by :
451- if k in self .index .names and k in self .axes [axis ]:
452- level_name , index_name = "an index" , "a column"
453- if axis == 1 :
454- level_name , index_name = index_name , level_name
455- raise ValueError (
456- f"{ k } is both { level_name } level and { index_name } label, which is ambiguous."
457- )
458459 # fastpath for multi column groupby
459460 if axis == 0 and all (
460461 (
You can’t perform that action at this time.
0 commit comments