Skip to content

Commit 61e8f5a

Browse files
committed
Merge pull request #8320 from jreback/fallback
BUG: Bug in .at that would accept integer indexers on a non-integer index and do fallback (GH7814)
2 parents 0714c02 + 2dce536 commit 61e8f5a

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Diff for: doc/source/v0.15.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ Bug Fixes
909909

910910
- Bug in accessing groups from a ``GroupBy`` when the original grouper
911911
was a tuple (:issue:`8121`).
912-
912+
- Bug in ``.at`` that would accept integer indexers on a non-integer index and do fallback (:issue:`7814`)
913913
- Bug with kde plot and NaNs (:issue:`8182`)
914914
- Bug in ``GroupBy.count`` with float32 data type were nan values were not excluded (:issue:`8169`).
915915
- Bug with stacked barplots and NaNs (:issue:`8175`).

Diff for: pandas/core/indexing.py

+12
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,18 @@ class _AtIndexer(_ScalarAccessIndexer):
15101510
""" label based scalar accessor """
15111511
_takeable = False
15121512

1513+
def _convert_key(self, key):
1514+
""" require they keys to be the same type as the index (so we don't fallback) """
1515+
for ax, i in zip(self.obj.axes, key):
1516+
if ax.is_integer():
1517+
if not com.is_integer(i):
1518+
raise ValueError("At based indexing on an integer index can only have integer "
1519+
"indexers")
1520+
else:
1521+
if com.is_integer(i):
1522+
raise ValueError("At based indexing on an non-integer index can only have non-integer "
1523+
"indexers")
1524+
return key
15131525

15141526
class _iAtIndexer(_ScalarAccessIndexer):
15151527

Diff for: pandas/tests/test_indexing.py

+22
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,28 @@ def f():
866866
df.loc[[3]]
867867
self.assertRaises(KeyError, f)
868868

869+
# at should not fallback
870+
# GH 7814
871+
s = Series([1,2,3], index=list('abc'))
872+
result = s.at['a']
873+
self.assertEquals(result, 1)
874+
self.assertRaises(ValueError, lambda : s.at[0])
875+
876+
df = DataFrame({'A' : [1,2,3]},index=list('abc'))
877+
result = df.at['a','A']
878+
self.assertEquals(result, 1)
879+
self.assertRaises(ValueError, lambda : df.at['a',0])
880+
881+
s = Series([1,2,3], index=[3,2,1])
882+
result = s.at[1]
883+
self.assertEquals(result, 3)
884+
self.assertRaises(ValueError, lambda : s.at['a'])
885+
886+
df = DataFrame({0 : [1,2,3]},index=[3,2,1])
887+
result = df.at[1,0]
888+
self.assertEquals(result, 3)
889+
self.assertRaises(ValueError, lambda : df.at['a',0])
890+
869891
def test_loc_getitem_label_slice(self):
870892

871893
# label slices (with ints)

0 commit comments

Comments
 (0)