diff --git a/doc/source/whatsnew/v0.12.0.rst b/doc/source/whatsnew/v0.12.0.rst index c12adb2f1334f..6a8008b1e7c82 100644 --- a/doc/source/whatsnew/v0.12.0.rst +++ b/doc/source/whatsnew/v0.12.0.rst @@ -434,7 +434,7 @@ Bug fixes ``Series`` or ``NaN``. For example, .. ipython:: python - :okwarning: + :okexcept: strs = "go", "bow", "joe", "slow" ds = pd.Series(strs) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 6239ddf9442e7..c1619af190613 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -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`) diff --git a/pandas/core/strings/accessor.py b/pandas/core/strings/accessor.py index 67eb4932a6a5c..ff9c72b74e5eb 100644 --- a/pandas/core/strings/accessor.py +++ b/pandas/core/strings/accessor.py @@ -7,7 +7,6 @@ TYPE_CHECKING, Callable, Hashable, - Iterator, Literal, cast, ) @@ -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, diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 46bc6d82b55db..56c97ac7a4dc5 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -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"), diff --git a/pandas/tests/strings/test_cat.py b/pandas/tests/strings/test_cat.py index 4decdff8063a8..01c5bf25e0601 100644 --- a/pandas/tests/strings/test_cat.py +++ b/pandas/tests/strings/test_cat.py @@ -1,3 +1,5 @@ +import re + import numpy as np import pytest @@ -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) diff --git a/pandas/tests/strings/test_strings.py b/pandas/tests/strings/test_strings.py index d62858f139b0b..beda123facb26 100644 --- a/pandas/tests/strings/test_strings.py +++ b/pandas/tests/strings/test_strings.py @@ -11,7 +11,6 @@ Index, MultiIndex, Series, - isna, ) import pandas._testing as tm @@ -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