Skip to content

Commit

Permalink
Fix bug where use of .ix[tuple(...)]=x fails to correctly check out o…
Browse files Browse the repository at this point in the history
…f bounds index for columns. This is a regression as it used to work in pandas 0.12.0. The code incorrectly checks the column index against the len() of the dataframe to see if it is in bounds not against the number of columns. Have added 2 tests with non-square dataframes to test that the fix works.
  • Loading branch information
Andrew Burrows authored and burrowsa committed Jan 9, 2014
1 parent 045e93a commit 5a69710
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,8 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):
return {'key': obj}

# a positional
if obj >= len(self.obj) and not isinstance(labels, MultiIndex):
if (obj >= self.obj.shape[axis] and
not isinstance(labels, MultiIndex)):
raise ValueError("cannot set by positional indexing with "
"enlargement")

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2352,6 +2352,14 @@ def check_slicing_positional(index):
#self.assertRaises(TypeError, lambda : s.iloc[2.0:5.0])
#self.assertRaises(TypeError, lambda : s.iloc[2:5.0])

def test_set_ix_out_of_bounds_axis_0(self):
df = pd.DataFrame(randn(2, 5), index=["row%s" % i for i in range(2)], columns=["col%s" % i for i in range(5)])
self.assertRaises(ValueError, df.ix.__setitem__, (2, 0), 100)

def test_set_ix_out_of_bounds_axis_1(self):
df = pd.DataFrame(randn(5, 2), index=["row%s" % i for i in range(5)], columns=["col%s" % i for i in range(2)])
self.assertRaises(ValueError, df.ix.__setitem__, (0 , 2), 100)


if __name__ == '__main__':
import nose
Expand Down

0 comments on commit 5a69710

Please sign in to comment.