diff --git a/RELEASE.rst b/RELEASE.rst index 219eec42ec20f..19073c97d92eb 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -125,6 +125,7 @@ pandas 0.11.1 - Correctly parse date columns with embedded (nan/NaT) into datetime64[ns] dtype in ``read_csv`` when ``parse_dates`` is specified (GH3062_) - Fix not consolidating before to_csv (GH3624_) + - Fix alignment issue when setitem in a DataFrame with a piece of a DataFrame (GH3626_) .. _GH3164: https://github.com/pydata/pandas/issues/3164 .. _GH2786: https://github.com/pydata/pandas/issues/2786 @@ -177,6 +178,7 @@ pandas 0.11.1 .. _GH3611: https://github.com/pydata/pandas/issues/3611 .. _GH3062: https://github.com/pydata/pandas/issues/3062 .. _GH3624: https://github.com/pydata/pandas/issues/3624 +.. _GH3626: https://github.com/pydata/pandas/issues/3626 .. _GH1512: https://github.com/pydata/pandas/issues/1512 diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index dd27fa5c3473c..1cbc5abdc3ea3 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -138,14 +138,14 @@ def setter(item, v): # align to if item in value: v = value[item] - v = v.reindex(self.obj[item].reindex(v.index).dropna().index) + v = v.reindex(self.obj[item].index & v.index) setter(item, v.values) else: setter(item, np.nan) # we have an equal len ndarray - elif isinstance(value, np.ndarray) and value.ndim > 1: - if len(labels) != len(value): + elif isinstance(value, np.ndarray) and value.ndim == 2: + if len(labels) != value.shape[1]: raise ValueError('Must have equal len keys and value when' ' setting with an ndarray') diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index 65e7516d4b082..f6d106f422911 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -853,6 +853,41 @@ def test_iloc_panel_issue(self): self.assert_(p.iloc[1, :3, 1].shape == (3,)) self.assert_(p.iloc[:3, 1, 1].shape == (3,)) + def test_multi_assign(self): + + # GH 3626, an assignement of a sub-df to a df + df = DataFrame({'FC':['a','b','a','b','a','b'], + 'PF':[0,0,0,0,1,1], + 'col1':range(6), + 'col2':range(6,12)}) + df.ix[1,0]=np.nan + df2 = df.copy() + + mask=~df2.FC.isnull() + cols=['col1', 'col2'] + + dft = df2 * 2 + dft.ix[3,3] = np.nan + + expected = DataFrame({'FC':['a',np.nan,'a','b','a','b'], + 'PF':[0,0,0,0,1,1], + 'col1':Series([0,1,4,6,8,10],dtype='float64'), + 'col2':[12,7,16,np.nan,20,22]}) + + + # frame on rhs + df2.ix[mask, cols]= dft.ix[mask, cols] + assert_frame_equal(df2,expected) + df2.ix[mask, cols]= dft.ix[mask, cols] + assert_frame_equal(df2,expected) + + # with an ndarray on rhs + df2 = df.copy() + df2.ix[mask, cols]= dft.ix[mask, cols].values + assert_frame_equal(df2,expected) + df2.ix[mask, cols]= dft.ix[mask, cols].values + assert_frame_equal(df2,expected) + if __name__ == '__main__': import nose