Skip to content

REF: Don't materialize range if not needed #57857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2686,7 +2686,7 @@ def _value_counts(
names = result_series.index.names
# GH#55951 - Temporarily replace names in case they are integers
result_series.index.names = range(len(names))
index_level = list(range(len(self._grouper.groupings)))
index_level = range(len(self._grouper.groupings))
result_series = result_series.sort_index(
level=index_level, sort_remaining=False
)
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ def _set_levels(

if level is None:
new_levels = tuple(ensure_index(lev, copy=copy)._view() for lev in levels)
level_numbers = list(range(len(new_levels)))
level_numbers: range | list[int] = range(len(new_levels))
else:
level_numbers = [self._get_level_number(lev) for lev in level]
new_levels_list = list(self._levels)
Expand Down Expand Up @@ -3014,7 +3014,7 @@ def _maybe_to_slice(loc):
raise KeyError(key) from err
except TypeError:
# e.g. test_partial_slicing_with_multiindex partial string slicing
loc, _ = self.get_loc_level(key, list(range(self.nlevels)))
loc, _ = self.get_loc_level(key, range(self.nlevels))
return loc

# -- partial selection or non-unique index
Expand Down Expand Up @@ -3101,7 +3101,7 @@ def get_loc_level(self, key, level: IndexLabel = 0, drop_level: bool = True):
>>> mi.get_loc_level(["b", "e"])
(1, None)
"""
if not isinstance(level, (list, tuple)):
if not isinstance(level, (range, list, tuple)):
level = self._get_level_number(level)
else:
level = [self._get_level_number(lev) for lev in level]
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import itertools
from typing import (
TYPE_CHECKING,
Callable,
Expand Down Expand Up @@ -422,7 +423,7 @@ def _all_key(key):
row_margin = row_margin.stack()

# GH#26568. Use names instead of indices in case of numeric names
new_order_indices = [len(cols)] + list(range(len(cols)))
new_order_indices = itertools.chain([len(cols)], range(len(cols)))
new_order_names = [row_margin.index.names[i] for i in new_order_indices]
row_margin.index = row_margin.index.reorder_levels(new_order_names)
else:
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,13 +523,13 @@ def _ensure_key_mapped_multiindex(

if level is not None:
if isinstance(level, (str, int)):
sort_levels = [level]
level_iter = [level]
else:
sort_levels = level
level_iter = level

sort_levels = [index._get_level_number(lev) for lev in sort_levels]
sort_levels: range | set = {index._get_level_number(lev) for lev in level_iter}
else:
sort_levels = list(range(index.nlevels)) # satisfies mypy
sort_levels = range(index.nlevels)

mapped = [
ensure_key_mapped(index._get_level_values(level), key)
Expand Down
6 changes: 4 additions & 2 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,12 +1223,14 @@ def is_potential_multi_index(
bool : Whether or not columns could become a MultiIndex
"""
if index_col is None or isinstance(index_col, bool):
index_col = []
index_columns = set()
else:
index_columns = set(index_col)

return bool(
len(columns)
and not isinstance(columns, ABCMultiIndex)
and all(isinstance(c, tuple) for c in columns if c not in list(index_col))
and all(isinstance(c, tuple) for c in columns if c not in index_columns)
)


Expand Down
2 changes: 1 addition & 1 deletion pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ def _clean_options(
)
else:
if is_integer(skiprows):
skiprows = list(range(skiprows))
skiprows = range(skiprows)
if skiprows is None:
skiprows = set()
elif not callable(skiprows):
Expand Down
Loading