Skip to content

CLN: indexes/base.py #59928

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 2 commits into from
Oct 3, 2024
Merged
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
22 changes: 6 additions & 16 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4153,7 +4153,8 @@ def reindex(
preserve_names = not hasattr(target, "name")

# GH7774: preserve dtype/tz if target is empty and not an Index.
target = ensure_has_len(target) # target may be an iterator
if is_iterator(target):
target = list(target)

if not isinstance(target, Index) and len(target) == 0:
if level is not None and self._is_multi:
Expand Down Expand Up @@ -7568,21 +7569,9 @@ def ensure_index(index_like: Axes, copy: bool = False) -> Index:
return Index(index_like, copy=copy)


def ensure_has_len(seq):
"""
If seq is an iterator, put its values into a list.
"""
try:
len(seq)
except TypeError:
return list(seq)
else:
return seq


def trim_front(strings: list[str]) -> list[str]:
"""
Trims zeros and decimal points.
Trims leading spaces evenly among all strings.

Examples
--------
Expand All @@ -7594,8 +7583,9 @@ def trim_front(strings: list[str]) -> list[str]:
"""
if not strings:
return strings
while all(strings) and all(x[0] == " " for x in strings):
strings = [x[1:] for x in strings]
smallest_leading_space = min(len(x) - len(x.lstrip()) for x in strings)
if smallest_leading_space > 0:
strings = [x[smallest_leading_space:] for x in strings]
return strings


Expand Down