Skip to content

DEPR: Series.item and Index.item #27112

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 4 commits into from
Jun 29, 2019
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
@@ -608,6 +608,7 @@ Other deprecations
- :attr:`Index.dtype_str` is deprecated. (:issue:`18262`)
- :attr:`Series.imag` and :attr:`Series.real` are deprecated. (:issue:`18262`)
- :meth:`Series.put` is deprecated. (:issue:`18262`)
- :meth:`Index.item` and :meth:`Series.item` is deprecated. (:issue:`18262`)

.. _whatsnew_0250.prior_deprecations:

4 changes: 4 additions & 0 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 0 additions & 7 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
@@ -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)):
9 changes: 4 additions & 5 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
@@ -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]
9 changes: 5 additions & 4 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
@@ -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))
8 changes: 5 additions & 3 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -326,13 +326,15 @@ def test_ndarray_compat_properties(self):
pass

with pytest.raises(ValueError):
o.item() # len > 1
with tm.assert_produces_warning(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: