Пример #1
0
 def run_binary_test(self, df, other, assert_func, test_flex=False,
                     numexpr_ops=set(['gt', 'lt', 'ge', 'le', 'eq', 'ne'])):
     """
     tests solely that the result is the same whether or not numexpr is
     enabled.  Need to test whether the function does the correct thing
     elsewhere.
     """
     expr._MIN_ELEMENTS = 0
     expr.set_test_mode(True)
     operations = ['gt', 'lt', 'ge', 'le', 'eq', 'ne']
     for arith in operations:
         if test_flex:
             op = lambda x, y: getattr(df, arith)(y)
             op.__name__ = arith
         else:
             op = getattr(operator, arith)
         expr.set_use_numexpr(False)
         expected = op(df, other)
         expr.set_use_numexpr(True)
         expr.get_test_result()
         result = op(df, other)
         used_numexpr = expr.get_test_result()
         try:
             if arith in numexpr_ops:
                 assert used_numexpr, "Did not use numexpr as expected."
             else:
                 assert not used_numexpr, "Used numexpr unexpectedly."
             assert_func(expected, result)
         except Exception:
             pprint_thing("Failed test with operation %r" % arith)
             pprint_thing("test_flex was %r" % test_flex)
             raise
Пример #2
0
    def run_arithmetic_test(self, df, other, assert_func, check_dtype=False,
                            test_flex=True):
        expr._MIN_ELEMENTS = 0
        operations = ['add', 'sub', 'mul', 'mod', 'truediv', 'floordiv', 'pow']
        if not compat.PY3:
            operations.append('div')
        for arith in operations:
            operator_name = arith
            if arith == 'div':
                operator_name = 'truediv'

            if test_flex:
                op = lambda x, y: getattr(df, arith)(y)
                op.__name__ = arith
            else:
                op = getattr(operator, operator_name)
            expr.set_use_numexpr(False)
            expected = op(df, other)
            expr.set_use_numexpr(True)
            result = op(df, other)
            try:
                if check_dtype:
                    if arith == 'truediv':
                        assert expected.dtype.kind == 'f'
                assert_func(expected, result)
            except Exception:
                pprint_thing("Failed test with operator %r" % op.__name__)
                raise
    def test_binary_ops(self):
        def testit():

            for f, f2 in [(self.frame, self.frame2),
                          (self.mixed, self.mixed2)]:

                for op, op_str in [('add', '+'), ('sub', '-'), ('mul', '*'),
                                   ('div', '/'), ('pow', '**')]:
                    if op == 'div':
                        op = getattr(operator, 'truediv', None)
                    else:
                        op = getattr(operator, op, None)
                    if op is not None:
                        result = expr._can_use_numexpr(op, op_str, f, f,
                                                       'evaluate')
                        self.assertNotEqual(result, f._is_mixed_type)

                        result = expr.evaluate(op, op_str, f, f,
                                               use_numexpr=True)
                        expected = expr.evaluate(op, op_str, f, f,
                                                 use_numexpr=False)
                        tm.assert_numpy_array_equal(result, expected.values)

                        result = expr._can_use_numexpr(op, op_str, f2, f2,
                                                       'evaluate')
                        self.assertFalse(result)

        expr.set_use_numexpr(False)
        testit()
        expr.set_use_numexpr(True)
        expr.set_numexpr_threads(1)
        testit()
        expr.set_numexpr_threads()
        testit()
