Skip to content

BUG: Bug in setting using fancy indexing a single element with a non-scalar (e.g. a list) (GH6043) #6044

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
Jan 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: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ Bug Fixes
- Bug in Series construction of mixed type with datelike and an integer (which should result in
object type and not automatic conversion) (:issue:`6028`)
- Possible segfault when chained indexing with an object array under numpy 1.7.1 (:issue:`6016`)
- Bug in setting using fancy indexing a single element with a non-scalar (e.g. a list),
(:issue:`6043`)

pandas 0.13.0
-------------
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,18 @@ def setitem(self, indexer, value):
"different length than the value")

try:
# setting a single element for each dim and with a rhs that could be say a list
# GH 6043
if arr_value.ndim == 1 and (
np.isscalar(indexer) or (isinstance(indexer, tuple) and all([ np.isscalar(idx) for idx in indexer ]))):
values[indexer] = value

# if we are an exact match (ex-broadcasting),
# then use the resultant dtype
if len(arr_value.shape) and arr_value.shape[0] == values.shape[0] and np.prod(arr_value.shape) == np.prod(values.shape):
elif len(arr_value.shape) and arr_value.shape[0] == values.shape[0] and np.prod(arr_value.shape) == np.prod(values.shape):
values = arr_value.reshape(values.shape)

# set
else:
values[indexer] = value

Expand Down
42 changes: 42 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,48 @@ def test_ix_get_set_consistency(self):
self.assert_(df.ix['e', 8] == 45)
self.assert_(df.loc['e', 8] == 45)

def test_setitem_list(self):

# GH 6043
# ix with a list
df = DataFrame(index=[0,1], columns=[0])
df.ix[1,0] = [1,2,3]
df.ix[1,0] = [1,2]

result = DataFrame(index=[0,1], columns=[0])
result.ix[1,0] = [1,2]

assert_frame_equal(result,df)

# ix with an object
class TO(object):
def __init__(self, value):
self.value = value
def __str__(self):
return "[{0}]".format(self.value)
__repr__ = __str__
def __eq__(self, other):
return self.value == other.value
def view(self):
return self

df = DataFrame(index=[0,1], columns=[0])
df.ix[1,0] = TO(1)
df.ix[1,0] = TO(2)

result = DataFrame(index=[0,1], columns=[0])
result.ix[1,0] = TO(2)

assert_frame_equal(result,df)

# remains object dtype even after setting it back
df = DataFrame(index=[0,1], columns=[0])
df.ix[1,0] = TO(1)
df.ix[1,0] = np.nan
result = DataFrame(index=[0,1], columns=[0])

assert_frame_equal(result, df)

def test_iloc_mask(self):

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