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 062923f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 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
7 changes: 6 additions & 1 deletion pandas/core/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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 _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 +27,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 +544,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 (
_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
31 changes: 20 additions & 11 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,24 +1625,33 @@ 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)

@tm.skip_if_ne_ge_2_6_9
def test_floor_and_ceil_functions_in_ne_lt_2_6_9(self):
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):
@tm.skip_if_ne_lt_2_6_9
def test_floor_and_ceil_functions_in_ne_ge_2_6_9(self):
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
5 changes: 4 additions & 1 deletion pandas/util/_test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_foo():
from pandas.compat.numpy import _np_version_under1p15

from pandas.core.computation.expressions import (
_NUMEXPR_INSTALLED, _USE_NUMEXPR)
_NUMEXPR_INSTALLED, _USE_NUMEXPR, _ne_version_under_2_6_9)


def safe_import(mod_name, min_version=None):
Expand Down Expand Up @@ -184,6 +184,9 @@ def decorated_func(func):
enabled=_USE_NUMEXPR,
installed=_NUMEXPR_INSTALLED))

skip_if_ne_lt_2_6_9 = pytest.mark.skipif(_ne_version_under_2_6_9)
skip_if_ne_ge_2_6_9 = pytest.mark.skipif(not _ne_version_under_2_6_9)


def parametrize_fixture_doc(*args):
"""
Expand Down

0 comments on commit 062923f

Please sign in to comment.