Skip to content

Commit

Permalink
Added DeprecationWarning to Series.str.__iter__
Browse files Browse the repository at this point in the history
  • Loading branch information
SaturnFromTitan committed Nov 28, 2019
1 parent bc75e19 commit ab8c473
Show file tree
Hide file tree
Showing 4 changed files with 50 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
25 changes: 25 additions & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,31 @@ The following methods now also correctly output values for unobserved categories
df.groupby(["cat_1", "cat_2"], observed=False)["value"].count()
Avoid iterating over ``Series.str``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Iterating over the `.str` accessor of :class:`Series` will now raise a
`DeprecationWarning` and be removed in future releases (:issue:`28277`).
Since no clear purpose of this feature could be derived, there will be
no replacement.

.. code-block:: ipython
In [1]: s = pd.Series(["a", "ab"])
In [2]: s
Out[2]:
0 a
1 ab
dtype: object
In [3]: generator = (_ for _ in s.str)
In [4]: next(generator)
Out[4]:
DeprecationWarning: Columnar iteration over characters will be deprecated in future releases.
0 a
1 a
dtype: object
.. _whatsnew_1000.api_breaking.deps:

Increased minimum versions for dependencies
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2093,6 +2093,11 @@ def __getitem__(self, key):
return self.get(key)

def __iter__(self):
warnings.warn(
"Columnar iteration over characters will be deprecated in future releases.",
DeprecationWarning,
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 @@ -329,17 +329,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(DeprecationWarning):
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 @@ -351,8 +352,9 @@ def test_iter_empty(self):

i, s = 100, 1

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

# nothing to iterate over so nothing defined values should remain
# unchanged
Expand All @@ -362,8 +364,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(DeprecationWarning):
for i, s in enumerate(ds.str):
pass

assert not i
tm.assert_series_equal(ds, s)
Expand All @@ -373,8 +376,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(DeprecationWarning):
for i, s in enumerate(ds.str):
pass

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

0 comments on commit ab8c473

Please sign in to comment.