Skip to content

Commit d6bada5

Browse files
committed
Merge pull request pandas-dev#7552 from jreback/iloc
BUG: Bug in setitem with list-of-lists and single vs mixed types (GH7551)
2 parents 9c22ba1 + dd4a329 commit d6bada5

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

doc/source/v0.14.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Bug Fixes
169169
- Bug in ``DataFrame.where`` with a symmetric shaped frame and a passed other of a DataFrame (:issue:`7506`)
170170
- Bug in Panel indexing with a multi-index axis (:issue:`7516`)
171171
- Regression in datetimelike slice indexing with a duplicated index and non-exact end-points (:issue:`7523`)
172-
172+
- Bug in setitem with list-of-lists and single vs mixed types (:issue:`7551`:)
173173
- Bug in timeops with non-aligned Series (:issue:`7500`)
174174

175175

pandas/core/indexing.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,20 @@ def can_do_equal_len():
419419
else:
420420
setter(item, np.nan)
421421

422-
# we have an equal len ndarray to our labels
423-
elif isinstance(value, np.ndarray) and value.ndim == 2:
422+
# we have an equal len ndarray/convertible to our labels
423+
elif np.array(value).ndim == 2:
424+
425+
# note that this coerces the dtype if we are mixed
426+
# GH 7551
427+
value = np.array(value,dtype=object)
424428
if len(labels) != value.shape[1]:
425429
raise ValueError('Must have equal len keys and value '
426430
'when setting with an ndarray')
427431

428432
for i, item in enumerate(labels):
429-
setter(item, value[:, i])
433+
434+
# setting with a list, recoerces
435+
setter(item, value[:, i].tolist())
430436

431437
# we have an equal len list/ndarray
432438
elif can_do_equal_len():

pandas/tests/test_indexing.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,6 @@ def test_iloc_setitem_series(self):
12721272
result = df.iloc[:,2:3]
12731273
assert_frame_equal(result, expected)
12741274

1275-
def test_iloc_setitem_series(self):
12761275
s = Series(np.random.randn(10), index=lrange(0,20,2))
12771276

12781277
s.iloc[1] = 1
@@ -1284,6 +1283,20 @@ def test_iloc_setitem_series(self):
12841283
result = s.iloc[:4]
12851284
assert_series_equal(result, expected)
12861285

1286+
def test_iloc_setitem_list_of_lists(self):
1287+
1288+
# GH 7551
1289+
# list-of-list is set incorrectly in mixed vs. single dtyped frames
1290+
df = DataFrame(dict(A = np.arange(5,dtype='int64'), B = np.arange(5,10,dtype='int64')))
1291+
df.iloc[2:4] = [[10,11],[12,13]]
1292+
expected = DataFrame(dict(A = [0,1,10,12,4], B = [5,6,11,13,9]))
1293+
assert_frame_equal(df, expected)
1294+
1295+
df = DataFrame(dict(A = list('abcde'), B = np.arange(5,10,dtype='int64')))
1296+
df.iloc[2:4] = [['x',11],['y',13]]
1297+
expected = DataFrame(dict(A = ['a','b','x','y','e'], B = [5,6,11,13,9]))
1298+
assert_frame_equal(df, expected)
1299+
12871300
def test_iloc_getitem_multiindex(self):
12881301
mi_labels = DataFrame(np.random.randn(4, 3), columns=[['i', 'i', 'j'],
12891302
['A', 'A', 'B']],

0 commit comments

Comments
 (0)