Skip to content

Commit 882f3c8

Browse files
Added DeprecationWarning to Series.str.__iter__
1 parent bc75e19 commit 882f3c8

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

doc/source/whatsnew/v1.0.0.rst

+25
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,31 @@ The following methods now also correctly output values for unobserved categories
260260
df.groupby(["cat_1", "cat_2"], observed=False)["value"].count()
261261
262262
263+
Avoid iterating over ``Series.str``
264+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
265+
266+
Iterating over the `.str` accessor of :class:`Series` will now raise a
267+
`DeprecationWarning` and be removed in future releases (:issue:`28277`).
268+
Since no clear purpose of this feature could be derived, there will be
269+
no replacement.
270+
271+
.. code-block:: ipython
272+
273+
In [1]: s = pd.Series(["a", "ab"])
274+
In [2]: s
275+
Out[2]:
276+
0 a
277+
1 ab
278+
dtype: object
279+
280+
In [3]: generator = (_ for _ in s.str)
281+
In [4]: next(generator)
282+
Out[4]:
283+
DeprecationWarning: Columnar iteration over characters will be deprecated in future releases.
284+
0 a
285+
1 a
286+
dtype: object
287+
263288
.. _whatsnew_1000.api_breaking.deps:
264289

265290
Increased minimum versions for dependencies

pandas/core/strings.py

+5
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,11 @@ def __getitem__(self, key):
20932093
return self.get(key)
20942094

20952095
def __iter__(self):
2096+
warnings.warn(
2097+
"Columnar iteration over characters will be deprecated in future releases.",
2098+
DeprecationWarning,
2099+
stacklevel=2,
2100+
)
20962101
i = 0
20972102
g = self.get(i)
20982103
while g.notna().any():

pandas/tests/test_strings.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -329,17 +329,18 @@ def test_iter(self):
329329
strs = "google", "wikimedia", "wikipedia", "wikitravel"
330330
ds = Series(strs)
331331

332-
for s in ds.str:
333-
# iter must yield a Series
334-
assert isinstance(s, Series)
332+
with tm.assert_produces_warning(DeprecationWarning):
333+
for s in ds.str:
334+
# iter must yield a Series
335+
assert isinstance(s, Series)
335336

336-
# indices of each yielded Series should be equal to the index of
337-
# the original Series
338-
tm.assert_index_equal(s.index, ds.index)
337+
# indices of each yielded Series should be equal to the index of
338+
# the original Series
339+
tm.assert_index_equal(s.index, ds.index)
339340

340-
for el in s:
341-
# each element of the series is either a basestring/str or nan
342-
assert isinstance(el, str) or isna(el)
341+
for el in s:
342+
# each element of the series is either a basestring/str or nan
343+
assert isinstance(el, str) or isna(el)
343344

344345
# desired behavior is to iterate until everything would be nan on the
345346
# next iter so make sure the last element of the iterator was 'l' in
@@ -351,8 +352,9 @@ def test_iter_empty(self):
351352

352353
i, s = 100, 1
353354

354-
for i, s in enumerate(ds.str):
355-
pass
355+
with tm.assert_produces_warning(DeprecationWarning):
356+
for i, s in enumerate(ds.str):
357+
pass
356358

357359
# nothing to iterate over so nothing defined values should remain
358360
# unchanged
@@ -362,8 +364,9 @@ def test_iter_empty(self):
362364
def test_iter_single_element(self):
363365
ds = Series(["a"])
364366

365-
for i, s in enumerate(ds.str):
366-
pass
367+
with tm.assert_produces_warning(DeprecationWarning):
368+
for i, s in enumerate(ds.str):
369+
pass
367370

368371
assert not i
369372
tm.assert_series_equal(ds, s)
@@ -373,8 +376,9 @@ def test_iter_object_try_string(self):
373376

374377
i, s = 100, "h"
375378

376-
for i, s in enumerate(ds.str):
377-
pass
379+
with tm.assert_produces_warning(DeprecationWarning):
380+
for i, s in enumerate(ds.str):
381+
pass
378382

379383
assert i == 100
380384
assert s == "h"

0 commit comments

Comments
 (0)