Skip to content

Commit

Permalink
BUG: close #881, setitem boolean indexing on dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
adamklein committed Mar 8, 2012
1 parent 64d3239 commit 2ef6ee9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,12 @@ def _set_item_multiple(self, keys, value):
for k1, k2 in zip(keys, value.columns):
self[k1] = value[k2]
else:
self.ix[:, keys] = value
if isinstance(keys, np.ndarray) and keys.dtype == np.bool_:
# boolean slicing should happen on rows, consistent with
# behavior of getitem
self.ix[keys, :] = value
else:
self.ix[:, keys] = value

def _set_item(self, key, value):
"""
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,16 @@ def test_setitem_fancy_2d(self):
frame.ix[:, 'B':'C'] = 4.
assert_frame_equal(frame, expected)

# new corner case of boolean slicing / setting
frame = DataFrame(zip([2,3,9,6,7], [np.nan]*5),
columns=['a','b'])
lst = [100]
lst.extend([np.nan]*4)
expected = DataFrame(zip([100,3,9,6,7], lst), columns=['a','b'])
frame[frame['a'] == 2] = 100
assert_frame_equal(frame, expected)


def test_fancy_getitem_slice_mixed(self):
sliced = self.mixed_frame.ix[:, -3:]
self.assert_(sliced['D'].dtype == np.float64)
Expand Down

0 comments on commit 2ef6ee9

Please sign in to comment.