Skip to content

Commit 08c32ea

Browse files
SaturnFromTitanadmin
authored and
admin
committed
Added DeprecationWarning to Series.str.__iter__
1 parent ed20822 commit 08c32ea

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

doc/source/whatsnew/v0.12.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ Bug fixes
417417
original ``Series`` or ``NaN``. For example,
418418

419419
.. ipython:: python
420+
:okwarning:
420421
421422
strs = 'go', 'bow', 'joe', 'slow'
422423
ds = pd.Series(strs)

doc/source/whatsnew/v1.0.0.rst

+25
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,31 @@ When :class:`Categorical` contains ``np.nan``,
356356
357357
pd.Categorical([1, 2, np.nan], ordered=True).min()
358358
359+
Avoid iterating over ``Series.str``
360+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
361+
362+
Iterating over the `.str` accessor of :class:`Series` will now raise a
363+
`DeprecationWarning` and be removed in future releases (:issue:`28277`).
364+
Since no clear purpose of this feature could be derived, there will be
365+
no replacement.
366+
367+
.. code-block:: ipython
368+
369+
In [1]: s = pd.Series(["a", "ab"])
370+
In [2]: s
371+
Out[2]:
372+
0 a
373+
1 ab
374+
dtype: object
375+
376+
In [3]: generator = (_ for _ in s.str)
377+
In [4]: next(generator)
378+
Out[4]:
379+
DeprecationWarning: Columnar iteration over characters will be deprecated in future releases.
380+
0 a
381+
1 a
382+
dtype: object
383+
359384
.. _whatsnew_1000.api_breaking.deps:
360385

361386
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
@@ -327,17 +327,18 @@ def test_iter(self):
327327
strs = "google", "wikimedia", "wikipedia", "wikitravel"
328328
ds = Series(strs)
329329

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

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

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

342343
# desired behavior is to iterate until everything would be nan on the
343344
# next iter so make sure the last element of the iterator was 'l' in
@@ -349,8 +350,9 @@ def test_iter_empty(self):
349350

350351
i, s = 100, 1
351352

352-
for i, s in enumerate(ds.str):
353-
pass
353+
with tm.assert_produces_warning(DeprecationWarning):
354+
for i, s in enumerate(ds.str):
355+
pass
354356

355357
# nothing to iterate over so nothing defined values should remain
356358
# unchanged
@@ -360,8 +362,9 @@ def test_iter_empty(self):
360362
def test_iter_single_element(self):
361363
ds = Series(["a"])
362364

363-
for i, s in enumerate(ds.str):
364-
pass
365+
with tm.assert_produces_warning(DeprecationWarning):
366+
for i, s in enumerate(ds.str):
367+
pass
365368

366369
assert not i
367370
tm.assert_series_equal(ds, s)
@@ -371,8 +374,9 @@ def test_iter_object_try_string(self):
371374

372375
i, s = 100, "h"
373376

374-
for i, s in enumerate(ds.str):
375-
pass
377+
with tm.assert_produces_warning(DeprecationWarning):
378+
for i, s in enumerate(ds.str):
379+
pass
376380

377381
assert i == 100
378382
assert s == "h"

0 commit comments

Comments
 (0)