Skip to content

Commit

Permalink
Added FutureWarning to Series.str.__iter__ (pandas-dev#29909)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaturnFromTitan authored and proost committed Dec 19, 2019
1 parent b8251ec commit 5f16b1f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.12.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ Bug fixes
original ``Series`` or ``NaN``. For example,

.. ipython:: python
:okwarning:
strs = 'go', 'bow', 'joe', 'slow'
ds = pd.Series(strs)
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ Other API changes
- :meth:`Series.dropna` has dropped its ``**kwargs`` argument in favor of a single ``how`` parameter.
Supplying anything else than ``how`` to ``**kwargs`` raised a ``TypeError`` previously (:issue:`29388`)
- When testing pandas, the new minimum required version of pytest is 5.0.1 (:issue:`29664`)
- :meth:`Series.str.__iter__` was deprecated and will be removed in future releases (:issue:`28277`).


.. _whatsnew_1000.api.documentation:
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,11 @@ def __getitem__(self, key):
return self.get(key)

def __iter__(self):
warnings.warn(
"Columnar iteration over characters will be deprecated in future releases.",
FutureWarning,
stacklevel=2,
)
i = 0
g = self.get(i)
while g.notna().any():
Expand Down
34 changes: 19 additions & 15 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,18 @@ def test_iter(self):
strs = "google", "wikimedia", "wikipedia", "wikitravel"
ds = Series(strs)

for s in ds.str:
# iter must yield a Series
assert isinstance(s, Series)
with tm.assert_produces_warning(FutureWarning):
for s in ds.str:
# iter must yield a Series
assert isinstance(s, Series)

# indices of each yielded Series should be equal to the index of
# the original Series
tm.assert_index_equal(s.index, ds.index)
# indices of each yielded Series should be equal to the index of
# the original Series
tm.assert_index_equal(s.index, ds.index)

for el in s:
# each element of the series is either a basestring/str or nan
assert isinstance(el, str) or isna(el)
for el in s:
# each element of the series is either a basestring/str or nan
assert isinstance(el, str) or isna(el)

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

i, s = 100, 1

for i, s in enumerate(ds.str):
pass
with tm.assert_produces_warning(FutureWarning):
for i, s in enumerate(ds.str):
pass

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

for i, s in enumerate(ds.str):
pass
with tm.assert_produces_warning(FutureWarning):
for i, s in enumerate(ds.str):
pass

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

i, s = 100, "h"

for i, s in enumerate(ds.str):
pass
with tm.assert_produces_warning(FutureWarning):
for i, s in enumerate(ds.str):
pass

assert i == 100
assert s == "h"
Expand Down

0 comments on commit 5f16b1f

Please sign in to comment.