Skip to content

BUG: Bug in setitem with list-of-lists and single vs mixed types (GH7551) #7552

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
Jun 23, 2014
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
2 changes: 1 addition & 1 deletion doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Bug Fixes
- Bug in ``DataFrame.where`` with a symmetric shaped frame and a passed other of a DataFrame (:issue:`7506`)
- Bug in Panel indexing with a multi-index axis (:issue:`7516`)
- Regression in datetimelike slice indexing with a duplicated index and non-exact end-points (:issue:`7523`)

- Bug in setitem with list-of-lists and single vs mixed types (:issue:`7551`:)
- Bug in timeops with non-aligned Series (:issue:`7500`)


Expand Down
12 changes: 9 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,20 @@ def can_do_equal_len():
else:
setter(item, np.nan)

# we have an equal len ndarray to our labels
elif isinstance(value, np.ndarray) and value.ndim == 2:
# we have an equal len ndarray/convertible to our labels
elif np.array(value).ndim == 2:

# note that this coerces the dtype if we are mixed
# GH 7551
value = np.array(value,dtype=object)
if len(labels) != value.shape[1]:
raise ValueError('Must have equal len keys and value '
'when setting with an ndarray')

for i, item in enumerate(labels):
setter(item, value[:, i])

# setting with a list, recoerces
setter(item, value[:, i].tolist())

# we have an equal len list/ndarray
elif can_do_equal_len():
Expand Down
15 changes: 14 additions & 1 deletion pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,6 @@ def test_iloc_setitem_series(self):
result = df.iloc[:,2:3]
assert_frame_equal(result, expected)

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

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

def test_iloc_setitem_list_of_lists(self):

# GH 7551
# list-of-list is set incorrectly in mixed vs. single dtyped frames
df = DataFrame(dict(A = np.arange(5,dtype='int64'), B = np.arange(5,10,dtype='int64')))
df.iloc[2:4] = [[10,11],[12,13]]
expected = DataFrame(dict(A = [0,1,10,12,4], B = [5,6,11,13,9]))
assert_frame_equal(df, expected)

df = DataFrame(dict(A = list('abcde'), B = np.arange(5,10,dtype='int64')))
df.iloc[2:4] = [['x',11],['y',13]]
expected = DataFrame(dict(A = ['a','b','x','y','e'], B = [5,6,11,13,9]))
assert_frame_equal(df, expected)

def test_iloc_getitem_multiindex(self):
mi_labels = DataFrame(np.random.randn(4, 3), columns=[['i', 'i', 'j'],
['A', 'A', 'B']],
Expand Down