diff --git a/pandas/core/frame.py b/pandas/core/frame.py index fe7ca6fa5c9b1..c9184f148e5a9 100755 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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') @@ -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([]) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index aec7ddffd84e4..dcc7bcb909cd4 100755 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -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