Skip to content

REF: Remove CategoricalIndex.get_value #31765

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 12 commits into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
REF: CategoricalIndex.get_value no longer needed
  • Loading branch information
jbrockmendel committed Feb 5, 2020
commit 5d643628fa2fb83f99afb0a9e742acb06bd83d98
29 changes: 0 additions & 29 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,35 +478,6 @@ def get_loc(self, key, method=None):
except KeyError:
raise KeyError(key)

def get_value(self, series: "Series", key: Any):
"""
Fast lookup of value from 1-dimensional ndarray. Only use this if you
know what you're doing

Parameters
----------
series : Series
1-dimensional array to take values from
key: : scalar
The value of this index at the position of the desired value,
otherwise the positional index of the desired value

Returns
-------
Any
The element of the series at the position indicated by the key
"""
k = key
try:
k = self._convert_scalar_indexer(k, kind="getitem")
indexer = self.get_loc(k)
return series.take([indexer])[0]
except (KeyError, TypeError):
pass

# we might be a positional inexer
return Index.get_value(self, series, key)

@Appender(Index.where.__doc__)
def where(self, cond, other=None):
# TODO: Investigate an alternative implementation with
Expand Down
15 changes: 2 additions & 13 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2101,19 +2101,8 @@ def _convert_key(self, key, is_setter: bool = False):
return list(key)

for ax, i in zip(self.obj.axes, key):
if ax.is_integer():
if not is_integer(i):
raise ValueError(
"At based indexing on an integer index "
"can only have integer indexers"
)
else:
if is_integer(i) and not (ax.holds_integer() or ax.is_floating()):
raise ValueError(
"At based indexing on an non-integer "
"index can only have non-integer "
"indexers"
)
ax._convert_scalar_indexer(i, "loc")

return key


Expand Down
3 changes: 3 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,9 @@ def _get_value(self, label, takeable: bool = False):
"""
if takeable:
return self._values[label]

# We assume that _convert_scalar_indexer has already been called,
# with kind="loc", if necessary, by the time we get here
return self.index.get_value(self, label)

def __setitem__(self, key, value):
Expand Down
16 changes: 10 additions & 6 deletions pandas/tests/indexing/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,33 @@ def test_at_to_fail(self):
result = s.at["a"]
assert result == 1
msg = (
"At based indexing on an non-integer index can only have "
"non-integer indexers"
"cannot do label indexing on <class 'pandas.core.indexes.base.Index'> "
r"with these indexers \[0\] of <class 'int'>"
)
with pytest.raises(ValueError, match=msg):
with pytest.raises(TypeError, match=msg):
s.at[0]
with pytest.raises(TypeError, match=msg):
s.loc[0]

df = DataFrame({"A": [1, 2, 3]}, index=list("abc"))
result = df.at["a", "A"]
assert result == 1
with pytest.raises(ValueError, match=msg):
with pytest.raises(TypeError, match=msg):
df.at["a", 0]
with pytest.raises(TypeError, match=msg):
df.loc["a", 0]

s = Series([1, 2, 3], index=[3, 2, 1])
result = s.at[1]
assert result == 3
msg = "At based indexing on an integer index can only have integer indexers"
with pytest.raises(ValueError, match=msg):
with pytest.raises(KeyError, match="a"):
s.at["a"]

df = DataFrame({0: [1, 2, 3]}, index=[3, 2, 1])
result = df.at[1, 0]
assert result == 3
with pytest.raises(ValueError, match=msg):
with pytest.raises(KeyError, match="a"):
df.at["a", 0]

# GH 13822, incorrect error string with non-unique columns when missing
Expand Down