Skip to content

BUG: Bug in .loc performing fallback integer indexing with object dtype indices (GH7496) #7497

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 1 commit into from
Jun 19, 2014
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
2 changes: 2 additions & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ API changes
when comparing a ``Period`` with another object using ``==`` if the other
object isn't a ``Period`` ``False`` is returned.

- Bug in ``.loc`` performing fallback integer indexing with ``object`` dtype indices (:issue:`7496`)

.. _whatsnew_0141.prior_deprecations:

Prior Version Deprecations/Changes
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def _convert_list_indexer_for_mixed(self, keyarr, typ=None):
and we have a mixed index (e.g. number/labels). figure out
the indexer. return None if we can't help
"""
if com.is_integer_dtype(keyarr) and not self.is_floating():
if (typ is None or typ in ['iloc','ix']) and (com.is_integer_dtype(keyarr) and not self.is_floating()):
if self.inferred_type != 'integer':
keyarr = np.where(keyarr < 0,
len(self) + keyarr, keyarr)
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,38 @@ def test_loc_to_fail(self):
# raise a KeyError?
self.assertRaises(KeyError, df.loc.__getitem__, tuple([[1, 2], [1, 2]]))

# GH 7496
# loc should not fallback

s = Series()
s.loc[1] = 1
s.loc['a'] = 2

self.assertRaises(KeyError, lambda : s.loc[-1])

result = s.loc[[-1, -2]]
expected = Series(np.nan,index=[-1,-2])
assert_series_equal(result, expected)

result = s.loc[['4']]
expected = Series(np.nan,index=['4'])
assert_series_equal(result, expected)

s.loc[-1] = 3
result = s.loc[[-1,-2]]
expected = Series([3,np.nan],index=[-1,-2])
assert_series_equal(result, expected)

s['a'] = 2
result = s.loc[[-2]]
expected = Series([np.nan],index=[-2])
assert_series_equal(result, expected)

del s['a']
def f():
s.loc[[-2]] = 0
self.assertRaises(KeyError, f)

def test_loc_getitem_label_slice(self):

# label slices (with ints)
Expand Down