Skip to content

Commit b9c3856

Browse files
committed
BUG: Bug in .loc performing fallback integer indexing with object dtype indices (GH7496)
1 parent 324208e commit b9c3856

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

doc/source/v0.14.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ API changes
6262
when comparing a ``Period`` with another object using ``==`` if the other
6363
object isn't a ``Period`` ``False`` is returned.
6464

65+
- Bug in ``.loc`` performing fallback integer indexing with ``object`` dtype indices (:issue:`7496`)
66+
6567
.. _whatsnew_0141.prior_deprecations:
6668

6769
Prior Version Deprecations/Changes

pandas/core/index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def _convert_list_indexer_for_mixed(self, keyarr, typ=None):
609609
and we have a mixed index (e.g. number/labels). figure out
610610
the indexer. return None if we can't help
611611
"""
612-
if com.is_integer_dtype(keyarr) and not self.is_floating():
612+
if (typ is None or typ in ['iloc','ix']) and (com.is_integer_dtype(keyarr) and not self.is_floating()):
613613
if self.inferred_type != 'integer':
614614
keyarr = np.where(keyarr < 0,
615615
len(self) + keyarr, keyarr)

pandas/tests/test_indexing.py

+32
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,38 @@ def test_loc_to_fail(self):
806806
# raise a KeyError?
807807
self.assertRaises(KeyError, df.loc.__getitem__, tuple([[1, 2], [1, 2]]))
808808

809+
# GH 7496
810+
# loc should not fallback
811+
812+
s = Series()
813+
s.loc[1] = 1
814+
s.loc['a'] = 2
815+
816+
self.assertRaises(KeyError, lambda : s.loc[-1])
817+
818+
result = s.loc[[-1, -2]]
819+
expected = Series(np.nan,index=[-1,-2])
820+
assert_series_equal(result, expected)
821+
822+
result = s.loc[['4']]
823+
expected = Series(np.nan,index=['4'])
824+
assert_series_equal(result, expected)
825+
826+
s.loc[-1] = 3
827+
result = s.loc[[-1,-2]]
828+
expected = Series([3,np.nan],index=[-1,-2])
829+
assert_series_equal(result, expected)
830+
831+
s['a'] = 2
832+
result = s.loc[[-2]]
833+
expected = Series([np.nan],index=[-2])
834+
assert_series_equal(result, expected)
835+
836+
del s['a']
837+
def f():
838+
s.loc[[-2]] = 0
839+
self.assertRaises(KeyError, f)
840+
809841
def test_loc_getitem_label_slice(self):
810842

811843
# label slices (with ints)

0 commit comments

Comments
 (0)