Skip to content

DEP: Remove Series.str.__iter__ #49268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.12.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ Bug fixes
``Series`` or ``NaN``. For example,

.. ipython:: python
:okwarning:
:okexcept:

strs = "go", "bow", "joe", "slow"
ds = pd.Series(strs)
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ Removal of prior version deprecations/changes
- Removed the ``center`` keyword in :meth:`DataFrame.expanding` (:issue:`20647`)
- Removed the ``pandas.datetime`` submodule (:issue:`30489`)
- Removed the ``pandas.np`` submodule (:issue:`30296`)
- Removed :meth:`Series.str.__iter__` (:issue:`28277`)
- Removed ``pandas.SparseArray`` in favor of :class:`arrays.SparseArray` (:issue:`30642`)
- Removed ``pandas.SparseSeries`` and ``pandas.SparseDataFrame`` (:issue:`30642`)
- Enforced disallowing a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)
Expand Down
14 changes: 0 additions & 14 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
TYPE_CHECKING,
Callable,
Hashable,
Iterator,
Literal,
cast,
)
Expand Down Expand Up @@ -242,19 +241,6 @@ def __getitem__(self, key):
result = self._data.array._str_getitem(key)
return self._wrap_result(result)

def __iter__(self) -> Iterator:
warnings.warn(
"Columnar iteration over characters will be deprecated in future releases.",
FutureWarning,
stacklevel=find_stack_level(),
)
i = 0
g = self.get(i)
while g.notna().any():
yield g
i += 1
g = self.get(i)

def _wrap_result(
self,
result,
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ def shape(self):
((_ for _ in []), True, "generator-empty"),
(Series([1]), True, "Series"),
(Series([], dtype=object), True, "Series-empty"),
(Series(["a"]).str, True, "StringMethods"),
(Series([], dtype="O").str, True, "StringMethods-empty"),
(Series(["a"]).str, False, "StringMethods"),
(Series([], dtype="O").str, False, "StringMethods-empty"),
(Index([1]), True, "Index"),
(Index([]), True, "Index-empty"),
(DataFrame([[1]]), True, "DataFrame"),
Expand Down
23 changes: 10 additions & 13 deletions pandas/tests/strings/test_cat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import numpy as np
import pytest

Expand Down Expand Up @@ -380,18 +382,13 @@ def test_cat_different_classes(klass):

def test_cat_on_series_dot_str():
# GH 28277
# Test future warning of `Series.str.__iter__`
ps = Series(["AbC", "de", "FGHI", "j", "kLLLm"])
with tm.assert_produces_warning(FutureWarning):

message = re.escape(
"others must be Series, Index, DataFrame, np.ndarray "
"or list-like (either containing only strings or "
"containing only objects of type Series/Index/"
"np.ndarray[1-dim])"
)
with pytest.raises(TypeError, match=message):
ps.str.cat(others=ps.str)
# TODO(2.0): The following code can be uncommented
# when `Series.str.__iter__` is removed.

# message = re.escape(
# "others must be Series, Index, DataFrame, np.ndarray "
# "or list-like (either containing only strings or "
# "containing only objects of type Series/Index/"
# "np.ndarray[1-dim])"
# )
# with pytest.raises(TypeError, match=message):
# ps.str.cat(others=ps.str)
69 changes: 0 additions & 69 deletions pandas/tests/strings/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Index,
MultiIndex,
Series,
isna,
)
import pandas._testing as tm

Expand All @@ -34,74 +33,6 @@ def assert_series_or_index_equal(left, right):
tm.assert_index_equal(left, right)


def test_iter():
# GH3638
strs = "google", "wikimedia", "wikipedia", "wikitravel"
ser = Series(strs)

with tm.assert_produces_warning(FutureWarning):
for s in ser.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, ser.index)

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
# this case since 'wikitravel' is the longest string
assert s.dropna().values.item() == "l"


def test_iter_empty(any_string_dtype):
ser = Series([], dtype=any_string_dtype)

i, s = 100, 1

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

# nothing to iterate over so nothing defined values should remain
# unchanged
assert i == 100
assert s == 1


def test_iter_single_element(any_string_dtype):
ser = Series(["a"], dtype=any_string_dtype)

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

assert not i
tm.assert_series_equal(ser, s)


def test_iter_object_try_string():
ser = Series(
[
slice(None, np.random.randint(10), np.random.randint(10, 20))
for _ in range(4)
]
)

i, s = 100, "h"

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

assert i == 100
assert s == "h"


# test integer/float dtypes (inferred by constructor) and mixed


Expand Down