Skip to content

Commit 5f22794

Browse files
committed
Merge pull request #3632 from jreback/assign_df
BUG: (GH3626) issue with alignment of a DataFrame setitem with a piece of another DataFrame
2 parents c91c44b + eff9032 commit 5f22794

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ pandas 0.11.1
126126
- Correctly parse date columns with embedded (nan/NaT) into datetime64[ns] dtype in ``read_csv``
127127
when ``parse_dates`` is specified (GH3062_)
128128
- Fix not consolidating before to_csv (GH3624_)
129+
- Fix alignment issue when setitem in a DataFrame with a piece of a DataFrame (GH3626_)
129130

130131
.. _GH3164: https://github.com/pydata/pandas/issues/3164
131132
.. _GH2786: https://github.com/pydata/pandas/issues/2786
@@ -178,6 +179,7 @@ pandas 0.11.1
178179
.. _GH3611: https://github.com/pydata/pandas/issues/3611
179180
.. _GH3062: https://github.com/pydata/pandas/issues/3062
180181
.. _GH3624: https://github.com/pydata/pandas/issues/3624
182+
.. _GH3626: https://github.com/pydata/pandas/issues/3626
181183
.. _GH1512: https://github.com/pydata/pandas/issues/1512
182184

183185

pandas/core/indexing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ def setter(item, v):
138138
# align to
139139
if item in value:
140140
v = value[item]
141-
v = v.reindex(self.obj[item].reindex(v.index).dropna().index)
141+
v = v.reindex(self.obj[item].index & v.index)
142142
setter(item, v.values)
143143
else:
144144
setter(item, np.nan)
145145

146146
# we have an equal len ndarray
147-
elif isinstance(value, np.ndarray) and value.ndim > 1:
148-
if len(labels) != len(value):
147+
elif isinstance(value, np.ndarray) and value.ndim == 2:
148+
if len(labels) != value.shape[1]:
149149
raise ValueError('Must have equal len keys and value when'
150150
' setting with an ndarray')
151151

pandas/tests/test_indexing.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,41 @@ def test_iloc_panel_issue(self):
853853
self.assert_(p.iloc[1, :3, 1].shape == (3,))
854854
self.assert_(p.iloc[:3, 1, 1].shape == (3,))
855855

856+
def test_multi_assign(self):
857+
858+
# GH 3626, an assignement of a sub-df to a df
859+
df = DataFrame({'FC':['a','b','a','b','a','b'],
860+
'PF':[0,0,0,0,1,1],
861+
'col1':range(6),
862+
'col2':range(6,12)})
863+
df.ix[1,0]=np.nan
864+
df2 = df.copy()
865+
866+
mask=~df2.FC.isnull()
867+
cols=['col1', 'col2']
868+
869+
dft = df2 * 2
870+
dft.ix[3,3] = np.nan
871+
872+
expected = DataFrame({'FC':['a',np.nan,'a','b','a','b'],
873+
'PF':[0,0,0,0,1,1],
874+
'col1':Series([0,1,4,6,8,10],dtype='float64'),
875+
'col2':[12,7,16,np.nan,20,22]})
876+
877+
878+
# frame on rhs
879+
df2.ix[mask, cols]= dft.ix[mask, cols]
880+
assert_frame_equal(df2,expected)
881+
df2.ix[mask, cols]= dft.ix[mask, cols]
882+
assert_frame_equal(df2,expected)
883+
884+
# with an ndarray on rhs
885+
df2 = df.copy()
886+
df2.ix[mask, cols]= dft.ix[mask, cols].values
887+
assert_frame_equal(df2,expected)
888+
df2.ix[mask, cols]= dft.ix[mask, cols].values
889+
assert_frame_equal(df2,expected)
890+
856891

857892
if __name__ == '__main__':
858893
import nose

0 commit comments

Comments
 (0)