Skip to content

Commit 6d56e73

Browse files
committed
BUG: Indexing bugs with reordered indexes (GH6252, GH6254)
1 parent fd9d816 commit 6d56e73

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Bug Fixes
7373

7474
- Bug in version string gen. for dev versions with shallow clones / install from tarball (:issue:`6127`)
7575
- Inconsistent tz parsing Timestamp/to_datetime for current year (:issue:`5958`)
76+
- Indexing bugs with reordered indexes (:issue:`6252`, :issue:`6254`)
7677

7778
pandas 0.13.1
7879
-------------

pandas/core/internals.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,8 @@ def setitem(self, indexer, value):
633633
# if we are an exact match (ex-broadcasting),
634634
# then use the resultant dtype
635635
elif len(arr_value.shape) and arr_value.shape[0] == values.shape[0] and np.prod(arr_value.shape) == np.prod(values.shape):
636-
values = arr_value.reshape(values.shape)
636+
values[indexer] = value
637+
values = values.astype(arr_value.dtype)
637638

638639
# set
639640
else:

pandas/tests/test_indexing.py

+26
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,32 @@ def test_loc_setitem_frame(self):
646646
result = df.ix[:,1:]
647647
assert_frame_equal(result, expected)
648648

649+
# GH 6254
650+
# setting issue
651+
df = DataFrame(index=[3, 5, 4], columns=['A'])
652+
df.loc[[4, 3, 5], 'A'] = [1, 2, 3]
653+
expected = DataFrame(dict(A = Series([1,2,3],index=[4, 3, 5]))).reindex(index=[3,5,4])
654+
assert_frame_equal(df, expected)
655+
656+
# GH 6252
657+
# setting with an empty frame
658+
keys1 = ['@' + str(i) for i in range(5)]
659+
val1 = np.arange(5)
660+
661+
keys2 = ['@' + str(i) for i in range(4)]
662+
val2 = np.arange(4)
663+
664+
index = list(set(keys1).union(keys2))
665+
df = DataFrame(index = index)
666+
df['A'] = nan
667+
df.loc[keys1, 'A'] = val1
668+
669+
df['B'] = nan
670+
df.loc[keys2, 'B'] = val2
671+
672+
expected = DataFrame(dict(A = Series(val1,index=keys1), B = Series(val2,index=keys2))).reindex(index=index)
673+
assert_frame_equal(df, expected)
674+
649675
def test_loc_setitem_frame_multiples(self):
650676

651677
# multiple setting

0 commit comments

Comments
 (0)