Пример #4
0
    def test_boolean_ops(self):


        def testit():
            for f, f2 in [ (self.frame, self.frame2), (self.mixed, self.mixed2) ]:

                f11 = f
                f12 = f + 1

                f21 = f2
                f22 = f2 + 1

                for op, op_str in [('gt','>'),('lt','<'),('ge','>='),('le','<='),('eq','=='),('ne','!=')]:

                    op = getattr(operator,op)

                    result   = expr._can_use_numexpr(op, op_str, f11, f12, 'evaluate')
                    self.assertNotEqual(result, f11._is_mixed_type)

                    result   = expr.evaluate(op, op_str, f11, f12, use_numexpr=True)
                    expected = expr.evaluate(op, op_str, f11, f12, use_numexpr=False)
                    tm.assert_numpy_array_equal(result,expected.values)

                    result   = expr._can_use_numexpr(op, op_str, f21, f22, 'evaluate')
                    self.assertFalse(result)

        expr.set_use_numexpr(False)
        testit()
        expr.set_use_numexpr(True)
        expr.set_numexpr_threads(1)
        testit()
        expr.set_numexpr_threads()
        testit()
Пример #5
0
 def run_frame(self,
               df,
               other,
               binary_comp=None,
               run_binary=True,
               **kwargs):
     self.run_arithmetic_test(df,
                              other,
                              assert_frame_equal,
                              test_flex=False,
                              **kwargs)
     self.run_arithmetic_test(df,
                              other,
                              assert_frame_equal,
                              test_flex=True,
                              **kwargs)
     if run_binary:
         if binary_comp is None:
             expr.set_use_numexpr(False)
             binary_comp = other + 1
             expr.set_use_numexpr(True)
         self.run_binary_test(df,
                              binary_comp,
                              assert_frame_equal,
                              test_flex=False,
                              **kwargs)
         self.run_binary_test(df,
                              binary_comp,
                              assert_frame_equal,
                              test_flex=True,
                              **kwargs)
Пример #6
0
    def run_arithmetic_test(self, df, other, assert_func, check_dtype=False,
                            test_flex=True):
        expr._MIN_ELEMENTS = 0
        operations = ['add', 'sub', 'mul', 'mod', 'truediv', 'floordiv', 'pow']
        if not compat.PY3:
            operations.append('div')
        for arith in operations:
            operator_name = arith
            if arith == 'div':
                operator_name = 'truediv'

            if test_flex:
                op = lambda x, y: getattr(df, arith)(y)
                op.__name__ = arith
            else:
                op = getattr(operator, operator_name)
            expr.set_use_numexpr(False)
            expected = op(df, other)
            expr.set_use_numexpr(True)
            result = op(df, other)
            try:
                if check_dtype:
                    if arith == 'truediv':
                        assert expected.dtype.kind == 'f'
                assert_func(expected, result)
            except Exception:
                com.pprint_thing("Failed test with operator %r" % op.__name__)
                raise
Пример #7
0
    def test_boolean_ops(self):


        def testit():
            for f, f2 in [ (self.frame, self.frame2), (self.mixed, self.mixed2) ]:

                f11 = f
                f12 = f + 1

                f21 = f2
                f22 = f2 + 1

                for op, op_str in [('gt','>'),('lt','<'),('ge','>='),('le','<='),('eq','=='),('ne','!=')]:

                    op = getattr(operator,op)

                    result   = expr._can_use_numexpr(op, op_str, f11, f12, 'evaluate')
                    self.assert_(result == (not f11._is_mixed_type))

                    result   = expr.evaluate(op, op_str, f11, f12, use_numexpr=True)
                    expected = expr.evaluate(op, op_str, f11, f12, use_numexpr=False)
                    assert_array_equal(result,expected.values)

                    result   = expr._can_use_numexpr(op, op_str, f21, f22, 'evaluate')
                    self.assert_(result == False)

        expr.set_use_numexpr(False)
        testit()
        expr.set_use_numexpr(True)
        expr.set_numexpr_threads(1)
        testit()
        expr.set_numexpr_threads()
        testit()
