Skip to content

Commit f47ba8e

Browse files
committed
BUG pandas-dev#19860 Corrected use of mixed indexes with .at
.at incorrectly disallowed the use of integer indexes when a mixed index was used disabled fallback indexing when a mixed index is used
1 parent 4f11d1a commit f47ba8e

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

pandas/core/indexes/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3115,8 +3115,8 @@ def get_value(self, series, key):
31153115
iloc = self.get_loc(key)
31163116
return s[iloc]
31173117
except KeyError:
3118-
if (len(self) > 0 and
3119-
self.inferred_type in ['integer', 'boolean']):
3118+
if (len(self) > 0
3119+
and (self.holds_integer() or self.is_boolean())):
31203120
raise
31213121
elif is_integer(key):
31223122
return s[key]
@@ -3129,7 +3129,7 @@ def get_value(self, series, key):
31293129
return self._engine.get_value(s, k,
31303130
tz=getattr(series.dtype, 'tz', None))
31313131
except KeyError as e1:
3132-
if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:
3132+
if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
31333133
raise
31343134

31353135
try:

pandas/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,7 @@ def _convert_key(self, key, is_setter=False):
23542354
raise ValueError("At based indexing on an integer index "
23552355
"can only have integer indexers")
23562356
else:
2357-
if is_integer(i):
2357+
if is_integer(i) and not ax.holds_integer():
23582358
raise ValueError("At based indexing on an non-integer "
23592359
"index can only have non-integer "
23602360
"indexers")

pandas/tests/indexing/test_indexing.py

+35
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,20 @@ def test_index_type_coercion(self):
666666
idxr(s2)['0'] = 0
667667
assert s2.index.is_object()
668668

669+
@pytest.mark.parametrize("index,val", [
670+
(Index([0, 1, '2']), '1'),
671+
(Index([0, 1, '2']), 2),
672+
])
673+
def test_mixed_index_not_contains(self, index, val):
674+
assert val not in index
675+
676+
@pytest.mark.parametrize("index,val", [
677+
(Index([0, 1, '2']), 0),
678+
(Index([0, 1, '2']), '2'),
679+
])
680+
def test_mixed_index_contains(self, index, val):
681+
assert val in index
682+
669683

670684
class TestMisc(Base):
671685

@@ -710,6 +724,27 @@ def test_float_index_at_iat(self):
710724
for i in range(len(s)):
711725
assert s.iat[i] == i + 1
712726

727+
def test_mixed_index_at_iat(self):
728+
s = Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 1, 2])
729+
for el, item in s.iteritems():
730+
assert s.at[el] == item
731+
for i in range(len(s)):
732+
assert s.iat[i] == i + 1
733+
734+
def test_mixed_index_assignment(self):
735+
s = Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 1, 2])
736+
s.at['a'] = 11
737+
assert s.iat[0] == 11
738+
s.at[1] = 22
739+
assert s.iat[3] == 22
740+
741+
def test_mixed_index_no_fallback(self):
742+
s = Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 1, 2])
743+
with pytest.raises(KeyError):
744+
s.at[0]
745+
with pytest.raises(KeyError):
746+
s.at[4]
747+
713748
def test_rhs_alignment(self):
714749
# GH8258, tests that both rows & columns are aligned to what is
715750
# assigned to. covers both uniform data-type & multi-type cases

0 commit comments

Comments
 (0)