Skip to content

Commit

Permalink
Fix numexpr version issue
Browse files Browse the repository at this point in the history
  • Loading branch information
anjsudh committed Dec 30, 2018
1 parent 38115f6 commit 3ccb40c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
16 changes: 10 additions & 6 deletions pandas/core/computation/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import pandas as pd
from pandas.core.base import StringMixin
import pandas.core.common as com
from pandas.core.computation.check import _NUMEXPR_INSTALLED, _NUMEXPR_VERSION
from pandas.core.computation.common import _ensure_decoded, _result_type_many
from pandas.core.computation.scope import _DEFAULT_GLOBALS

Expand All @@ -25,10 +24,10 @@

_unary_math_ops = ('sin', 'cos', 'exp', 'log', 'expm1', 'log1p',
'sqrt', 'sinh', 'cosh', 'tanh', 'arcsin', 'arccos',
'arctan', 'arccosh', 'arcsinh', 'arctanh', 'abs', 'log10')
'arctan', 'arccosh', 'arcsinh', 'arctanh', 'abs', 'log10',
'floor', 'ceil'
)
_binary_math_ops = ('arctan2',)
if _NUMEXPR_INSTALLED and _NUMEXPR_VERSION >= LooseVersion('2.6.9'):
_unary_math_ops += ('floor', 'ceil')

_mathops = _unary_math_ops + _binary_math_ops

Expand Down Expand Up @@ -544,9 +543,14 @@ def __unicode__(self):


class FuncNode(object):

def __init__(self, name):
if name not in _mathops:
from pandas.core.computation.check import (_NUMEXPR_INSTALLED,
_NUMEXPR_VERSION)
if name not in _mathops or (
_NUMEXPR_INSTALLED and
_NUMEXPR_VERSION < LooseVersion('2.6.9') and
name in ('floor', 'ceil')
):
raise ValueError(
"\"{0}\" is not a supported function".format(name))

Expand Down
20 changes: 17 additions & 3 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def ne_lt_2_6_9():
return 'numexpr'


@pytest.fixture
def unary_fns_for_ne():
if _NUMEXPR_INSTALLED:
if _NUMEXPR_VERSION >= LooseVersion('2.6.9'):
return _unary_math_ops
else:
return tuple(x for x in _unary_math_ops
if x not in ("floor", "ceil"))
else:
pytest.skip("numexpr is not present")


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

Expand Down Expand Up @@ -1632,18 +1644,20 @@ def eval(self, *args, **kwargs):
kwargs['level'] = kwargs.pop('level', 0) + 1
return pd.eval(*args, **kwargs)

def test_unary_functions(self):
def test_unary_functions(self, unary_fns_for_ne):
df = DataFrame({'a': np.random.randn(10)})
a = df.a

for fn in self.unary_fns:
for fn in unary_fns_for_ne:
expr = "{0}(a)".format(fn)
got = self.eval(expr)
with np.errstate(all='ignore'):
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):
def test_floor_and_ceil_functions_raise_error(self,
ne_lt_2_6_9,
unary_fns_for_ne):
for fn in ('floor', 'ceil'):
msg = "\"{0}\" is not a supported function".format(fn)
with pytest.raises(ValueError, match=msg):
Expand Down

0 comments on commit 3ccb40c

Please sign in to comment.