Skip to content

Commit

Permalink
BUG: Series.get() with ExtensionArray and integer index (#21260)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Irv authored and jreback committed Jun 29, 2018
1 parent a3e56f2 commit 5c761f1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ Reshaping
ExtensionArray
^^^^^^^^^^^^^^

- Bug in :meth:`Series.get` for ``Series`` using ``ExtensionArray`` and integer index (:issue:`21257`)
- :meth:`Series.combine()` works correctly with :class:`~pandas.api.extensions.ExtensionArray` inside of :class:`Series` (:issue:`20825`)
- :meth:`Series.combine()` with scalar argument now works for any function type (:issue:`21248`)
-
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2988,16 +2988,20 @@ 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
# GH 20882, 21257
# Unify Index and ExtensionArray treatment
# First try to convert the key to a location
# If that fails, see if key is an integer, and
# If that fails, raise a KeyError if an integer
# index, otherwise, see if key is an integer, and
# try that
try:
iloc = self.get_loc(key)
return s[iloc]
except KeyError:
if is_integer(key):
if (len(self) > 0 and
self.inferred_type in ['integer', 'boolean']):
raise
elif is_integer(key):
return s[key]

s = com._values_from_object(series)
Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/extension/base/getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_get(self, data):
expected = s.iloc[[0, 1]]
self.assert_series_equal(result, expected)

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

s = pd.Series(data[:6], index=list('abcdef'))
Expand All @@ -147,6 +147,11 @@ def test_get(self, data):
assert s.get(-1) == s.iloc[-1]
assert s.get(len(s)) is None

# GH 21257
s = pd.Series(data)
s2 = s[::2]
assert s2.get(1) is None

def test_take_sequence(self, data):
result = pd.Series(data)[[0, 1, 3]]
assert result.iloc[0] == data[0]
Expand Down
43 changes: 43 additions & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,49 @@ def test_getitem_box_float64(test_data):
assert isinstance(value, np.float64)


@pytest.mark.parametrize(
'arr',
[
np.random.randn(10),
tm.makeDateIndex(10, name='a').tz_localize(
tz='US/Eastern'),
])
def test_get(arr):
# GH 21260
s = Series(arr, index=[2 * i for i in range(len(arr))])
assert s.get(4) == s.iloc[2]

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

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

assert s.get(-1) is None
assert s.get(s.index.max() + 1) is None

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

result = s.get(slice('b', 'd'))
expected = s.iloc[[1, 2, 3]]
tm.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

# GH 21257
s = pd.Series(arr)
s2 = s[::2]
assert s2.get(1) is None


def test_series_box_timestamp():
rng = pd.date_range('20090415', '20090519', freq='B')
ser = Series(rng)
Expand Down

0 comments on commit 5c761f1

Please sign in to comment.