From 4a3228541b7f31fd42a37c2f98df4cbfea00333a Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 18 Sep 2024 11:04:21 -0700 Subject: [PATCH] CLN: indexes.base.py --- pandas/core/indexes/base.py | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2346c20004210..7a9207fc2421b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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: @@ -7564,21 +7565,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 -------- @@ -7590,8 +7579,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