Пример #8
0
 def run_binary_test(self, df, other, assert_func, test_flex=False,
                     numexpr_ops=set(['gt', 'lt', 'ge', 'le', 'eq', 'ne'])):
     """
     tests solely that the result is the same whether or not numexpr is
     enabled.  Need to test whether the function does the correct thing
     elsewhere.
     """
     expr._MIN_ELEMENTS = 0
     expr.set_test_mode(True)
     operations = ['gt', 'lt', 'ge', 'le', 'eq', 'ne']
     for arith in operations:
         if test_flex:
             op = lambda x, y: getattr(df, arith)(y)
             op.__name__ = arith
         else:
             op = getattr(operator, arith)
         expr.set_use_numexpr(False)
         expected = op(df, other)
         expr.set_use_numexpr(True)
         expr.get_test_result()
         result = op(df, other)
         used_numexpr = expr.get_test_result()
         try:
             if arith in numexpr_ops:
                 assert used_numexpr, "Did not use numexpr as expected."
             else:
                 assert not used_numexpr, "Used numexpr unexpectedly."
             assert_func(expected, result)
         except Exception:
             com.pprint_thing("Failed test with operation %r" % arith)
             com.pprint_thing("test_flex was %r" % test_flex)
             raise
Пример #9
0
    def setup(self, use_numexpr, threads):
        self.df = DataFrame(np.random.randn(20000, 100))
        self.df2 = DataFrame(np.random.randn(20000, 100))

        if threads != 'default':
            expr.set_numexpr_threads(threads)
        if not use_numexpr:
            expr.set_use_numexpr(False)
Пример #10
0
    def test_binary_ops(self):
        def testit():

            for f, f2 in [(self.frame, self.frame2),
                          (self.mixed, self.mixed2)]:

                for op, op_str in [('add', '+'), ('sub', '-'), ('mul', '*'),
                                   ('div', '/'), ('pow', '**')]:

                    # numpy >= 1.11 doesn't handle integers
                    # raised to integer powers
                    # https://github.com/pandas-dev/pandas/issues/15363
                    if op == 'pow' and not _np_version_under1p11:
                        continue

                    if op == 'div':
                        op = getattr(operator, 'truediv', None)
                    else:
                        op = getattr(operator, op, None)
                    if op is not None:
                        result = expr._can_use_numexpr(op, op_str, f, f,
                                                       'evaluate')
                        self.assertNotEqual(result, f._is_mixed_type)

                        result = expr.evaluate(op,
                                               op_str,
                                               f,
                                               f,
                                               use_numexpr=True)
                        expected = expr.evaluate(op,
                                                 op_str,
                                                 f,
                                                 f,
                                                 use_numexpr=False)

                        if isinstance(result, DataFrame):
                            tm.assert_frame_equal(result, expected)
                        else:
                            tm.assert_numpy_array_equal(
                                result, expected.values)

                        result = expr._can_use_numexpr(op, op_str, f2, f2,
                                                       'evaluate')
                        self.assertFalse(result)

        expr.set_use_numexpr(False)
        testit()
        expr.set_use_numexpr(True)
        expr.set_numexpr_threads(1)
        testit()
        expr.set_numexpr_threads()
        testit()
Пример #11
0
 def run_frame(self, df, other, binary_comp=None, run_binary=True,
               **kwargs):
     self.run_arithmetic_test(df, other, assert_frame_equal,
                              test_flex=False, **kwargs)
     self.run_arithmetic_test(df, other, assert_frame_equal, test_flex=True,
                              **kwargs)
     if run_binary:
         if binary_comp is None:
             expr.set_use_numexpr(False)
             binary_comp = other + 1
             expr.set_use_numexpr(True)
         self.run_binary_test(df, binary_comp, assert_frame_equal,
                              test_flex=False, **kwargs)
         self.run_binary_test(df, binary_comp, assert_frame_equal,
                              test_flex=True, **kwargs)
