|
48 | 48 | from pandas.core.construction import extract_array
|
49 | 49 |
|
50 | 50 | if TYPE_CHECKING:
|
51 |
| - from collections.abc import Hashable |
| 51 | + from collections.abc import ( |
| 52 | + Hashable, |
| 53 | + Iterator, |
| 54 | + ) |
52 | 55 |
|
53 | 56 | from pandas import (
|
54 | 57 | DataFrame,
|
@@ -243,6 +246,9 @@ def __getitem__(self, key):
|
243 | 246 | result = self._data.array._str_getitem(key)
|
244 | 247 | return self._wrap_result(result)
|
245 | 248 |
|
| 249 | + def __iter__(self) -> Iterator: |
| 250 | + raise TypeError(f"'{type(self).__name__}' object is not iterable") |
| 251 | + |
246 | 252 | def _wrap_result(
|
247 | 253 | self,
|
248 | 254 | result,
|
@@ -438,22 +444,26 @@ def _get_series_list(self, others):
|
438 | 444 | others = DataFrame(others, index=idx)
|
439 | 445 | return [others[x] for x in others]
|
440 | 446 | elif is_list_like(others, allow_sets=False):
|
441 |
| - others = list(others) # ensure iterators do not get read twice etc |
442 |
| - |
443 |
| - # in case of list-like `others`, all elements must be |
444 |
| - # either Series/Index/np.ndarray (1-dim)... |
445 |
| - if all( |
446 |
| - isinstance(x, (ABCSeries, ABCIndex)) |
447 |
| - or (isinstance(x, np.ndarray) and x.ndim == 1) |
448 |
| - for x in others |
449 |
| - ): |
450 |
| - los: list[Series] = [] |
451 |
| - while others: # iterate through list and append each element |
452 |
| - los = los + self._get_series_list(others.pop(0)) |
453 |
| - return los |
454 |
| - # ... or just strings |
455 |
| - elif all(not is_list_like(x) for x in others): |
456 |
| - return [Series(others, index=idx)] |
| 447 | + try: |
| 448 | + others = list(others) # ensure iterators do not get read twice etc |
| 449 | + except TypeError: |
| 450 | + # e.g. ser.str, raise below |
| 451 | + pass |
| 452 | + else: |
| 453 | + # in case of list-like `others`, all elements must be |
| 454 | + # either Series/Index/np.ndarray (1-dim)... |
| 455 | + if all( |
| 456 | + isinstance(x, (ABCSeries, ABCIndex)) |
| 457 | + or (isinstance(x, np.ndarray) and x.ndim == 1) |
| 458 | + for x in others |
| 459 | + ): |
| 460 | + los: list[Series] = [] |
| 461 | + while others: # iterate through list and append each element |
| 462 | + los = los + self._get_series_list(others.pop(0)) |
| 463 | + return los |
| 464 | + # ... or just strings |
| 465 | + elif all(not is_list_like(x) for x in others): |
| 466 | + return [Series(others, index=idx)] |
457 | 467 | raise TypeError(
|
458 | 468 | "others must be Series, Index, DataFrame, np.ndarray "
|
459 | 469 | "or list-like (either containing only strings or "
|
|
0 commit comments