Skip to content

DEPR: remove Index.get_value #49208

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 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
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: 0 additions & 1 deletion doc/redirects.csv
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,6 @@ generated/pandas.Index.get_indexer_non_unique,../reference/api/pandas.Index.get_
generated/pandas.Index.get_level_values,../reference/api/pandas.Index.get_level_values
generated/pandas.Index.get_loc,../reference/api/pandas.Index.get_loc
generated/pandas.Index.get_slice_bound,../reference/api/pandas.Index.get_slice_bound
generated/pandas.Index.get_value,../reference/api/pandas.Index.get_value
generated/pandas.Index.groupby,../reference/api/pandas.Index.groupby
generated/pandas.Index.has_duplicates,../reference/api/pandas.Index.has_duplicates
generated/pandas.Index.hasnans,../reference/api/pandas.Index.hasnans
Expand Down
1 change: 0 additions & 1 deletion doc/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ Selecting
Index.get_level_values
Index.get_loc
Index.get_slice_bound
Index.get_value
Index.isin
Index.slice_indexer
Index.slice_locs
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
- Removed :func:`is_extension_type` in favor of :func:`is_extension_array_dtype` (:issue:`29457`)
- Removed :meth:`Index.get_value` (:issue:`33907`)
- Remove :meth:`DataFrameGroupBy.pad` and :meth:`DataFrameGroupBy.backfill` (:issue:`45076`)
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)

Expand Down
39 changes: 0 additions & 39 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5981,45 +5981,6 @@ def argsort(self, *args, **kwargs) -> npt.NDArray[np.intp]:
# by RangeIndex, MultIIndex
return self._data.argsort(*args, **kwargs)

@final
def get_value(self, series: Series, key):
"""
Fast lookup of value from 1-dimensional ndarray.

Only use this if you know what you're doing.

Returns
-------
scalar or Series
"""
warnings.warn(
"get_value is deprecated and will be removed in a future version. "
"Use Series[key] instead.",
FutureWarning,
stacklevel=find_stack_level(),
)

self._check_indexing_error(key)

try:
# GH 20882, 21257
# First try to convert the key to a location
# If that fails, raise a KeyError if an integer
# index, otherwise, see if key is an integer, and
# try that
loc = self.get_loc(key)
except KeyError:
if not self._should_fallback_to_positional:
raise
elif is_integer(key):
# If the Index cannot hold integer, then this is unambiguously
# a locational lookup.
loc = key
else:
raise

return self._get_values_for_loc(series, loc, key)

def _check_indexing_error(self, key):
if not is_scalar(key):
# if key is not a scalar, directly raise an error (the code below
Expand Down
27 changes: 0 additions & 27 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,33 +710,6 @@ def test_maybe_cast_slice_duplicate_monotonic(self):
assert result == expected


class TestGetValue:
def test_get_value(self):
# specifically make sure we have test for np.datetime64 key
dti = date_range("2016-01-01", periods=3)

arr = np.arange(6, 9)
ser = pd.Series(arr, index=dti)

key = dti[1]

with pytest.raises(AttributeError, match="has no attribute '_values'"):
with tm.assert_produces_warning(FutureWarning):
dti.get_value(arr, key)

with tm.assert_produces_warning(FutureWarning):
result = dti.get_value(ser, key)
assert result == 7

with tm.assert_produces_warning(FutureWarning):
result = dti.get_value(ser, key.to_pydatetime())
assert result == 7

with tm.assert_produces_warning(FutureWarning):
result = dti.get_value(ser, key.to_datetime64())
assert result == 7


