Skip to content

Commit

Permalink
CLN: indexes/base.py (#59928)
Browse files Browse the repository at this point in the history
CLN: indexes.base.py
  • Loading branch information
mroeschke authored Oct 3, 2024
1 parent 198ed86 commit c47296a
Showing 1 changed file with 6 additions and 16 deletions.
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

0 comments on commit c47296a

Please sign in to comment.