Skip to content

BUG: (GH3626) issue with alignment of a DataFrame setitem with a piece of another DataFrame #3632

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 2 commits into from
May 17, 2013
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 RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
35 changes: 35 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down