Skip to content

Commit 5a69710

Browse files
Andrew Burrowsburrowsa
authored andcommitted
Fix bug where use of .ix[tuple(...)]=x fails to correctly check out of 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.
1 parent 045e93a commit 5a69710

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pandas/core/indexing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,8 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):
903903
return {'key': obj}
904904

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

pandas/tests/test_indexing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,6 +2352,14 @@ def check_slicing_positional(index):
23522352
#self.assertRaises(TypeError, lambda : s.iloc[2.0:5.0])
23532353
#self.assertRaises(TypeError, lambda : s.iloc[2:5.0])
23542354

2355+
def test_set_ix_out_of_bounds_axis_0(self):
2356+
df = pd.DataFrame(randn(2, 5), index=["row%s" % i for i in range(2)], columns=["col%s" % i for i in range(5)])
2357+
self.assertRaises(ValueError, df.ix.__setitem__, (2, 0), 100)
2358+
2359+
def test_set_ix_out_of_bounds_axis_1(self):
2360+
df = pd.DataFrame(randn(5, 2), index=["row%s" % i for i in range(5)], columns=["col%s" % i for i in range(2)])
2361+
self.assertRaises(ValueError, df.ix.__setitem__, (0 , 2), 100)
2362+
23552363

23562364
if __name__ == '__main__':
23572365
import nose

0 commit comments

Comments
 (0)