def _where(df_or_series, cond, other=np.nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False, replace_true=False): if df_or_series.ndim == 2 and getattr(other, 'ndim', 2) == 1 and axis is None: raise ValueError('Must specify axis=0 or 1') axis = validate_axis(axis or 0, df_or_series) op = DataFrameWhere(cond=cond, other=other, axis=axis, level=level, errors=errors, try_cast=try_cast, replace_true=replace_true) result = op(df_or_series) if inplace: df_or_series.data = result.data else: return result
def testValidateAxis(self): df = DataFrame(pd.DataFrame(np.random.rand(4, 3))) self.assertEqual(validate_axis(0, df), 0) self.assertEqual(validate_axis('index', df), 0) self.assertEqual(validate_axis(1, df), 1) self.assertEqual(validate_axis('columns', df), 1) with self.assertRaises(ValueError): validate_axis('unknown index', df) with self.assertRaises(ValueError): validate_axis(object(), df) with self.assertRaises(ValueError): validate_axis(-1, df) with self.assertRaises(ValueError): validate_axis(2, df) df2 = df[df[0] < 0.5] # create unknown shape self.assertEqual(validate_axis(0, df2), 0)
def test_validate_axis(): df = DataFrame(pd.DataFrame(np.random.rand(4, 3))) assert validate_axis(0, df) == 0 assert validate_axis('index', df) == 0 assert validate_axis(1, df) == 1 assert validate_axis('columns', df) == 1 with pytest.raises(ValueError): validate_axis('unknown index', df) with pytest.raises(ValueError): validate_axis(object(), df) with pytest.raises(ValueError): validate_axis(-1, df) with pytest.raises(ValueError): validate_axis(2, df) df2 = df[df[0] < 0.5] # create unknown shape assert validate_axis(0, df2) == 0