Skip to content

Commit

Permalink
DEPR: get_value (pandas-dev#33907)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed May 1, 2020
1 parent 6c1901c commit 839d8e8
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 21 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ Deprecations
arguments (:issue:`27573`).

- :func:`pandas.api.types.is_categorical` is deprecated and will be removed in a future version; use `:func:pandas.api.types.is_categorical_dtype` instead (:issue:`33385`)
- :meth:`Index.get_value` is deprecated and will be removed in a future version (:issue:`19728`)

.. ---------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4540,6 +4540,13 @@ def get_value(self, series: "Series", key):
-------
scalar or Series
"""
warnings.warn(
"get_value is deprecated and will be removed in a future version. "
"Use Series[key] instead",
FutureWarning,
stacklevel=2,
)

self._check_indexing_error(key)

try:
Expand Down
12 changes: 8 additions & 4 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,13 +608,17 @@ def test_get_value(self):
key = dti[1]

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

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

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

result = dti.get_value(ser, key.to_datetime64())
with tm.assert_produces_warning(FutureWarning):
result = dti.get_value(ser, key.to_datetime64())
assert result == 7
27 changes: 18 additions & 9 deletions pandas/tests/indexes/period/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,21 +673,24 @@ def test_get_value(self):
input0 = pd.Series(np.array([1, 2, 3]), index=idx0)
expected0 = 2

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

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

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

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

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

@pytest.mark.parametrize("freq", ["H", "D"])
Expand All @@ -700,7 +703,8 @@ def test_get_value_datetime_hourly(self, freq):
ts = dti[0]

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

Expand All @@ -709,14 +713,16 @@ def test_get_value_datetime_hourly(self, freq):
with pytest.raises(KeyError, match="2016-01-01 03:00"):
pi.get_loc(ts2)
with pytest.raises(KeyError, match="2016-01-01 03:00"):
pi.get_value(ser, ts2)
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
assert pi.get_value(ser, ts2) == 7
with tm.assert_produces_warning(FutureWarning):
assert pi.get_value(ser, ts2) == 7
assert ser[ts2] == 7
assert ser.loc[ts2] == 7

Expand All @@ -726,13 +732,15 @@ def test_get_value_integer(self):
pi = dti.to_period("D")
ser = pd.Series(range(3), index=pi)
with pytest.raises(IndexError, match=msg):
pi.get_value(ser, 16801)
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 = pd.Series(range(3), index=pi2)
with pytest.raises(IndexError, match=msg):
pi2.get_value(ser2, 46)
with tm.assert_produces_warning(FutureWarning):
pi2.get_value(ser2, 46)


class TestContains:
Expand All @@ -758,7 +766,8 @@ def test_contains(self):
with pytest.raises(KeyError, match=key):
idx0.get_loc(key)
with pytest.raises(KeyError, match=key):
idx0.get_value(ser, key)
with tm.assert_produces_warning(FutureWarning):
idx0.get_value(ser, key)

assert "2017-09" in idx0

Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/period/test_partial_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def test_partial_slice_doesnt_require_monotonicity(self):

expected = ser[indexer_2014]

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

result = ser.loc["2014"]
Expand All @@ -131,7 +132,8 @@ def test_partial_slice_doesnt_require_monotonicity(self):

expected = ser[indexer_may2015]

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

result = ser.loc["May 2015"]
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,9 +1697,11 @@ def test_get_value(self, indices):

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

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

@pytest.mark.parametrize("values", [["foo", "bar", "quux"], {"foo", "bar", "quux"}])
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,11 @@ def test_lookups_datetimelike_values(self, vals):

expected = vals[1]

result = ser.index.get_value(ser, 4.0)
with tm.assert_produces_warning(FutureWarning):
result = ser.index.get_value(ser, 4.0)
assert isinstance(result, type(expected)) and result == expected
result = ser.index.get_value(ser, 4)
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]
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexing/multiindex/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ def test_getitem_intkey_leading_level(
with pytest.raises(KeyError, match="14"):
ser[14]
with pytest.raises(KeyError, match="14"):
mi.get_value(ser, 14)
with tm.assert_produces_warning(FutureWarning):
mi.get_value(ser, 14)

# ---------------------------------------------------------------------
# AMBIGUOUS CASES!
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ def test_getitem_categorical_str():
tm.assert_series_equal(result, expected)

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


Expand Down

0 comments on commit 839d8e8

Please sign in to comment.