Skip to content
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

BUG: Series.get() with ExtensionArray and integer index #21260

Merged
merged 6 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -270,6 +270,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