Skip to content

Commit 804eaae

Browse files
committed
in core/frame.py
changed method __getitem__ to use .mask directly (e.g. df.mask(df > 0) is equivalent semantically to df[df>0]) added inplace keyword to where method (to update the dataframe in place, default is NOT to use inplace, and return a new dataframe) changed method _boolean_set_ to use where and inplace=True (this allows alignment of the passed values and is slightly less strict than the current method) all tests pass (as well as an added test in boolean frame indexing)
1 parent 2d576ee commit 804eaae

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

pandas/core/frame.py

100644100755
+9-10
Original file line numberDiff line numberDiff line change
@@ -1775,9 +1775,8 @@ def __getitem__(self, key):
17751775
elif isinstance(self.columns, MultiIndex):
17761776
return self._getitem_multilevel(key)
17771777
elif isinstance(key, DataFrame):
1778-
values = key.values
1779-
if values.dtype == bool:
1780-
return self.values[values]
1778+
if key.values.dtype == bool:
1779+
return self.mask(key)
17811780
else:
17821781
raise ValueError('Cannot index using non-boolean DataFrame')
17831782
else:
@@ -1891,11 +1890,7 @@ def _boolean_set(self, key, value):
18911890
if self._is_mixed_type:
18921891
raise ValueError('Cannot do boolean setting on mixed-type frame')
18931892

1894-
if isinstance(value, DataFrame):
1895-
assert(value._indexed_same(self))
1896-
np.putmask(self.values, mask, value.values)
1897-
else:
1898-
self.values[mask] = value
1893+
self.where(key, value, inplace=True)
18991894

19001895
def _set_item_multiple(self, keys, value):
19011896
if isinstance(value, DataFrame):
@@ -4878,7 +4873,7 @@ def combineMult(self, other):
48784873
"""
48794874
return self.mul(other, fill_value=1.)
48804875

4881-
def where(self, cond, other):
4876+
def where(self, cond, other, inplace=False):
48824877
"""
48834878
Return a DataFrame with the same shape as self and whose corresponding
48844879
entries are from self where cond is True and otherwise are from other.
@@ -4905,9 +4900,13 @@ def where(self, cond, other):
49054900
if isinstance(other, DataFrame):
49064901
_, other = self.align(other, join='left', fill_value=NA)
49074902

4903+
if inplace:
4904+
np.putmask(self.values, cond, other)
4905+
return self
4906+
49084907
rs = np.where(cond, self, other)
49094908
return self._constructor(rs, self.index, self.columns)
4910-
4909+
49114910
def mask(self, cond):
49124911
"""
49134912
Returns copy of self whose values are replaced with nan if the

pandas/tests/test_frame.py

100644100755
+6
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ def test_getitem_boolean(self):
141141

142142
self.assertRaises(ValueError, self.tsframe.__getitem__, self.tsframe)
143143

144+
# test df[df >0] works
145+
bif = self.tsframe[self.tsframe > 0]
146+
bifw = DataFrame(np.where(self.tsframe>0,self.tsframe,np.nan),index=self.tsframe.index,columns=self.tsframe.columns)
147+
self.assert_(isinstance(bif,DataFrame))
148+
self.assert_(bif.shape == self.tsframe.shape)
149+
assert_frame_equal(bif,bifw)
144150

145151
def test_getitem_boolean_list(self):
146152
df = DataFrame(np.arange(12).reshape(3,4))

0 commit comments

Comments
 (0)