From 84c75d3244348122afcb3afaa50e5a6cef22f4b9 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 28 Jun 2019 17:10:46 -0500 Subject: [PATCH 1/3] Deprecate item --- pandas/core/base.py | 4 ++++ pandas/core/indexes/base.py | 7 ------- pandas/core/indexes/numeric.py | 9 ++++----- pandas/core/indexes/period.py | 5 +++++ pandas/tests/series/test_api.py | 9 +++++---- pandas/tests/test_base.py | 8 +++++--- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 30e800cb9bd73..93db65deff820 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -693,11 +693,15 @@ def item(self): """ Return the first element of the underlying data as a python scalar. + .. deprecated 0.25.0 + Returns ------- scalar The first element of %(klass)s. """ + warnings.warn('`item` has been deprecated and will be removed in a ' + 'future version', FutureWarning, stacklevel=2) return self.values.item() @property diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index c96d9e2c5f77a..ed985526210f5 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -57,13 +57,6 @@ _index_shared_docs = dict() -def _try_get_item(x): - try: - return x.item() - except AttributeError: - return x - - def _make_comparison_op(op, cls): def cmp_method(self, other): if isinstance(other, (np.ndarray, Index, ABCSeries)): diff --git a/pandas/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index a228895e527aa..5f9c1f22887cc 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -16,7 +16,6 @@ from pandas.core import algorithms import pandas.core.common as com -import pandas.core.indexes.base as ibase from pandas.core.indexes.base import ( Index, InvalidIndexError, _index_shared_docs) from pandas.core.ops import get_op_result_name @@ -442,7 +441,9 @@ def __contains__(self, other): return np.isnan(other) and self.hasnans except ValueError: try: - return len(other) <= 1 and ibase._try_get_item(other) in self + return len(other) <= 1 and other.item() in self + except AttributeError: + return len(other) <= 1 and other in self except TypeError: pass except TypeError: @@ -457,9 +458,7 @@ def get_loc(self, key, method=None, tolerance=None): nan_idxs = self._nan_idxs try: return nan_idxs.item() - except (ValueError, IndexError): - # should only need to catch ValueError here but on numpy - # 1.7 .item() can raise IndexError when NaNs are present + except ValueError: if not len(nan_idxs): raise KeyError(key) return nan_idxs diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index b20b0c6f853d9..dc11099c3e903 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -874,7 +874,12 @@ def item(self): """ return the first element of the underlying data as a python scalar + + .. deprecated 0.25.0 + """ + warnings.warn('`item` has been deprecated and will be removed in a ' + 'future version', FutureWarning, stacklevel=2) # TODO(DatetimeArray): remove if len(self) == 1: return self[0] diff --git a/pandas/tests/series/test_api.py b/pandas/tests/series/test_api.py index fac796fbf325a..1cd5bd09a82e7 100644 --- a/pandas/tests/series/test_api.py +++ b/pandas/tests/series/test_api.py @@ -419,10 +419,11 @@ def f(x): tm.assert_series_equal(result, expected) # .item() - s = Series([1]) - result = s.item() - assert result == 1 - assert s.item() == s.iloc[0] + with tm.assert_produces_warning(FutureWarning): + s = Series([1]) + result = s.item() + assert result == 1 + assert s.item() == s.iloc[0] # using an ndarray like function s = Series(np.random.randn(10)) diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index d82b205803b09..bfe1903396818 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -326,13 +326,15 @@ def test_ndarray_compat_properties(self): pass with pytest.raises(ValueError): - o.item() # len > 1 + with pytest.raises(FutureWarning): + o.item() # len > 1 assert o.ndim == 1 assert o.size == len(o) - assert Index([1]).item() == 1 - assert Series([1]).item() == 1 + with tm.assert_produces_warning(FutureWarning): + assert Index([1]).item() == 1 + assert Series([1]).item() == 1 def test_value_counts_unique_nunique(self): for orig in self.objs: From 4f46c3325577c08d9711e50734b0e4f44ada135f Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 28 Jun 2019 17:13:12 -0500 Subject: [PATCH 2/3] Add whatsnew --- doc/source/whatsnew/v0.25.0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 27a72014a9f8e..abc427c36c423 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -604,7 +604,8 @@ Other deprecations - The :meth:`Series.ftype`, :meth:`Series.ftypes` and :meth:`DataFrame.ftypes` methods are deprecated and will be removed in a future version. Instead, use :meth:`Series.dtype` and :meth:`DataFrame.dtypes` (:issue:`26705`). - :meth:`Timedelta.resolution` is deprecated and replaced with :meth:`Timedelta.resolution_string`. In a future version, :meth:`Timedelta.resolution` will be changed to behave like the standard library :attr:`timedelta.resolution` (:issue:`21344`) -- func:`read_table` has been undeprecated. (:issue:`25220`) +- :func:`read_table` has been undeprecated. (:issue:`25220`) +- :meth:`Index.item` and :meth:`Series.item` is deprecated (:issue:`18262`) .. _whatsnew_0250.prior_deprecations: From 8261bdd9630347853cd0d09126d98138341be125 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Fri, 28 Jun 2019 18:01:45 -0500 Subject: [PATCH 3/3] Use assert_produces_warning instead --- pandas/tests/test_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py index bfe1903396818..f9a1bb97cc48c 100644 --- a/pandas/tests/test_base.py +++ b/pandas/tests/test_base.py @@ -326,7 +326,7 @@ def test_ndarray_compat_properties(self): pass with pytest.raises(ValueError): - with pytest.raises(FutureWarning): + with tm.assert_produces_warning(FutureWarning): o.item() # len > 1 assert o.ndim == 1