Skip to content

Commit

Permalink
added back mask method that does condition inversion
Browse files Browse the repository at this point in the history
added condition testing to where that raised ValueError on an invalid condition (e.g. not an ndarray like object)
added tests for same
  • Loading branch information
jreback committed Nov 13, 2012
1 parent 8034116 commit a414346
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -4882,6 +4882,9 @@ def where(self, cond, other=NA, inplace=False):
-------
wh: DataFrame
"""
if not hasattr(cond,'shape'):
raise ValueError('where requires an ndarray like object for its condition')

if isinstance(cond, np.ndarray):
if cond.shape != self.shape:
raise ValueError('Array onditional must be same shape as self')
Expand All @@ -4901,6 +4904,21 @@ def where(self, cond, other=NA, inplace=False):
rs = np.where(cond, self, other)
return self._constructor(rs, self.index, self.columns)

def mask(self, cond):
"""
Returns copy of self whose values are replaced with nan if the
inverted condition is True
Parameters
----------
cond: boolean DataFrame or array
Returns
-------
wh: DataFrame
"""
return self.where(~cond, NA)

_EMPTY_SERIES = Series([])


Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5220,6 +5220,18 @@ def test_where(self):
err2 = cond.ix[:2, :].values
self.assertRaises(ValueError, df.where, err2, other1)

# invalid conditions
self.assertRaises(ValueError, df.mask, True)
self.assertRaises(ValueError, df.mask, 0)

def test_mask(self):
df = DataFrame(np.random.randn(5, 3))
cond = df > 0

rs = df.where(cond, np.nan)
assert_frame_equal(rs, df.mask(df <= 0))
assert_frame_equal(rs, df.mask(~cond))


#----------------------------------------------------------------------
# Transposing
Expand Down

0 comments on commit a414346

Please sign in to comment.