Skip to content

BUG: iter(ser.str) did not raise TypeError #54174

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 7 commits into from
Jul 24, 2023
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 doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ Conversion

Strings
^^^^^^^
-
- Bug in :meth:`Series.str` that did not raise a ``TypeError`` when iterated (:issue:`54173`)
-

Interval
Expand Down
44 changes: 27 additions & 17 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
from pandas.core.construction import extract_array

if TYPE_CHECKING:
from collections.abc import Hashable
from collections.abc import (
Hashable,
Iterator,
)

from pandas import (
DataFrame,
Expand Down Expand Up @@ -243,6 +246,9 @@ def __getitem__(self, key):
result = self._data.array._str_getitem(key)
return self._wrap_result(result)

def __iter__(self) -> Iterator:
raise TypeError(f"'{type(self).__name__}' object is not iterable")

def _wrap_result(
self,
result,
Expand Down Expand Up @@ -438,22 +444,26 @@ def _get_series_list(self, others):
others = DataFrame(others, index=idx)
return [others[x] for x in others]
elif is_list_like(others, allow_sets=False):
others = list(others) # ensure iterators do not get read twice etc

# in case of list-like `others`, all elements must be
# either Series/Index/np.ndarray (1-dim)...
if all(
isinstance(x, (ABCSeries, ABCIndex))
or (isinstance(x, np.ndarray) and x.ndim == 1)
for x in others
):
los: list[Series] = []
while others: # iterate through list and append each element
los = los + self._get_series_list(others.pop(0))
return los
# ... or just strings
elif all(not is_list_like(x) for x in others):
return [Series(others, index=idx)]
try:
others = list(others) # ensure iterators do not get read twice etc
except TypeError:
# e.g. ser.str, raise below
pass
else:
# in case of list-like `others`, all elements must be
# either Series/Index/np.ndarray (1-dim)...
if all(
isinstance(x, (ABCSeries, ABCIndex))
or (isinstance(x, np.ndarray) and x.ndim == 1)
for x in others
):
los: list[Series] = []
while others: # iterate through list and append each element
los = los + self._get_series_list(others.pop(0))
return los
# ... or just strings
elif all(not is_list_like(x) for x in others):
return [Series(others, index=idx)]
raise TypeError(
"others must be Series, Index, DataFrame, np.ndarray "
"or list-like (either containing only strings or "
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ def shape(self):
((_ for _ in []), True, "generator-empty"),
(Series([1]), True, "Series"),
(Series([], dtype=object), True, "Series-empty"),
(Series(["a"]).str, False, "StringMethods"),
(Series([], dtype="O").str, False, "StringMethods-empty"),
# Series.str will still raise a TypeError if iterated
(Series(["a"]).str, True, "StringMethods"),
(Series([], dtype="O").str, True, "StringMethods-empty"),
(Index([1]), True, "Index"),
(Index([]), True, "Index-empty"),
(DataFrame([[1]]), True, "DataFrame"),
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/strings/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ def test_startswith_endswith_non_str_patterns(pattern):
ser.str.endswith(pattern)


def test_iter_raises():
# GH 54173
ser = Series(["foo", "bar"])
with pytest.raises(TypeError, match="'StringMethods' object is not iterable"):
iter(ser.str)


# test integer/float dtypes (inferred by constructor) and mixed


Expand Down