class TestGetSliceBounds:
@pytest.mark.parametrize("box", [date, datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
Expand Down
14 changes: 0 additions & 14 deletions pandas/tests/indexes/interval/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
IntervalIndex,
MultiIndex,
NaT,
Series,
Timedelta,
Timestamp,
array,
Expand Down Expand Up @@ -582,19 +581,6 @@ def test_putmask_td64(self):
tm.assert_index_equal(result, expected)


class TestGetValue:
@pytest.mark.parametrize("key", [[5], (2, 3)])
def test_get_value_non_scalar_errors(self, key):
# GH#31117
idx = IntervalIndex.from_tuples([(1, 3), (2, 4), (3, 5), (7, 10), (3, 10)])
ser = Series(range(len(idx)), index=idx)

msg = str(key)
with pytest.raises(InvalidIndexError, match=msg):
with tm.assert_produces_warning(FutureWarning):
idx.get_value(ser, key)


class TestContains:
# .__contains__, not .contains

Expand Down
7 changes: 0 additions & 7 deletions pandas/tests/indexes/numeric/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,6 @@ def test_lookups_datetimelike_values(self, vals, dtype):

expected = vals[1]

with tm.assert_produces_warning(FutureWarning):
result = ser.index.get_value(ser, 4.0)
assert isinstance(result, type(expected)) and result == expected
with tm.assert_produces_warning(FutureWarning):
result = ser.index.get_value(ser, 4)
assert isinstance(result, type(expected)) and result == expected

result = ser[4.0]
assert isinstance(result, type(expected)) and result == expected
result = ser[4]
Expand Down
59 changes: 2 additions & 57 deletions pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,85 +774,34 @@ def test_take_fill_value(self):


class TestGetValue:
def test_get_value(self):
# GH 17717
p0 = Period("2017-09-01")
p1 = Period("2017-09-02")
p2 = Period("2017-09-03")

idx0 = PeriodIndex([p0, p1, p2])
input0 = Series(np.array([1, 2, 3]), index=idx0)
expected0 = 2

with tm.assert_produces_warning(FutureWarning):
result0 = idx0.get_value(input0, p1)
assert result0 == expected0

idx1 = PeriodIndex([p1, p1, p2])
input1 = Series(np.array([1, 2, 3]), index=idx1)
expected1 = input1.iloc[[0, 1]]

with tm.assert_produces_warning(FutureWarning):
result1 = idx1.get_value(input1, p1)
tm.assert_series_equal(result1, expected1)

idx2 = PeriodIndex([p1, p2, p1])
input2 = Series(np.array([1, 2, 3]), index=idx2)
expected2 = input2.iloc[[0, 2]]

with tm.assert_produces_warning(FutureWarning):
result2 = idx2.get_value(input2, p1)
tm.assert_series_equal(result2, expected2)

@pytest.mark.parametrize("freq", ["H", "D"])
def test_get_value_datetime_hourly(self, freq):
# get_loc and get_value should treat datetime objects symmetrically
# TODO: this test used to test get_value, which is removed in 2.0.
# should this test be moved somewhere, or is what's left redundant?
dti = date_range("2016-01-01", periods=3, freq="MS")
pi = dti.to_period(freq)
ser = Series(range(7, 10), index=pi)

ts = dti[0]

assert pi.get_loc(ts) == 0
with tm.assert_produces_warning(FutureWarning):
assert pi.get_value(ser, ts) == 7
assert ser[ts] == 7
assert ser.loc[ts] == 7

ts2 = ts + Timedelta(hours=3)
if freq == "H":
with pytest.raises(KeyError, match="2016-01-01 03:00"):
pi.get_loc(ts2)
with pytest.raises(KeyError, match="2016-01-01 03:00"):
with tm.assert_produces_warning(FutureWarning):
pi.get_value(ser, ts2)
with pytest.raises(KeyError, match="2016-01-01 03:00"):
ser[ts2]
with pytest.raises(KeyError, match="2016-01-01 03:00"):
ser.loc[ts2]
else:
assert pi.get_loc(ts2) == 0
with tm.assert_produces_warning(FutureWarning):
assert pi.get_value(ser, ts2) == 7
assert ser[ts2] == 7
assert ser.loc[ts2] == 7

def test_get_value_integer(self):
msg = "index 16801 is out of bounds for axis 0 with size 3"
dti = date_range("2016-01-01", periods=3)
pi = dti.to_period("D")
ser = Series(range(3), index=pi)
with pytest.raises(IndexError, match=msg):
with tm.assert_produces_warning(FutureWarning):
pi.get_value(ser, 16801)

msg = "index 46 is out of bounds for axis 0 with size 3"
pi2 = dti.to_period("Y") # duplicates, ordinals are all 46
ser2 = Series(range(3), index=pi2)
with pytest.raises(IndexError, match=msg):
with tm.assert_produces_warning(FutureWarning):
pi2.get_value(ser2, 46)


class TestContains:
def test_contains(self):
Expand All @@ -864,7 +813,6 @@ def test_contains(self):

ps0 = [p0, p1, p2]
idx0 = PeriodIndex(ps0)
ser = Series(range(6, 9), index=idx0)

for p in ps0:
assert p in idx0
Expand All @@ -876,9 +824,6 @@ def test_contains(self):
assert key not in idx0
with pytest.raises(KeyError, match=key):
idx0.get_loc(key)
with pytest.raises(KeyError, match=key):
with tm.assert_produces_warning(FutureWarning):
idx0.get_value(ser, key)

assert "2017-09" in idx0

Expand Down
10 changes: 0 additions & 10 deletions pandas/tests/indexes/period/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,6 @@ def test_partial_slice_doesnt_require_monotonicity(self):
tm.assert_numpy_array_equal(result, indexer_2014)

expected = ser[indexer_2014]

with tm.assert_produces_warning(FutureWarning):
result = nidx.get_value(ser, "2014")
tm.assert_series_equal(result, expected)

result = ser.loc["2014"]
tm.assert_series_equal(result, expected)

Expand All @@ -193,11 +188,6 @@ def test_partial_slice_doesnt_require_monotonicity(self):
tm.assert_numpy_array_equal(result, indexer_may2015)

expected = ser[indexer_may2015]

with tm.assert_produces_warning(FutureWarning):
result = nidx.get_value(ser, "May 2015")
tm.assert_series_equal(result, expected)

result = ser.loc["May 2015"]
tm.assert_series_equal(result, expected)

Expand Down
20 changes: 0 additions & 20 deletions pandas/tests/indexes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
NaT,
PeriodIndex,
RangeIndex,
Series,
TimedeltaIndex,
)
import pandas._testing as tm
Expand Down Expand Up @@ -176,25 +175,6 @@ def test_contains_requires_hashable_raises(self, index):
{} in index._engine


class TestGetValue:
@pytest.mark.parametrize(
"index", ["string", "int", "datetime", "timedelta"], indirect=True
)
def test_get_value(self, index):
# TODO(2.0): can remove once get_value deprecation is enforced GH#19728
values = np.random.randn(100)
value = index[67]

with pytest.raises(AttributeError, match="has no attribute '_values'"):
# Index.get_value requires a Series, not an ndarray
with tm.assert_produces_warning(FutureWarning):
index.get_value(values, value)

with tm.assert_produces_warning(FutureWarning):
result = index.get_value(Series(values, index=values), value)
tm.assert_almost_equal(result, values[67])


class TestGetLoc:
def test_get_loc_non_hashable(self, index):
# MultiIndex and Index raise TypeError, others InvalidIndexError
Expand Down
3 changes: 0 additions & 3 deletions pandas/tests/indexing/multiindex/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ def test_getitem_intkey_leading_level(

with pytest.raises(KeyError, match="14"):
ser[14]
with pytest.raises(KeyError, match="14"):
with tm.assert_produces_warning(FutureWarning):
mi.get_value(ser, 14)

# ---------------------------------------------------------------------

Expand Down
5 changes: 0 additions & 5 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,6 @@ def test_getitem_categorical_str():
expected = ser.iloc[[0, 3]]
tm.assert_series_equal(result, expected)

# Check the intermediate steps work as expected
with tm.assert_produces_warning(FutureWarning):
result = ser.index.get_value(ser, "a")
tm.assert_series_equal(result, expected)


def test_slice_can_reorder_not_uniquely_indexed():
ser = Series(1, index=["a", "a", "b", "b", "c"])
Expand Down