Skip to content

Commit dd11fc2

Browse files
authored
DEPR: Series.item and Index.item (#27112)
* Deprecate item * Add whatsnew * Use assert_produces_warning instead
1 parent d7d26be commit dd11fc2

File tree

7 files changed

+24
-19
lines changed

7 files changed

+24
-19
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ Other deprecations
608608
- :attr:`Index.dtype_str` is deprecated. (:issue:`18262`)
609609
- :attr:`Series.imag` and :attr:`Series.real` are deprecated. (:issue:`18262`)
610610
- :meth:`Series.put` is deprecated. (:issue:`18262`)
611+
- :meth:`Index.item` and :meth:`Series.item` is deprecated. (:issue:`18262`)
611612

612613
.. _whatsnew_0250.prior_deprecations:
613614

pandas/core/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,15 @@ def item(self):
693693
"""
694694
Return the first element of the underlying data as a python scalar.
695695
696+
.. deprecated 0.25.0
697+
696698
Returns
697699
-------
698700
scalar
699701
The first element of %(klass)s.
700702
"""
703+
warnings.warn('`item` has been deprecated and will be removed in a '
704+
'future version', FutureWarning, stacklevel=2)
701705
return self.values.item()
702706

703707
@property

pandas/core/indexes/base.py

-7
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@
5757
_index_shared_docs = dict()
5858

5959

60-
def _try_get_item(x):
61-
try:
62-
return x.item()
63-
except AttributeError:
64-
return x
65-
66-
6760
def _make_comparison_op(op, cls):
6861
def cmp_method(self, other):
6962
if isinstance(other, (np.ndarray, Index, ABCSeries)):

pandas/core/indexes/numeric.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
from pandas.core import algorithms
1818
import pandas.core.common as com
19-
import pandas.core.indexes.base as ibase
2019
from pandas.core.indexes.base import (
2120
Index, InvalidIndexError, _index_shared_docs)
2221
from pandas.core.ops import get_op_result_name
@@ -442,7 +441,9 @@ def __contains__(self, other):
442441
return np.isnan(other) and self.hasnans
443442
except ValueError:
444443
try:
445-
return len(other) <= 1 and ibase._try_get_item(other) in self
444+
return len(other) <= 1 and other.item() in self
445+
except AttributeError:
446+
return len(other) <= 1 and other in self
446447
except TypeError:
447448
pass
448449
except TypeError:
@@ -457,9 +458,7 @@ def get_loc(self, key, method=None, tolerance=None):
457458
nan_idxs = self._nan_idxs
458459
try:
459460
return nan_idxs.item()
460-
except (ValueError, IndexError):
461-
# should only need to catch ValueError here but on numpy
462-
# 1.7 .item() can raise IndexError when NaNs are present
461+
except ValueError:
463462
if not len(nan_idxs):
464463
raise KeyError(key)
465464
return nan_idxs

pandas/core/indexes/period.py

+5
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,12 @@ def item(self):
874874
"""
875875
return the first element of the underlying data as a python
876876
scalar
877+
878+
.. deprecated 0.25.0
879+
877880
"""
881+
warnings.warn('`item` has been deprecated and will be removed in a '
882+
'future version', FutureWarning, stacklevel=2)
878883
# TODO(DatetimeArray): remove
879884
if len(self) == 1:
880885
return self[0]

pandas/tests/series/test_api.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,11 @@ def f(x):
419419
tm.assert_series_equal(result, expected)
420420

421421
# .item()
422-
s = Series([1])
423-
result = s.item()
424-
assert result == 1
425-
assert s.item() == s.iloc[0]
422+
with tm.assert_produces_warning(FutureWarning):
423+
s = Series([1])
424+
result = s.item()
425+
assert result == 1
426+
assert s.item() == s.iloc[0]
426427

427428
# using an ndarray like function
428429
s = Series(np.random.randn(10))

pandas/tests/test_base.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,15 @@ def test_ndarray_compat_properties(self):
326326
pass
327327

328328
with pytest.raises(ValueError):
329-
o.item() # len > 1
329+
with tm.assert_produces_warning(FutureWarning):
330+
o.item() # len > 1
330331

331332
assert o.ndim == 1
332333
assert o.size == len(o)
333334

334-
assert Index([1]).item() == 1
335-
assert Series([1]).item() == 1
335+
with tm.assert_produces_warning(FutureWarning):
336+
assert Index([1]).item() == 1
337+
assert Series([1]).item() == 1
336338

337339
def test_value_counts_unique_nunique(self):
338340
for orig in self.objs:

0 commit comments

Comments
 (0)