From 2a40b7be11d0c7d136af776fcf10ceca38fcac20 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Thu, 28 Nov 2019 00:56:42 +0100 Subject: [PATCH 1/3] Added DeprecationWarning to Series.str.__iter__ --- doc/source/whatsnew/v0.12.0.rst | 1 + doc/source/whatsnew/v1.0.0.rst | 25 ++++++++++++++++++++++++ pandas/core/strings.py | 5 +++++ pandas/tests/test_strings.py | 34 ++++++++++++++++++--------------- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/doc/source/whatsnew/v0.12.0.rst b/doc/source/whatsnew/v0.12.0.rst index 0a74d67486715..86ff338536f80 100644 --- a/doc/source/whatsnew/v0.12.0.rst +++ b/doc/source/whatsnew/v0.12.0.rst @@ -417,6 +417,7 @@ Bug fixes original ``Series`` or ``NaN``. For example, .. ipython:: python + :okwarning: strs = 'go', 'bow', 'joe', 'slow' ds = pd.Series(strs) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 5c9543580be26..cfa9ba49f29e7 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -356,6 +356,31 @@ When :class:`Categorical` contains ``np.nan``, pd.Categorical([1, 2, np.nan], ordered=True).min() +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 diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 6cc102dce3b9c..bfb1cd75b4889 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -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.", + DeprecationWarning, + stacklevel=2, + ) i = 0 g = self.get(i) while g.notna().any(): diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index cf52e286a47a5..6326e88097115 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -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(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 @@ -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(DeprecationWarning): + for i, s in enumerate(ds.str): + pass # nothing to iterate over so nothing defined values should remain # unchanged @@ -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(DeprecationWarning): + for i, s in enumerate(ds.str): + pass assert not i tm.assert_series_equal(ds, s) @@ -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(DeprecationWarning): + for i, s in enumerate(ds.str): + pass assert i == 100 assert s == "h" From 435f60517c78fb9a0cd6181158c4c8b0cf8b0f24 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 30 Nov 2019 13:26:53 +0100 Subject: [PATCH 2/3] raising FutureWarning instead of DeprecationWarning --- doc/source/whatsnew/v1.0.0.rst | 4 ++-- pandas/core/strings.py | 2 +- pandas/tests/test_strings.py | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index cfa9ba49f29e7..7a41d4e4772aa 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -360,7 +360,7 @@ 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`). + `FutureWarning` and be removed in future releases (:issue:`28277`). Since no clear purpose of this feature could be derived, there will be no replacement. @@ -376,7 +376,7 @@ Since no clear purpose of this feature could be derived, there will be In [3]: generator = (_ for _ in s.str) In [4]: next(generator) Out[4]: - DeprecationWarning: Columnar iteration over characters will be deprecated in future releases. + FutureWarning: Columnar iteration over characters will be deprecated in future releases. 0 a 1 a dtype: object diff --git a/pandas/core/strings.py b/pandas/core/strings.py index bfb1cd75b4889..040a28ea2ee8c 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -2093,7 +2093,7 @@ def __getitem__(self, key): def __iter__(self): warnings.warn( "Columnar iteration over characters will be deprecated in future releases.", - DeprecationWarning, + FutureWarning, stacklevel=2, ) i = 0 diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index 6326e88097115..d08c8b1cc13e5 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -327,7 +327,7 @@ def test_iter(self): strs = "google", "wikimedia", "wikipedia", "wikitravel" ds = Series(strs) - with tm.assert_produces_warning(DeprecationWarning): + with tm.assert_produces_warning(FutureWarning): for s in ds.str: # iter must yield a Series assert isinstance(s, Series) @@ -350,7 +350,7 @@ def test_iter_empty(self): i, s = 100, 1 - with tm.assert_produces_warning(DeprecationWarning): + with tm.assert_produces_warning(FutureWarning): for i, s in enumerate(ds.str): pass @@ -362,7 +362,7 @@ def test_iter_empty(self): def test_iter_single_element(self): ds = Series(["a"]) - with tm.assert_produces_warning(DeprecationWarning): + with tm.assert_produces_warning(FutureWarning): for i, s in enumerate(ds.str): pass @@ -374,7 +374,7 @@ def test_iter_object_try_string(self): i, s = 100, "h" - with tm.assert_produces_warning(DeprecationWarning): + with tm.assert_produces_warning(FutureWarning): for i, s in enumerate(ds.str): pass From 61a2a060968c6bb68aa26aa46b15d0372d2101f1 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 1 Dec 2019 00:29:49 +0100 Subject: [PATCH 3/3] updated whatsnew entry --- doc/source/whatsnew/v1.0.0.rst | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 7a41d4e4772aa..9a41531855245 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -356,31 +356,6 @@ When :class:`Categorical` contains ``np.nan``, pd.Categorical([1, 2, np.nan], ordered=True).min() -Avoid iterating over ``Series.str`` -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -Iterating over the `.str` accessor of :class:`Series` will now raise a - `FutureWarning` 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]: - FutureWarning: 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 @@ -465,6 +440,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: