Skip to content

Commit 1fa5b87

Browse files
committed
BUG: Bug in setting using fancy indexing a single element with a non-scalar (e.g. a list),
(GH6043)
1 parent eacc354 commit 1fa5b87

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

doc/source/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ Bug Fixes
135135
- Bug in Series construction of mixed type with datelike and an integer (which should result in
136136
object type and not automatic conversion) (:issue:`6028`)
137137
- Possible segfault when chained indexing with an object array under numpy 1.7.1 (:issue:`6016`)
138+
- Bug in setting using fancy indexing a single element with a non-scalar (e.g. a list),
139+
(:issue:`6043`)
138140

139141
pandas 0.13.0
140142
-------------

pandas/core/internals.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,10 +605,18 @@ def setitem(self, indexer, value):
605605
"different length than the value")
606606

607607
try:
608+
# setting a single element for each dim and with a rhs that could be say a list
609+
# GH 6043
610+
if arr_value.ndim == 1 and (
611+
np.isscalar(indexer) or (isinstance(indexer, tuple) and all([ np.isscalar(idx) for idx in indexer ]))):
612+
values[indexer] = value
613+
608614
# if we are an exact match (ex-broadcasting),
609615
# then use the resultant dtype
610-
if len(arr_value.shape) and arr_value.shape[0] == values.shape[0] and np.prod(arr_value.shape) == np.prod(values.shape):
616+
elif len(arr_value.shape) and arr_value.shape[0] == values.shape[0] and np.prod(arr_value.shape) == np.prod(values.shape):
611617
values = arr_value.reshape(values.shape)
618+
619+
# set
612620
else:
613621
values[indexer] = value
614622

pandas/tests/test_indexing.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,48 @@ def test_ix_get_set_consistency(self):
12861286
self.assert_(df.ix['e', 8] == 45)
12871287
self.assert_(df.loc['e', 8] == 45)
12881288

1289+
def test_setitem_list(self):
1290+
1291+
# GH 6043
1292+
# ix with a list
1293+
df = DataFrame(index=[0,1], columns=[0])
1294+
df.ix[1,0] = [1,2,3]
1295+
df.ix[1,0] = [1,2]
1296+
1297+
result = DataFrame(index=[0,1], columns=[0])
1298+
result.ix[1,0] = [1,2]
1299+
1300+
assert_frame_equal(result,df)
1301+
1302+
# ix with an object
1303+
class TO(object):
1304+
def __init__(self, value):
1305+
self.value = value
1306+
def __str__(self):
1307+
return "[{0}]".format(self.value)
1308+
__repr__ = __str__
1309+
def __eq__(self, other):
1310+
return self.value == other.value
1311+
def view(self):
1312+
return self
1313+
1314+
df = DataFrame(index=[0,1], columns=[0])
1315+
df.ix[1,0] = TO(1)
1316+
df.ix[1,0] = TO(2)
1317+
1318+
result = DataFrame(index=[0,1], columns=[0])
1319+
result.ix[1,0] = TO(2)
1320+
1321+
assert_frame_equal(result,df)
1322+
1323+
# remains object dtype even after setting it back
1324+
df = DataFrame(index=[0,1], columns=[0])
1325+
df.ix[1,0] = TO(1)
1326+
df.ix[1,0] = np.nan
1327+
result = DataFrame(index=[0,1], columns=[0])
1328+
1329+
assert_frame_equal(result, df)
1330+
12891331
def test_iloc_mask(self):
12901332

12911333
# GH 3631, iloc with a mask (of a series) should raise

0 commit comments

Comments
 (0)