Skip to content

Commit

Permalink
numexpr less than 2.6.9
Browse files Browse the repository at this point in the history
  • Loading branch information
anjsudh committed Dec 24, 2018
1 parent 3aa2703 commit 42bc642
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pandas/core/computation/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

_NUMEXPR_INSTALLED = False
_MIN_NUMEXPR_VERSION = "2.6.1"
_ne_version_under_2_6_9 = None

try:
import numexpr as ne
ver = LooseVersion(ne.__version__)
_NUMEXPR_INSTALLED = ver >= LooseVersion(_MIN_NUMEXPR_VERSION)
_ne_version_under_2_6_9 = ver < LooseVersion('2.6.9')

if not _NUMEXPR_INSTALLED:
warnings.warn(
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/computation/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np

import pandas.core.common as com
from pandas.core.computation.check import _NUMEXPR_INSTALLED
from pandas.core.computation.check import _NUMEXPR_INSTALLED, _ne_version_under_2_6_9
from pandas.core.config import get_option

if _NUMEXPR_INSTALLED:
Expand All @@ -20,6 +20,7 @@
_TEST_MODE = None
_TEST_RESULT = None
_USE_NUMEXPR = _NUMEXPR_INSTALLED
_ne_version_under2_6_9 = _ne_version_under_2_6_9
_evaluate = None
_where = None

Expand Down
8 changes: 7 additions & 1 deletion pandas/core/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from pandas.core.base import StringMixin
import pandas.core.common as com
from pandas.core.computation.common import _ensure_decoded, _result_type_many
from pandas.core.computation.expressions import (_NUMEXPR_INSTALLED,
_ne_version_under_2_6_9)
from pandas.core.computation.scope import _DEFAULT_GLOBALS

from pandas.io.formats.printing import pprint_thing, pprint_thing_encoded
Expand All @@ -26,6 +28,7 @@
'arctan', 'arccosh', 'arcsinh', 'arctanh', 'abs', 'log10',
'floor', 'ceil')
_binary_math_ops = ('arctan2',)
_ops_for_ne_gt_2_6_9 = ('floor', 'ceil')
_mathops = _unary_math_ops + _binary_math_ops


Expand Down Expand Up @@ -542,9 +545,12 @@ def __unicode__(self):
class FuncNode(object):

def __init__(self, name):
if name not in _mathops or not hasattr(np, name):
if name not in _mathops or (
_NUMEXPR_INSTALLED and _ne_version_under_2_6_9 and name in _ops_for_ne_gt_2_6_9
):
raise ValueError(
"\"{0}\" is not a supported function".format(name))

self.name = name
self.func = getattr(np, name)

Expand Down
45 changes: 34 additions & 11 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
assert_numpy_array_equal, assert_series_equal,
assert_produces_warning)
from pandas.compat import PY3, reduce
from pandas.core.computation.expressions import (
_ne_version_under_2_6_9)


_series_frame_incompatible = _bool_ops_syms
_scalar_skip = 'in', 'not in'
Expand All @@ -54,6 +57,19 @@ def parser(request):
return request.param


@pytest.fixture
def ne_lt_2_6_9():
if not _ne_version_under_2_6_9:
pytest.skip("numexpr is > 2.6.9")
return 'numexpr'

@pytest.fixture
def ne_gt_2_6_9():
if _ne_version_under_2_6_9:
pytest.skip("numexpr is < 2.6.9")
return 'numexpr'


def engine_has_neg_frac(engine):
return _engines[engine].has_neg_frac

Expand Down Expand Up @@ -1625,24 +1641,31 @@ def eval(self, *args, **kwargs):
def test_unary_functions(self):
df = DataFrame({'a': np.random.randn(10)})
a = df.a
for fn in self.unary_fns:
unary_functions = [x for x in self.unary_fns
if x not in ('floor', 'ceil')]
for fn in unary_functions:
expr = "{0}(a)".format(fn)
got = self.eval(expr)
with np.errstate(all='ignore'):
if hasattr(np, fn):
got = self.eval(expr)
expect = getattr(np, fn)(a)
tm.assert_series_equal(got, expect, check_names=False)
expect = getattr(np, fn)(a)
tm.assert_series_equal(got, expect, check_names=False)

def test_floor_and_ceil_functions_raise_error(self, ne_lt_2_6_9):
for fn in ('floor', 'ceil'):
msg = "\"{0}\" is not a supported function".format(fn)
with pytest.raises(ValueError, match=msg):
expr = "{0}(100)".format(fn)
self.eval(expr)

def test_all_unary_functions_not_supported_in_numpy_112(self):
def test_floor_and_ceil_functions_evaluate_expressions(self, ne_gt_2_6_9):
df = DataFrame({'a': np.random.randn(10)})
a = df.a
for fn in self.unary_fns:
for fn in ('floor', 'ceil'):
expr = "{0}(a)".format(fn)
got = self.eval(expr)
with np.errstate(all='ignore'):
if not hasattr(np, fn):
msg = '"{0}" is not a supported function'.format(fn)
with pytest.raises(ValueError, match=msg):
self.eval(expr)
expect = getattr(np, fn)(a)
tm.assert_series_equal(got, expect, check_names=False)

def test_binary_functions(self):
df = DataFrame({'a': np.random.randn(10),
Expand Down

0 comments on commit 42bc642

Please sign in to comment.