Skip to content

Commit 5f16b1f

Browse files
SaturnFromTitanproost
authored andcommitted
Added FutureWarning to Series.str.__iter__ (pandas-dev#29909)
1 parent b8251ec commit 5f16b1f

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-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

+1
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ Other API changes
467467
- :meth:`Series.dropna` has dropped its ``**kwargs`` argument in favor of a single ``how`` parameter.
468468
Supplying anything else than ``how`` to ``**kwargs`` raised a ``TypeError`` previously (:issue:`29388`)
469469
- When testing pandas, the new minimum required version of pytest is 5.0.1 (:issue:`29664`)
470+
- :meth:`Series.str.__iter__` was deprecated and will be removed in future releases (:issue:`28277`).
470471

471472

472473
.. _whatsnew_1000.api.documentation:

pandas/core/strings.py

+5
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,11 @@ def __getitem__(self, key):
20912091
return self.get(key)
20922092

20932093
def __iter__(self):
2094+
warnings.warn(
2095+
"Columnar iteration over characters will be deprecated in future releases.",
2096+
FutureWarning,
2097+
stacklevel=2,
2098+
)
20942099
i = 0
20952100
g = self.get(i)
20962101
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(FutureWarning):
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(FutureWarning):
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(FutureWarning):
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(FutureWarning):
378+
for i, s in enumerate(ds.str):
379+
pass
376380

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

0 commit comments

Comments
 (0)