Example #1
0
class TestDataFrameEvalWithFrame(object):

    def setup_method(self, method):
        self.frame = DataFrame(np.random.randn(10, 3), columns=list('abc'))

    def teardown_method(self, method):
        del self.frame

    def test_simple_expr(self, parser, engine):
        res = self.frame.eval('a + b', engine=engine, parser=parser)
        expect = self.frame.a + self.frame.b
        assert_series_equal(res, expect)

    def test_bool_arith_expr(self, parser, engine):
        res = self.frame.eval('a[a < 1] + b', engine=engine, parser=parser)
        expect = self.frame.a[self.frame.a < 1] + self.frame.b
        assert_series_equal(res, expect)

    @pytest.mark.parametrize('op', ['+', '-', '*', '/'])
    def test_invalid_type_for_operator_raises(self, parser, engine, op):
        df = DataFrame({'a': [1, 2], 'b': ['c', 'd']})
        msg = r"unsupported operand type\(s\) for .+: '.+' and '.+'"

        with pytest.raises(TypeError, match=msg):
            df.eval('a {0} b'.format(op), engine=engine, parser=parser)
Example #2
0
class TestDataFrameEvalNumExprPandas(tm.TestCase):

    @classmethod
    def setUpClass(cls):
        super(TestDataFrameEvalNumExprPandas, cls).setUpClass()
        cls.engine = 'numexpr'
        cls.parser = 'pandas'
        tm.skip_if_no_ne()

    def setUp(self):
        self.frame = DataFrame(randn(10, 3), columns=list('abc'))

    def tearDown(self):
        del self.frame

    def test_simple_expr(self):
        res = self.frame.eval('a + b', engine=self.engine, parser=self.parser)
        expect = self.frame.a + self.frame.b
        assert_series_equal(res, expect)

    def test_bool_arith_expr(self):
        res = self.frame.eval('a[a < 1] + b', engine=self.engine,
                              parser=self.parser)
        expect = self.frame.a[self.frame.a < 1] + self.frame.b
        assert_series_equal(res, expect)

    def test_invalid_type_for_operator_raises(self):
        df = DataFrame({'a': [1, 2], 'b': ['c', 'd']})
        ops = '+', '-', '*', '/'
        for op in ops:
            with tm.assertRaisesRegexp(TypeError,
                                       r"unsupported operand type\(s\) for "
                                       r".+: '.+' and '.+'"):
                df.eval('a {0} b'.format(op), engine=self.engine,
                        parser=self.parser)
Example #3
0
class TestDataFrameEvalWithFrame(object):

    def setup_method(self, method):
        self.frame = DataFrame(randn(10, 3), columns=list('abc'))

    def teardown_method(self, method):
        del self.frame

    def test_simple_expr(self, parser, engine):
        res = self.frame.eval('a + b', engine=engine, parser=parser)
        expect = self.frame.a + self.frame.b
        assert_series_equal(res, expect)

    def test_bool_arith_expr(self, parser, engine):
        res = self.frame.eval('a[a < 1] + b', engine=engine, parser=parser)
        expect = self.frame.a[self.frame.a < 1] + self.frame.b
        assert_series_equal(res, expect)

    def test_invalid_type_for_operator_raises(self, parser, engine):
        df = DataFrame({'a': [1, 2], 'b': ['c', 'd']})
        ops = '+', '-', '*', '/'
        for op in ops:
            with tm.assert_raises_regex(TypeError,
                                        r"unsupported operand type\(s\) "
                                        "for .+: '.+' and '.+'"):
                df.eval('a {0} b'.format(op), engine=engine, parser=parser)
Example #4
0
 def test_invalid_type_for_operator_raises(self, parser, engine):
     df = DataFrame({'a': [1, 2], 'b': ['c', 'd']})
     ops = '+', '-', '*', '/'
     for op in ops:
         with tm.assert_raises_regex(TypeError,
                                     r"unsupported operand type\(s\) "
                                     "for .+: '.+' and '.+'"):
             df.eval('a {0} b'.format(op), engine=engine, parser=parser)
Example #5
0
 def test_invalid_type_for_operator_raises(self):
     df = DataFrame({'a': [1, 2], 'b': ['c', 'd']})
     ops = '+', '-', '*', '/'
     for op in ops:
         with tm.assertRaisesRegexp(TypeError,
                                    "unsupported operand type\(s\) for "
                                    ".+: '.+' and '.+'"):
             df.eval('a {0} b'.format(op), engine=self.engine,
                     parser=self.parser)
Example #6
0
 def test_eval_resolvers_as_list(self):
     # GH 14095
     df = DataFrame(np.random.randn(10, 2), columns=list('ab'))
     dict1 = {'a': 1}
     dict2 = {'b': 2}
     assert (df.eval('a + b', resolvers=[dict1, dict2]) ==
             dict1['a'] + dict2['b'])
     assert (pd.eval('a + b', resolvers=[dict1, dict2]) ==
             dict1['a'] + dict2['b'])
Example #7
0
    def test_invalid_type_for_operator_raises(self, parser, engine, op):
        df = DataFrame({'a': [1, 2], 'b': ['c', 'd']})
        msg = r"unsupported operand type\(s\) for .+: '.+' and '.+'"

        with pytest.raises(TypeError, match=msg):
            df.eval('a {0} b'.format(op), engine=engine, parser=parser)