Skip to content

BUG: Bug in setting with ix/loc and a mixed int/string index (GH4544) #5064

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
Oct 1, 2013
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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ Bug Fixes
chunks of the same file. Now coerces to numerical type or raises warning. (:issue:`3866`)
- Fix a bug where reshaping a ``Series`` to its own shape raised ``TypeError`` (:issue:`4554`)
and other reshaping issues.
- Bug in setting with ``ix/loc`` and a mixed int/string index (:issue:`4544`)

pandas 0.12.0
-------------
Expand Down
23 changes: 17 additions & 6 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,25 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):
- No, prefer label-based indexing
"""
labels = self.obj._get_axis(axis)

# if we are a scalar indexer and not type correct raise
obj = self._convert_scalar_indexer(obj, axis)

# see if we are positional in nature
is_int_index = labels.is_integer()
is_int_positional = com.is_integer(obj) and not is_int_index

if com.is_integer(obj) and not is_int_index:
# if we are a label return me
try:
return labels.get_loc(obj)
except (KeyError, TypeError):
pass
except (ValueError):
if not is_int_positional:
raise

# a positional
if is_int_positional:

# if we are setting and its not a valid location
# its an insert which fails by definition
Expand All @@ -795,11 +811,6 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):

return obj

try:
return labels.get_loc(obj)
except (KeyError, TypeError):
pass

if isinstance(obj, slice):
return self._convert_slice_indexer(obj, axis)

Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,26 @@ def test_ix_assign_column_mixed(self):
df['b'].ix[[1,3]] = [100,-100]
assert_frame_equal(df,expected)

def test_ix_get_set_consistency(self):

# GH 4544
# ix/loc get/set not consistent when
# a mixed int/string index
df = DataFrame(np.arange(16).reshape((4, 4)),
columns=['a', 'b', 8, 'c'],
index=['e', 7, 'f', 'g'])

self.assert_(df.ix['e', 8] == 2)
self.assert_(df.loc['e', 8] == 2)

df.ix['e', 8] = 42
self.assert_(df.ix['e', 8] == 42)
self.assert_(df.loc['e', 8] == 42)

df.loc['e', 8] = 45
self.assert_(df.ix['e', 8] == 45)
self.assert_(df.loc['e', 8] == 45)

def test_iloc_mask(self):

# GH 3631, iloc with a mask (of a series) should raise
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ def test_setitem_float_labels(self):
tmp = s.copy()

s.ix[1] = 'zoo'
tmp.values[1] = 'zoo'
tmp.iloc[2] = 'zoo'

assert_series_equal(s, tmp)

Expand Down