From 40cdc3a498d0142aa8a7f2d6e8607612a3f10722 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 20 Oct 2022 08:12:29 -0700 Subject: [PATCH] DEPR: remove Index.get_value --- doc/redirects.csv | 1 - doc/source/reference/indexing.rst | 1 - doc/source/whatsnew/v2.0.0.rst | 1 + pandas/core/indexes/base.py | 39 ------------ .../tests/indexes/datetimes/test_indexing.py | 27 --------- .../tests/indexes/interval/test_indexing.py | 14 ----- pandas/tests/indexes/numeric/test_numeric.py | 7 --- pandas/tests/indexes/period/test_indexing.py | 59 +------------------ .../indexes/period/test_partial_slicing.py | 10 ---- pandas/tests/indexes/test_indexing.py | 20 ------- .../tests/indexing/multiindex/test_partial.py | 3 - pandas/tests/series/indexing/test_getitem.py | 5 -- 12 files changed, 3 insertions(+), 184 deletions(-) diff --git a/doc/redirects.csv b/doc/redirects.csv index fda09d7644a49..6a79ed5958089 100644 --- a/doc/redirects.csv +++ b/doc/redirects.csv @@ -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 diff --git a/doc/source/reference/indexing.rst b/doc/source/reference/indexing.rst index ddfef14036ef3..533f4ee19f83b 100644 --- a/doc/source/reference/indexing.rst +++ b/doc/source/reference/indexing.rst @@ -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 diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index e57ba92267855..e7ef01d63190c 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -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`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index cde221c77fe9b..1608cc9ed53e8 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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 diff --git a/pandas/tests/indexes/datetimes/test_indexing.py b/pandas/tests/indexes/datetimes/test_indexing.py index 62fdff528bd84..5c8724a2a1c22 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -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]) diff --git a/pandas/tests/indexes/interval/test_indexing.py b/pandas/tests/indexes/interval/test_indexing.py index 4653981a1285d..c647226283b8d 100644 --- a/pandas/tests/indexes/interval/test_indexing.py +++ b/pandas/tests/indexes/interval/test_indexing.py @@ -14,7 +14,6 @@ IntervalIndex, MultiIndex, NaT, - Series, Timedelta, Timestamp, array, @@ -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 diff --git a/pandas/tests/indexes/numeric/test_numeric.py b/pandas/tests/indexes/numeric/test_numeric.py index 23262cb2eb768..dd62ad8b31fae 100644 --- a/pandas/tests/indexes/numeric/test_numeric.py +++ b/pandas/tests/indexes/numeric/test_numeric.py @@ -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] diff --git a/pandas/tests/indexes/period/test_indexing.py b/pandas/tests/indexes/period/test_indexing.py index df40822337ed0..fcc7fa083691e 100644 --- a/pandas/tests/indexes/period/test_indexing.py +++ b/pandas/tests/indexes/period/test_indexing.py @@ -774,39 +774,11 @@ 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) @@ -814,8 +786,6 @@ def test_get_value_datetime_hourly(self, freq): 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 @@ -823,36 +793,15 @@ def test_get_value_datetime_hourly(self, freq): 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): @@ -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 @@ -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 diff --git a/pandas/tests/indexes/period/test_partial_slicing.py b/pandas/tests/indexes/period/test_partial_slicing.py index 2cf1cf0f15652..1f704cfa064d8 100644 --- a/pandas/tests/indexes/period/test_partial_slicing.py +++ b/pandas/tests/indexes/period/test_partial_slicing.py @@ -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) @@ -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) diff --git a/pandas/tests/indexes/test_indexing.py b/pandas/tests/indexes/test_indexing.py index ab934df992d61..a0cb6691cb6a0 100644 --- a/pandas/tests/indexes/test_indexing.py +++ b/pandas/tests/indexes/test_indexing.py @@ -28,7 +28,6 @@ NaT, PeriodIndex, RangeIndex, - Series, TimedeltaIndex, ) import pandas._testing as tm @@ -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 diff --git a/pandas/tests/indexing/multiindex/test_partial.py b/pandas/tests/indexing/multiindex/test_partial.py index cface630c6647..0cc1e116aa2de 100644 --- a/pandas/tests/indexing/multiindex/test_partial.py +++ b/pandas/tests/indexing/multiindex/test_partial.py @@ -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) # --------------------------------------------------------------------- diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index cc67dd9caeea9..993c056045ae0 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -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"])