Skip to content

Commit 54b8f60

Browse files
authored
DEP: Remove Series.str.__iter__ (#49268)
* DEP: Remove Series.str.__iter__ * Fix tests
1 parent 1294b19 commit 54b8f60

File tree

6 files changed

+14
-99
lines changed

6 files changed

+14
-99
lines changed

doc/source/whatsnew/v0.12.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ Bug fixes
434434
``Series`` or ``NaN``. For example,
435435

436436
.. ipython:: python
437-
:okwarning:
437+
:okexcept:
438438
439439
strs = "go", "bow", "joe", "slow"
440440
ds = pd.Series(strs)

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ Removal of prior version deprecations/changes
200200
- Removed the ``truediv`` keyword from :func:`eval` (:issue:`29812`)
201201
- Removed the ``pandas.datetime`` submodule (:issue:`30489`)
202202
- Removed the ``pandas.np`` submodule (:issue:`30296`)
203+
- Removed :meth:`Series.str.__iter__` (:issue:`28277`)
203204
- Removed ``pandas.SparseArray`` in favor of :class:`arrays.SparseArray` (:issue:`30642`)
204205
- Removed ``pandas.SparseSeries`` and ``pandas.SparseDataFrame`` (:issue:`30642`)
205206
- Enforced disallowing a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)

pandas/core/strings/accessor.py

-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
TYPE_CHECKING,
88
Callable,
99
Hashable,
10-
Iterator,
1110
Literal,
1211
cast,
1312
)
@@ -242,19 +241,6 @@ def __getitem__(self, key):
242241
result = self._data.array._str_getitem(key)
243242
return self._wrap_result(result)
244243

245-
def __iter__(self) -> Iterator:
246-
warnings.warn(
247-
"Columnar iteration over characters will be deprecated in future releases.",
248-
FutureWarning,
249-
stacklevel=find_stack_level(),
250-
)
251-
i = 0
252-
g = self.get(i)
253-
while g.notna().any():
254-
yield g
255-
i += 1
256-
g = self.get(i)
257-
258244
def _wrap_result(
259245
self,
260246
result,

pandas/tests/dtypes/test_inference.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ def shape(self):
146146
((_ for _ in []), True, "generator-empty"),
147147
(Series([1]), True, "Series"),
148148
(Series([], dtype=object), True, "Series-empty"),
149-
(Series(["a"]).str, True, "StringMethods"),
150-
(Series([], dtype="O").str, True, "StringMethods-empty"),
149+
(Series(["a"]).str, False, "StringMethods"),
150+
(Series([], dtype="O").str, False, "StringMethods-empty"),
151151
(Index([1]), True, "Index"),
152152
(Index([]), True, "Index-empty"),
153153
(DataFrame([[1]]), True, "DataFrame"),

pandas/tests/strings/test_cat.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import numpy as np
24
import pytest
35

@@ -380,18 +382,13 @@ def test_cat_different_classes(klass):
380382

381383
def test_cat_on_series_dot_str():
382384
# GH 28277
383-
# Test future warning of `Series.str.__iter__`
384385
ps = Series(["AbC", "de", "FGHI", "j", "kLLLm"])
385-
with tm.assert_produces_warning(FutureWarning):
386+
387+
message = re.escape(
388+
"others must be Series, Index, DataFrame, np.ndarray "
389+
"or list-like (either containing only strings or "
390+
"containing only objects of type Series/Index/"
391+
"np.ndarray[1-dim])"
392+
)
393+
with pytest.raises(TypeError, match=message):
386394
ps.str.cat(others=ps.str)
387-
# TODO(2.0): The following code can be uncommented
388-
# when `Series.str.__iter__` is removed.
389-
390-
# message = re.escape(
391-
# "others must be Series, Index, DataFrame, np.ndarray "
392-
# "or list-like (either containing only strings or "
393-
# "containing only objects of type Series/Index/"
394-
# "np.ndarray[1-dim])"
395-
# )
396-
# with pytest.raises(TypeError, match=message):
397-
# ps.str.cat(others=ps.str)

pandas/tests/strings/test_strings.py

-69
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
Index,
1212
MultiIndex,
1313
Series,
14-
isna,
1514
)
1615
import pandas._testing as tm
1716

@@ -34,74 +33,6 @@ def assert_series_or_index_equal(left, right):
3433
tm.assert_index_equal(left, right)
3534

3635

37-
def test_iter():
38-
# GH3638
39-
strs = "google", "wikimedia", "wikipedia", "wikitravel"
40-
ser = Series(strs)
41-
42-
with tm.assert_produces_warning(FutureWarning):
43-
for s in ser.str:
44-
# iter must yield a Series
45-
assert isinstance(s, Series)
46-
47-
# indices of each yielded Series should be equal to the index of
48-
# the original Series
49-
tm.assert_index_equal(s.index, ser.index)
50-
51-
for el in s:
52-
# each element of the series is either a basestring/str or nan
53-
assert isinstance(el, str) or isna(el)
54-
55-
# desired behavior is to iterate until everything would be nan on the
56-
# next iter so make sure the last element of the iterator was 'l' in
57-
# this case since 'wikitravel' is the longest string
58-
assert s.dropna().values.item() == "l"
59-
60-
61-
def test_iter_empty(any_string_dtype):
62-
ser = Series([], dtype=any_string_dtype)
63-
64-
i, s = 100, 1
65-
66-
with tm.assert_produces_warning(FutureWarning):
67-
for i, s in enumerate(ser.str):
68-
pass
69-
70-
# nothing to iterate over so nothing defined values should remain
71-
# unchanged
72-
assert i == 100
73-
assert s == 1
74-
75-
76-
def test_iter_single_element(any_string_dtype):
77-
ser = Series(["a"], dtype=any_string_dtype)
78-
79-
with tm.assert_produces_warning(FutureWarning):
80-
for i, s in enumerate(ser.str):
81-
pass
82-
83-
assert not i
84-
tm.assert_series_equal(ser, s)
85-
86-
87-
def test_iter_object_try_string():
88-
ser = Series(
89-
[
90-
slice(None, np.random.randint(10), np.random.randint(10, 20))
91-
for _ in range(4)
92-
]
93-
)
94-
95-
i, s = 100, "h"
96-
97-
with tm.assert_produces_warning(FutureWarning):
98-
for i, s in enumerate(ser.str):
99-
pass
100-
101-
assert i == 100
102-
assert s == "h"
103-
104-
10536
# test integer/float dtypes (inferred by constructor) and mixed
10637

10738

0 commit comments

Comments
 (0)