Skip to content

BUG: Series.__setitem__[listlike_of_integers] with IntervalIndex #33473

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 18 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
725f762
BUG: Series.__getitem__ with MultiIndex and leading integer level
jbrockmendel Apr 8, 2020
3c2f9e7
dedicated tests
jbrockmendel Apr 8, 2020
a75626d
whatnsew
jbrockmendel Apr 8, 2020
fa30344
Merge branch 'master' of https://github.com/pandas-dev/pandas into mi…
jbrockmendel Apr 8, 2020
f40886e
test
jbrockmendel Apr 8, 2020
ad4f16c
REF: collect DataFrame.__setitem__ tests (#33408)
jbrockmendel Apr 8, 2020
becdcbf
use https link for Anaconda (#33413)
partev Apr 9, 2020
7bf22eb
unpinned 37 (#33423)
WillAyd Apr 9, 2020
5fcc787
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel Apr 9, 2020
0d5a0a7
revert
jbrockmendel Apr 10, 2020
aa6f3f8
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel Apr 10, 2020
2718bcc
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel Apr 10, 2020
af0cdb8
BUG: Series.__setitem__[listlike_of_integers] with IntervalIndex]
jbrockmendel Apr 11, 2020
2bb09e8
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel Apr 12, 2020
9b14800
whatsnew
jbrockmendel Apr 12, 2020
2a0cbbd
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel Apr 13, 2020
ffb0783
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel Apr 14, 2020
58f4621
Merge branch 'master' of https://github.com/pandas-dev/pandas into fa…
jbrockmendel Apr 16, 2020
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/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ Indexing
- Bug in `Series.__getitem__` with an integer key and a :class:`MultiIndex` with leading integer level failing to raise ``KeyError`` if the key is not present in the first level (:issue:`33355`)
- Bug in :meth:`DataFrame.iloc` when slicing a single column-:class:`DataFrame`` with ``ExtensionDtype`` (e.g. ``df.iloc[:, :1]``) returning an invalid result (:issue:`32957`)
- Bug in :meth:`DatetimeIndex.insert` and :meth:`TimedeltaIndex.insert` causing index ``freq`` to be lost when setting an element into an empty :class:`Series` (:issue:33573`)
- Bug in :meth:`Series.__setitem__` with an :class:`IntervalIndex` and a list-like key of integers (:issue:`33473`)

Missing
^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4604,9 +4604,9 @@ def _check_indexing_error(self, key):

def _should_fallback_to_positional(self) -> bool:
"""
If an integer key is not found, should we fall back to positional indexing?
Should an integer key be treated as positional?
"""
if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
if self.holds_integer() or self.is_boolean():
return False
return True

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def is_overlapping(self) -> bool:
# GH 23309
return self._engine.is_overlapping

def _should_fallback_to_positional(self):
def _should_fallback_to_positional(self) -> bool:
# integer lookups in Series.__getitem__ are unambiguously
# positional in this case
return self.dtype.subtype.kind in ["m", "M"]
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2342,10 +2342,8 @@ def _check_indexing_error(self, key):

def _should_fallback_to_positional(self) -> bool:
"""
If an integer key is not found, should we fall back to positional indexing?
Should integer key(s) be treated as positional?
"""
if not self.nlevels:
return False
# GH#33355
return self.levels[0]._should_fallback_to_positional()

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def astype(self, dtype, copy=True):
# Indexing Methods

@doc(Index._should_fallback_to_positional)
def _should_fallback_to_positional(self):
def _should_fallback_to_positional(self) -> bool:
return False

@doc(Index._convert_slice_indexer)
Expand Down
7 changes: 2 additions & 5 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
from pandas.core.indexes.api import (
Float64Index,
Index,
IntervalIndex,
InvalidIndexError,
MultiIndex,
ensure_index,
Expand Down Expand Up @@ -945,9 +944,7 @@ def _get_with(self, key):
if key_type == "integer":
# We need to decide whether to treat this as a positional indexer
# (i.e. self.iloc) or label-based (i.e. self.loc)
if self.index.is_integer() or self.index.is_floating():
return self.loc[key]
elif isinstance(self.index, IntervalIndex):
if not self.index._should_fallback_to_positional():
return self.loc[key]
else:
return self.iloc[key]
Expand Down Expand Up @@ -1070,7 +1067,7 @@ def _set_with(self, key, value):
# Note: key_type == "boolean" should not occur because that
# should be caught by the is_bool_indexer check in __setitem__
if key_type == "integer":
if self.index.inferred_type == "integer":
if not self.index._should_fallback_to_positional():
self._set_labels(key, value)
else:
self._set_values(key, value)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/series/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ def test_getitem_intlist_intindex_periodvalues(self):
tm.assert_series_equal(result, exp)
assert result.dtype == "Period[D]"

@pytest.mark.parametrize("box", [list, np.array, pd.Index])
def test_getitem_intlist_intervalindex_non_int(self, box):
# GH#33404 fall back to positional since ints are unambiguous
dti = date_range("2000-01-03", periods=3)
ii = pd.IntervalIndex.from_breaks(dti)
ser = Series(range(len(ii)), index=ii)

expected = ser.iloc[:1]
key = box([0])
result = ser[key]
tm.assert_series_equal(result, expected)


def test_getitem_generator(string_series):
gen = (x > 0 for x in string_series)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ def test_getitem_out_of_bounds(datetime_series):
datetime_series[len(datetime_series)]

# GH #917
msg = r"index -\d+ is out of bounds for axis 0 with size \d+"
# With a RangeIndex, an int key gives a KeyError
s = Series([], dtype=object)
with pytest.raises(IndexError, match=msg):
with pytest.raises(KeyError, match="-1"):
s[-1]


Expand Down