Skip to content

Commit

Permalink
BUG: Fix Series.get() for ExtensionArray and Categorical (pandas-dev#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Irv authored and jreback committed May 9, 2018
1 parent 21ee836 commit e978279
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3082,12 +3082,17 @@ def get_value(self, series, key):
# use this, e.g. DatetimeIndex
s = getattr(series, '_values', None)
if isinstance(s, (ExtensionArray, Index)) and is_scalar(key):
# GH 20825
# Unify Index and ExtensionArray treatment
# First try to convert the key to a location
# If that fails, see if key is an integer, and
# try that
try:
return s[key]
except (IndexError, ValueError):

# invalid type as an indexer
pass
iloc = self.get_loc(key)
return s[iloc]
except KeyError:
if is_integer(key):
return s[key]

s = com._values_from_object(series)
k = com._values_from_object(key)
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/extension/base/getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,36 @@ def test_getitem_slice(self, data):
result = data[slice(1)] # scalar
assert isinstance(result, type(data))

def test_get(self, data):
# GH 20882
s = pd.Series(data, index=[2 * i for i in range(len(data))])
assert s.get(4) == s.iloc[2]

result = s.get([4, 6])
expected = s.iloc[[2, 3]]
self.assert_series_equal(result, expected)

result = s.get(slice(2))
expected = s.iloc[[0, 1]]
self.assert_series_equal(result, expected)

assert s.get(-1) == s.iloc[-1]
assert s.get(s.index.max() + 1) is None

s = pd.Series(data[:6], index=list('abcdef'))
assert s.get('c') == s.iloc[2]

result = s.get(slice('b', 'd'))
expected = s.iloc[[1, 2, 3]]
self.assert_series_equal(result, expected)

result = s.get('Z')
assert result is None

assert s.get(4) == s.iloc[4]
assert s.get(-1) == s.iloc[-1]
assert s.get(len(s)) is None

def test_take_sequence(self, data):
result = pd.Series(data)[[0, 1, 3]]
assert result.iloc[0] == data[0]
Expand Down

0 comments on commit e978279

Please sign in to comment.