Skip to content

BUG #19860 Corrected use of mixed indexes with .at #22436

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 6 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3115,8 +3115,8 @@ def get_value(self, series, key):
iloc = self.get_loc(key)
return s[iloc]
except KeyError:
if (len(self) > 0 and
self.inferred_type in ['integer', 'boolean']):
if (len(self) > 0
and (self.holds_integer() or self.is_boolean())):
raise
elif is_integer(key):
return s[key]
Expand All @@ -3129,7 +3129,7 @@ def get_value(self, series, key):
return self._engine.get_value(s, k,
tz=getattr(series.dtype, 'tz', None))
except KeyError as e1:
if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:
if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
raise

try:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,7 @@ def _convert_key(self, key, is_setter=False):
raise ValueError("At based indexing on an integer index "
"can only have integer indexers")
else:
if is_integer(i):
if is_integer(i) and not ax.holds_integer():
raise ValueError("At based indexing on an non-integer "
"index can only have non-integer "
"indexers")
Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,20 @@ def test_index_type_coercion(self):
idxr(s2)['0'] = 0
assert s2.index.is_object()

@pytest.mark.parametrize("index,val", [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment with the issue number

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests should be in pandas/tests/indexes/test_base (look for other contains tests)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_index_not_contains and test_index_contains are in the same file a few lines above, should I move these up or create pandas/tests/indexes/test_base and move them there?

(Index([0, 1, '2']), '1'),
(Index([0, 1, '2']), 2),
])
def test_mixed_index_not_contains(self, index, val):
assert val not in index

@pytest.mark.parametrize("index,val", [
(Index([0, 1, '2']), 0),
(Index([0, 1, '2']), '2'),
])
def test_mixed_index_contains(self, index, val):
assert val in index


class TestMisc(Base):

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

def test_mixed_index_at_iat(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move these to pandas/tests/indexing/test_scalar.py (see where other ones are)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a gh comment with the issue number

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the test from the OP (was on dataframe)

s = Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 1, 2])
for el, item in s.iteritems():
assert s.at[el] == item
for i in range(len(s)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you check that we have similar tests for .loc (I think so), if not can you add a loc version (inside same test is ok). maybe even just add the .loc tests and move from elsewhere.

you can parameterize these over .at / .loc

assert s.iat[i] == i + 1

def test_mixed_index_assignment(self):
s = Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 1, 2])
s.at['a'] = 11
assert s.iat[0] == 11
s.at[1] = 22
assert s.iat[3] == 22

def test_mixed_index_no_fallback(self):
s = Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 1, 2])
with pytest.raises(KeyError):
s.at[0]
with pytest.raises(KeyError):
s.at[4]

def test_rhs_alignment(self):
# GH8258, tests that both rows & columns are aligned to what is
# assigned to. covers both uniform data-type & multi-type cases
Expand Down