Пример #12
0
    def test_binary_ops(self):
        def testit():

            for f, f2 in [(self.frame, self.frame2),
                          (self.mixed, self.mixed2)]:

                for op, op_str in [('add', '+'), ('sub', '-'), ('mul', '*'),
                                   ('div', '/'), ('pow', '**')]:

                    # numpy >= 1.11 doesn't handle integers
                    # raised to integer powers
                    # https://github.com/pandas-dev/pandas/issues/15363
                    if op == 'pow' and not _np_version_under1p11:
                        continue

                    if op == 'div':
                        op = getattr(operator, 'truediv', None)
                    else:
                        op = getattr(operator, op, None)
                    if op is not None:
                        result = expr._can_use_numexpr(op, op_str, f, f,
                                                       'evaluate')
                        self.assertNotEqual(result, f._is_mixed_type)

                        result = expr.evaluate(op, op_str, f, f,
                                               use_numexpr=True)
                        expected = expr.evaluate(op, op_str, f, f,
                                                 use_numexpr=False)

                        if isinstance(result, DataFrame):
                            tm.assert_frame_equal(result, expected)
                        else:
                            tm.assert_numpy_array_equal(result,
                                                        expected.values)

                        result = expr._can_use_numexpr(op, op_str, f2, f2,
                                                       'evaluate')
                        self.assertFalse(result)

        expr.set_use_numexpr(False)
        testit()
        expr.set_use_numexpr(True)
        expr.set_numexpr_threads(1)
        testit()
        expr.set_numexpr_threads()
        testit()
Пример #13
0
    def test_where(self):
        def testit():
            for f in [self.frame, self.frame2, self.mixed, self.mixed2]:

                for cond in [True, False]:

                    c = np.empty(f.shape, dtype=np.bool_)
                    c.fill(cond)
                    result = expr.where(c, f.values, f.values + 1)
                    expected = np.where(c, f.values, f.values + 1)
                    tm.assert_numpy_array_equal(result, expected)

        expr.set_use_numexpr(False)
        testit()
        expr.set_use_numexpr(True)
        expr.set_numexpr_threads(1)
        testit()
        expr.set_numexpr_threads()
        testit()
Пример #14
0
    def test_where(self):
        def testit():
            for f in [self.frame, self.frame2, self.mixed, self.mixed2]:

                for cond in [True, False]:

                    c = np.empty(f.shape, dtype=np.bool_)
                    c.fill(cond)
                    result = expr.where(c, f.values, f.values + 1)
                    expected = np.where(c, f.values, f.values + 1)
                    tm.assert_numpy_array_equal(result, expected)

        expr.set_use_numexpr(False)
        testit()
        expr.set_use_numexpr(True)
        expr.set_numexpr_threads(1)
        testit()
        expr.set_numexpr_threads()
        testit()
Пример #15
0
 def run_arithmetic_test(self, df, assert_func, check_dtype=False):
     expr._MIN_ELEMENTS = 0
     operations = ['add', 'sub', 'mul','mod','truediv','floordiv','pow']
     if not compat.PY3:
         operations.append('div')
     for arith in operations:
         op = getattr(operator, arith)
         expr.set_use_numexpr(False)
         expected = op(df, df)
         expr.set_use_numexpr(True)
         result = op(df, df)
         try:
             if check_dtype:
                 if arith == 'div':
                     assert expected.dtype.kind == df.dtype.kind
                 if arith == 'truediv':
                     assert expected.dtype.kind == 'f'
             assert_func(expected, result)
         except Exception:
             print("Failed test with operator %r" % op.__name__)
             raise
Пример #16
0
 def run_arithmetic_test(self, df, assert_func, check_dtype=False):
     expr._MIN_ELEMENTS = 0
     operations = ['add', 'sub', 'mul', 'mod', 'truediv', 'floordiv', 'pow']
     if not compat.PY3:
         operations.append('div')
     for arith in operations:
         op = getattr(operator, arith)
         expr.set_use_numexpr(False)
         expected = op(df, df)
         expr.set_use_numexpr(True)
         result = op(df, df)
         try:
             if check_dtype:
                 if arith == 'div':
                     assert expected.dtype.kind == df.dtype.kind
                 if arith == 'truediv':
                     assert expected.dtype.kind == 'f'
             assert_func(expected, result)
         except Exception:
             print("Failed test with operator %r" % op.__name__)
             raise
