Skip to content
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ Performance Improvements
- Improved performance of :meth:`pandas.core.groupby.GroupBy.quantile` (:issue:`20405`)
- Improved performance of :meth:`read_csv` by faster tokenizing and faster parsing of small float numbers (:issue:`25784`)
- Improved performance of :meth:`read_csv` by faster parsing of N/A and boolean values (:issue:`25804`)
- Fixed performance regression with :class:`MultiIndex` style :class:`Series` statistical operations (:issue:`25952`)

.. _whatsnew_0250.bug_fixes:

Expand Down
26 changes: 13 additions & 13 deletions pandas/core/groupby/grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,21 +520,21 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True,
any_arraylike = any(isinstance(g, (list, tuple, Series, Index, np.ndarray))
for g in keys)

try:
if isinstance(obj, DataFrame):
all_in_columns_index = all(g in obj.columns or g in obj.index.names
for g in keys)
elif isinstance(obj, Series):
all_in_columns_index = all(g in obj.index.names for g in keys)
else:
if (not any_callable and not any_arraylike and not any_groupers and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some comments here on what is being checked

match_axis_length and level is None):
try:
if isinstance(obj, DataFrame):
all_in_columns_index = all(g in obj.columns or g in
obj.index.names for g in keys)
elif isinstance(obj, Series):
all_in_columns_index = all(g in obj.index.names for g in keys)
else:
all_in_columns_index = False
except Exception:
all_in_columns_index = False
except Exception:
all_in_columns_index = False

if (not any_callable and not all_in_columns_index and
not any_arraylike and not any_groupers and
match_axis_length and level is None):
keys = [com.asarray_tuplesafe(keys)]
if not all_in_columns_index:
keys = [com.asarray_tuplesafe(keys)]

if isinstance(level, (tuple, list)):
if key is None:
Expand Down