From f69a46686904f3ad58610f20d08f7acaa817d073 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 2 Dec 2019 18:30:23 -0800 Subject: [PATCH 1/3] DEPR: remove ptp, real, imag, put, and str.partitiion kwarg --- doc/source/whatsnew/v1.0.0.rst | 4 ++ pandas/core/generic.py | 34 --------------- pandas/core/series.py | 60 +-------------------------- pandas/core/strings.py | 7 +--- pandas/tests/series/test_analytics.py | 36 ++-------------- pandas/tests/series/test_dtypes.py | 20 --------- pandas/tests/series/test_internals.py | 7 ---- pandas/tests/test_strings.py | 12 +++--- 8 files changed, 14 insertions(+), 166 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 470209a7f4a33..2fbe4ba029e43 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -562,6 +562,10 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more. - Passing multiple axes to :meth:`DataFrame.dropna` is no longer supported (:issue:`20995`) - Removed previously deprecated :meth:`Series.nonzero`, use `to_numpy().nonzero()` instead (:issue:`24048`) - Passing floating dtype ``codes`` to :meth:`Categorical.from_codes` is no longer supported, pass ``codes.astype(np.int64)`` instead (:issue:`21775`) +- :meth:`Series.str.partition` and :meth:`Series.str.rpartition` no longer accept "pat" keyword, use "sep" instead (:issue:`23767`) +- Removed the previously deprecated :meth:`Series.ptp`, use `numpy.ptp` instead (:issue:`21614`) +- Removed the previously deprecated :meth:`Series.put` (:issue:`27106`) +- Removed the previously deprecated :attr:`Series.real`, :attr:`Series.imag` (:issue:`27106`) - Removed the previously deprecated :meth:`Series.to_dense`, :meth:`DataFrame.to_dense` (:issue:`26684`) - Removed the previously deprecated :meth:`Index.dtype_str`, use ``str(index.dtype)`` instead (:issue:`27106`) - :meth:`Categorical.ravel` returns a :class:`Categorical` instead of a ``ndarray`` (:issue:`27199`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e19bf9c1c39ea..546e6abc9311c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -10266,40 +10266,6 @@ def compound(self, axis=None, skipna=None, level=None): _min_examples, ) - @classmethod - def _add_series_only_operations(cls): - """ - Add the series only operations to the cls; evaluate the doc - strings again. - """ - - axis_descr, name, name2 = _doc_parms(cls) - - def nanptp(values, axis=0, skipna=True): - nmax = nanops.nanmax(values, axis, skipna) - nmin = nanops.nanmin(values, axis, skipna) - warnings.warn( - "Method .ptp is deprecated and will be removed " - "in a future version. Use numpy.ptp instead.", - FutureWarning, - stacklevel=4, - ) - return nmax - nmin - - cls.ptp = _make_stat_function( - cls, - "ptp", - name, - name2, - axis_descr, - """Return the difference between the min and max value. - \n.. deprecated:: 0.24.0 Use numpy.ptp instead - \nReturn the difference between the maximum value and the - minimum value in the object. This is the equivalent of the - ``numpy.ndarray`` method ``ptp``.""", - nanptp, - ) - @classmethod def _add_series_or_dataframe_operations(cls): """ diff --git a/pandas/core/series.py b/pandas/core/series.py index 11e87a4eed27f..ba8476cb14b6f 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -158,7 +158,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame): _deprecations = ( base.IndexOpsMixin._deprecations | generic.NDFrame._deprecations - | frozenset(["compress", "valid", "real", "imag", "put", "ptp", "nonzero"]) + | frozenset(["compress"]) ) # Override cache_readonly bc Series is mutable @@ -528,23 +528,6 @@ def compress(self, condition, *args, **kwargs): nv.validate_compress(args, kwargs) return self[condition] - def put(self, *args, **kwargs): - """ - Apply the `put` method to its `values` attribute if it has one. - - .. deprecated:: 0.25.0 - - See Also - -------- - numpy.ndarray.put - """ - warnings.warn( - "`put` has been deprecated and will be removed in a future version.", - FutureWarning, - stacklevel=2, - ) - self._values.put(*args, **kwargs) - def __len__(self) -> int: """ Return the length of the Series. @@ -777,46 +760,6 @@ def __array__(self, dtype=None): # ---------------------------------------------------------------------- # Unary Methods - @property - def real(self): - """ - Return the real value of vector. - - .. deprecated:: 0.25.0 - """ - warnings.warn( - "`real` is deprecated and will be removed in a future version. " - "To eliminate this warning for a Series `ser`, use " - "`np.real(ser.to_numpy())` or `ser.to_numpy().real`.", - FutureWarning, - stacklevel=2, - ) - return self.values.real - - @real.setter - def real(self, v): - self.values.real = v - - @property - def imag(self): - """ - Return imag value of vector. - - .. deprecated:: 0.25.0 - """ - warnings.warn( - "`imag` is deprecated and will be removed in a future version. " - "To eliminate this warning for a Series `ser`, use " - "`np.imag(ser.to_numpy())` or `ser.to_numpy().imag`.", - FutureWarning, - stacklevel=2, - ) - return self.values.imag - - @imag.setter - def imag(self, v): - self.values.imag = v - # coercion __float__ = _coerce_method(float) __long__ = _coerce_method(int) @@ -4488,7 +4431,6 @@ def to_period(self, freq=None, copy=True): docs={"index": "The index (axis labels) of the Series."}, ) Series._add_numeric_operations() -Series._add_series_only_operations() Series._add_series_or_dataframe_operations() # Add arithmetic! diff --git a/pandas/core/strings.py b/pandas/core/strings.py index 137c37f938dfa..2d136de8b3424 100644 --- a/pandas/core/strings.py +++ b/pandas/core/strings.py @@ -9,7 +9,7 @@ import pandas._libs.lib as lib import pandas._libs.ops as libops -from pandas.util._decorators import Appender, deprecate_kwarg +from pandas.util._decorators import Appender from pandas.core.dtypes.common import ( ensure_object, @@ -2630,9 +2630,6 @@ def rsplit(self, pat=None, n=-1, expand=False): ---------- sep : str, default whitespace String to split on. - pat : str, default whitespace - .. deprecated:: 0.24.0 - Use ``sep`` instead. expand : bool, default True If True, return DataFrame/MultiIndex expanding dimensionality. If False, return Series/Index. @@ -2710,7 +2707,6 @@ def rsplit(self, pat=None, n=-1, expand=False): "also": "rpartition : Split the string at the last occurrence of `sep`.", } ) - @deprecate_kwarg(old_arg_name="pat", new_arg_name="sep") @forbid_nonstring_types(["bytes"]) def partition(self, sep=" ", expand=True): f = lambda x: x.partition(sep) @@ -2726,7 +2722,6 @@ def partition(self, sep=" ", expand=True): "also": "partition : Split the string at the first occurrence of `sep`.", } ) - @deprecate_kwarg(old_arg_name="pat", new_arg_name="sep") @forbid_nonstring_types(["bytes"]) def rpartition(self, sep=" ", expand=True): f = lambda x: x.rpartition(sep) diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index fe9306a06efc7..48f3f242bfb5d 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -857,41 +857,11 @@ def test_ptp(self): N = 1000 arr = np.random.randn(N) ser = Series(arr) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - assert np.ptp(ser) == np.ptp(arr) + assert np.ptp(ser) == np.ptp(arr) # GH11163 - s = Series([3, 5, np.nan, -3, 10]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - assert s.ptp() == 13 - assert pd.isna(s.ptp(skipna=False)) - - mi = pd.MultiIndex.from_product([["a", "b"], [1, 2, 3]]) - s = pd.Series([1, np.nan, 7, 3, 5, np.nan], index=mi) - - expected = pd.Series([6, 2], index=["a", "b"], dtype=np.float64) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - tm.assert_series_equal(s.ptp(level=0), expected) - - expected = pd.Series([np.nan, np.nan], index=["a", "b"]) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - tm.assert_series_equal(s.ptp(level=0, skipna=False), expected) - - msg = "No axis named 1 for object type " - with pytest.raises(ValueError, match=msg): - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - s.ptp(axis=1) - - s = pd.Series(["a", "b", "c", "d", "e"]) - msg = r"unsupported operand type\(s\) for -: 'str' and 'str'" - with pytest.raises(TypeError, match=msg): - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - s.ptp() - - msg = r"Series\.ptp does not implement numeric_only\." - with pytest.raises(NotImplementedError, match=msg): - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - s.ptp(numeric_only=True) + # TODO: this used to have tests using Series.ptp, should we + # re-implement the analogous test using np.ptp? def test_repeat(self): s = Series(np.random.randn(3), index=["a", "b", "c"]) diff --git a/pandas/tests/series/test_dtypes.py b/pandas/tests/series/test_dtypes.py index 0dc64651e8d58..065be966efa49 100644 --- a/pandas/tests/series/test_dtypes.py +++ b/pandas/tests/series/test_dtypes.py @@ -411,26 +411,6 @@ def test_astype_empty_constructor_equality(self, dtype): as_type_empty = Series([]).astype(dtype) tm.assert_series_equal(init_empty, as_type_empty) - @pytest.mark.filterwarnings("ignore::FutureWarning") - def test_complex(self): - # see gh-4819: complex access for ndarray compat - a = np.arange(5, dtype=np.float64) - b = Series(a + 4j * a) - - tm.assert_numpy_array_equal(a, np.real(b)) - tm.assert_numpy_array_equal(4 * a, np.imag(b)) - - b.real = np.arange(5) + 5 - tm.assert_numpy_array_equal(a + 5, np.real(b)) - tm.assert_numpy_array_equal(4 * a, np.imag(b)) - - def test_real_imag_deprecated(self): - # GH 18262 - s = pd.Series([1]) - with tm.assert_produces_warning(FutureWarning): - s.imag - s.real - def test_arg_for_errors_in_astype(self): # see gh-14878 s = Series([1, 2, 3]) diff --git a/pandas/tests/series/test_internals.py b/pandas/tests/series/test_internals.py index 187c5d90407ce..efcb500a0b79f 100644 --- a/pandas/tests/series/test_internals.py +++ b/pandas/tests/series/test_internals.py @@ -242,10 +242,3 @@ def test_hasnans_unchached_for_series(): ser.iloc[-1] = np.nan assert ser.hasnans is True assert Series.hasnans.__doc__ == pd.Index.hasnans.__doc__ - - -def test_put_deprecated(): - # GH 18262 - s = pd.Series([1]) - with tm.assert_produces_warning(FutureWarning): - s.put(0, 0) diff --git a/pandas/tests/test_strings.py b/pandas/tests/test_strings.py index 0e2f8ee6543e1..cf52e286a47a5 100644 --- a/pandas/tests/test_strings.py +++ b/pandas/tests/test_strings.py @@ -2966,23 +2966,21 @@ def test_partition_with_name(self): assert res.nlevels == 1 tm.assert_index_equal(res, exp) - def test_partition_deprecation(self): + def test_partition_sep_kwarg(self): # GH 22676; depr kwarg "pat" in favor of "sep" values = Series(["a_b_c", "c_d_e", np.nan, "f_g_h"]) # str.partition # using sep -> no warning expected = values.str.partition(sep="_") - with tm.assert_produces_warning(FutureWarning): - result = values.str.partition(pat="_") - tm.assert_frame_equal(result, expected) + result = values.str.partition("_") + tm.assert_frame_equal(result, expected) # str.rpartition # using sep -> no warning expected = values.str.rpartition(sep="_") - with tm.assert_produces_warning(FutureWarning): - result = values.str.rpartition(pat="_") - tm.assert_frame_equal(result, expected) + result = values.str.rpartition("_") + tm.assert_frame_equal(result, expected) def test_pipe_failures(self): # #2119 From 4b1453b98b507d7423f33d18cc60756319138edb Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 3 Dec 2019 09:56:53 -0800 Subject: [PATCH 2/3] revert removal of ptp --- doc/source/whatsnew/v1.0.0.rst | 1 - pandas/core/generic.py | 34 +++++++++++++++++++++++++ pandas/core/series.py | 3 ++- pandas/tests/series/test_analytics.py | 36 ++++++++++++++++++++++++--- 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index 2fbe4ba029e43..01fad9b9a0970 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -563,7 +563,6 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more. - Removed previously deprecated :meth:`Series.nonzero`, use `to_numpy().nonzero()` instead (:issue:`24048`) - Passing floating dtype ``codes`` to :meth:`Categorical.from_codes` is no longer supported, pass ``codes.astype(np.int64)`` instead (:issue:`21775`) - :meth:`Series.str.partition` and :meth:`Series.str.rpartition` no longer accept "pat" keyword, use "sep" instead (:issue:`23767`) -- Removed the previously deprecated :meth:`Series.ptp`, use `numpy.ptp` instead (:issue:`21614`) - Removed the previously deprecated :meth:`Series.put` (:issue:`27106`) - Removed the previously deprecated :attr:`Series.real`, :attr:`Series.imag` (:issue:`27106`) - Removed the previously deprecated :meth:`Series.to_dense`, :meth:`DataFrame.to_dense` (:issue:`26684`) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 546e6abc9311c..e19bf9c1c39ea 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -10266,6 +10266,40 @@ def compound(self, axis=None, skipna=None, level=None): _min_examples, ) + @classmethod + def _add_series_only_operations(cls): + """ + Add the series only operations to the cls; evaluate the doc + strings again. + """ + + axis_descr, name, name2 = _doc_parms(cls) + + def nanptp(values, axis=0, skipna=True): + nmax = nanops.nanmax(values, axis, skipna) + nmin = nanops.nanmin(values, axis, skipna) + warnings.warn( + "Method .ptp is deprecated and will be removed " + "in a future version. Use numpy.ptp instead.", + FutureWarning, + stacklevel=4, + ) + return nmax - nmin + + cls.ptp = _make_stat_function( + cls, + "ptp", + name, + name2, + axis_descr, + """Return the difference between the min and max value. + \n.. deprecated:: 0.24.0 Use numpy.ptp instead + \nReturn the difference between the maximum value and the + minimum value in the object. This is the equivalent of the + ``numpy.ndarray`` method ``ptp``.""", + nanptp, + ) + @classmethod def _add_series_or_dataframe_operations(cls): """ diff --git a/pandas/core/series.py b/pandas/core/series.py index ba8476cb14b6f..3f665f0b202c1 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -158,7 +158,7 @@ class Series(base.IndexOpsMixin, generic.NDFrame): _deprecations = ( base.IndexOpsMixin._deprecations | generic.NDFrame._deprecations - | frozenset(["compress"]) + | frozenset(["compress", "ptp"]) ) # Override cache_readonly bc Series is mutable @@ -4431,6 +4431,7 @@ def to_period(self, freq=None, copy=True): docs={"index": "The index (axis labels) of the Series."}, ) Series._add_numeric_operations() +Series._add_series_only_operations() Series._add_series_or_dataframe_operations() # Add arithmetic! diff --git a/pandas/tests/series/test_analytics.py b/pandas/tests/series/test_analytics.py index 48f3f242bfb5d..fe9306a06efc7 100644 --- a/pandas/tests/series/test_analytics.py +++ b/pandas/tests/series/test_analytics.py @@ -857,11 +857,41 @@ def test_ptp(self): N = 1000 arr = np.random.randn(N) ser = Series(arr) - assert np.ptp(ser) == np.ptp(arr) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + assert np.ptp(ser) == np.ptp(arr) # GH11163 - # TODO: this used to have tests using Series.ptp, should we - # re-implement the analogous test using np.ptp? + s = Series([3, 5, np.nan, -3, 10]) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + assert s.ptp() == 13 + assert pd.isna(s.ptp(skipna=False)) + + mi = pd.MultiIndex.from_product([["a", "b"], [1, 2, 3]]) + s = pd.Series([1, np.nan, 7, 3, 5, np.nan], index=mi) + + expected = pd.Series([6, 2], index=["a", "b"], dtype=np.float64) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + tm.assert_series_equal(s.ptp(level=0), expected) + + expected = pd.Series([np.nan, np.nan], index=["a", "b"]) + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + tm.assert_series_equal(s.ptp(level=0, skipna=False), expected) + + msg = "No axis named 1 for object type " + with pytest.raises(ValueError, match=msg): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + s.ptp(axis=1) + + s = pd.Series(["a", "b", "c", "d", "e"]) + msg = r"unsupported operand type\(s\) for -: 'str' and 'str'" + with pytest.raises(TypeError, match=msg): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + s.ptp() + + msg = r"Series\.ptp does not implement numeric_only\." + with pytest.raises(NotImplementedError, match=msg): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + s.ptp(numeric_only=True) def test_repeat(self): s = Series(np.random.randn(3), index=["a", "b", "c"]) From 96b47c5d362d4f05d90455b858819419904361f1 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 3 Dec 2019 18:16:40 -0800 Subject: [PATCH 3/3] fixup docs --- doc/redirects.csv | 1 - doc/source/reference/series.rst | 1 - 2 files changed, 2 deletions(-) diff --git a/doc/redirects.csv b/doc/redirects.csv index 599ad6d28a8f5..61902f3134a4d 100644 --- a/doc/redirects.csv +++ b/doc/redirects.csv @@ -1119,7 +1119,6 @@ generated/pandas.Series.pow,../reference/api/pandas.Series.pow generated/pandas.Series.prod,../reference/api/pandas.Series.prod generated/pandas.Series.product,../reference/api/pandas.Series.product generated/pandas.Series.ptp,../reference/api/pandas.Series.ptp -generated/pandas.Series.put,../reference/api/pandas.Series.put generated/pandas.Series.quantile,../reference/api/pandas.Series.quantile generated/pandas.Series.radd,../reference/api/pandas.Series.radd generated/pandas.Series.rank,../reference/api/pandas.Series.rank diff --git a/doc/source/reference/series.rst b/doc/source/reference/series.rst index 2485b94ab4d09..e13b4ed98a38b 100644 --- a/doc/source/reference/series.rst +++ b/doc/source/reference/series.rst @@ -39,7 +39,6 @@ Attributes Series.empty Series.dtypes Series.name - Series.put Conversion ----------