Пример #17
0
    def run_arithmetic(self,
                       df,
                       other,
                       assert_func,
                       check_dtype=False,
                       test_flex=True):
        expr._MIN_ELEMENTS = 0
        operations = ['add', 'sub', 'mul', 'mod', 'truediv', 'floordiv', 'pow']
        if not compat.PY3:
            operations.append('div')
        for arith in operations:

            # numpy >= 1.11 doesn't handle integers
            # raised to integer powers
            # https://github.com/pandas-dev/pandas/issues/15363
            if arith == 'pow' and not _np_version_under1p11:
                continue

            operator_name = arith
            if arith == 'div':
                operator_name = 'truediv'

            if test_flex:
                op = lambda x, y: getattr(df, arith)(y)
                op.__name__ = arith
            else:
                op = getattr(operator, operator_name)
            expr.set_use_numexpr(False)
            expected = op(df, other)
            expr.set_use_numexpr(True)

            result = op(df, other)
            try:
                if check_dtype:
                    if arith == 'truediv':
                        assert expected.dtype.kind == 'f'
                assert_func(expected, result)
            except Exception:
                pprint_thing("Failed test with operator %r" % op.__name__)
                raise
Пример #18
0
    def run_arithmetic(self, df, other, assert_func, check_dtype=False,
                       test_flex=True):
        expr._MIN_ELEMENTS = 0
        operations = ['add', 'sub', 'mul', 'mod', 'truediv', 'floordiv', 'pow']
        if not compat.PY3:
            operations.append('div')
        for arith in operations:

            # numpy >= 1.11 doesn't handle integers
            # raised to integer powers
            # https://github.com/pandas-dev/pandas/issues/15363
            if arith == 'pow' and not _np_version_under1p11:
                continue

            operator_name = arith
            if arith == 'div':
                operator_name = 'truediv'

            if test_flex:
                op = lambda x, y: getattr(df, arith)(y)
                op.__name__ = arith
            else:
                op = getattr(operator, operator_name)
            expr.set_use_numexpr(False)
            expected = op(df, other)
            expr.set_use_numexpr(True)

            result = op(df, other)
            try:
                if check_dtype:
                    if arith == 'truediv':
                        assert expected.dtype.kind == 'f'
                assert_func(expected, result)
            except Exception:
                pprint_thing("Failed test with operator %r" % op.__name__)
                raise
Пример #19
0
 def teardown(self, use_numexpr, threads):
     expr.set_use_numexpr(True)
     expr.set_numexpr_threads()
Пример #20
0
 def setup(self):
     if (expr is None):
         raise NotImplementedError
     self.df = DataFrame(np.random.randn(50000, 100))
     self.df2 = DataFrame(np.random.randn(50000, 100))
     expr.set_use_numexpr(False)
Пример #21
0
 def teardown(self):
     expr.set_use_numexpr(True)
Пример #22
0
 def setup(self):
     self.df = DataFrame(np.random.randn(50000, 100))
     self.df2 = DataFrame(np.random.randn(50000, 100))
     expr.set_use_numexpr(False)
Пример #23
0
 def teardown(self):
     expr.set_use_numexpr(True)
Пример #24
0
 def setup(self):
     self.df = DataFrame(np.random.randn(20000, 100))
     self.df2 = DataFrame(np.random.randn(20000, 100))
     expr.set_use_numexpr(False)
Пример #25
0
 def setup(self):
     if (expr is None):
         raise NotImplementedError
     self.df = DataFrame(np.random.randn(50000, 100))
     self.df2 = DataFrame(np.random.randn(50000, 100))
     expr.set_use_numexpr(False)