How to use the numexpr.compute function in numexpr

To help you get started, we’ve selected a few numexpr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sjdv1982 / seamless / tests / test-pandeval.py View on Github external
import numpy as np
import numexpr as ne
import pandas as pd
from pandas import DataFrame, eval as pd_eval
import seamless.pandeval
from seamless.pandeval.core.computation.eval import eval
# Sample data
s = np.zeros(6, 'S5')
v = np.array([5,8,3,6,7,2])
s[:4] = "Test", "Test2", "Test", "Test"
s_u = np.array([ss.decode() for ss in s])  # s as unicode
dic = {"s": s, "v": v}
dic_u = {"s": s_u, "v": v}


print("Golden standard (numexpr with ugly syntax)\t", ne.compute("(s == 'Test') & (v > 3)", dic))

df = DataFrame(dic)
print("pandas DataFrame with bytes (error)\t\t", df.eval("s == 'Test' and v > 3").values)
try:  # ugly syntax AND does not work
    print(df.eval("s == b'Test' and v > 3").values)
except AttributeError:
    print("*" * 50)
    import traceback; traceback.print_exc(0)
    print("*" * 50)

df_u = DataFrame(dic_u)
print("pandas DataFrame with unicode (works)\t\t", df_u.eval("s == 'Test' and v > 3").values)


print("pandas eval with bytes (wrong)\t\t\t", pd_eval("s == 'Test' and v > 3", global_dict=dic))
print("pandas eval with unicode (wrong)\t\t", pd_eval("s == 'Test' and v > 3", global_dict=dic_u))
github sjdv1982 / seamless / seamless / pandeval / core / computation / expressions.py View on Github external
def _evaluate_numexpr(op, op_str, a, b, truediv=True,
                      reversed=False, **eval_kwargs):
    result = None

    if _can_use_numexpr(op, op_str, a, b, 'compute'):
        try:

            # we were originally called by a reversed op
            # method
            if reversed:
                a, b = b, a

            a_value = getattr(a, "values", a)
            b_value = getattr(b, "values", b)
            result = ne.compute('a_value {op} b_value'.format(op=op_str),
                                 local_dict={'a_value': a_value,
                                             'b_value': b_value},
                                 casting='safe', truediv=truediv,
                                 **eval_kwargs)
        except ValueError as detail:
            if 'unknown type object' in str(detail):
                pass

    if _TEST_MODE:
        _store_test_result(result is not None)

    if result is None:
        result = _evaluate_standard(op, op_str, a, b)

    return result
github sjdv1982 / seamless / seamless / pandeval / core / computation / engines.py View on Github external
def _evaluate(self):
        import numexpr as ne

        # convert the expression to a valid numexpr expression
        s = self.convert()

        try:
            env = self.expr.env
            scope = env.full_scope
            truediv = scope['truediv']
            _check_ne_builtin_clash(self.expr)
            return ne.compute(s, local_dict=scope, truediv=truediv)
        except KeyError as e:
            # python 3 compat kludge
            try:
                msg = e.message
            except AttributeError:
                msg = compat.text_type(e)
            raise UndefinedVariableError(msg)
github sjdv1982 / seamless / seamless / pandeval / core / computation / expressions.py View on Github external
def _where_numexpr(cond, a, b):
    result = None

    if _can_use_numexpr(None, 'where', a, b, 'where'):

        try:
            cond_value = getattr(cond, 'values', cond)
            a_value = getattr(a, 'values', a)
            b_value = getattr(b, 'values', b)
            result = ne.compute('where(cond_value, a_value, b_value)',
                                 local_dict={'cond_value': cond_value,
                                             'a_value': a_value,
                                             'b_value': b_value},
                                 casting='safe')
        except ValueError as detail:
            if 'unknown type object' in str(detail):
                pass
        except Exception as detail:
            raise TypeError(str(detail))

    if result is None:
        result = _where_standard(cond, a, b)

    return result