Skip to content

Instantly share code, notes, and snippets.

@seibert
Created August 7, 2015 14:50
Show Gist options
  • Save seibert/c3dfa376428f15d90d19 to your computer and use it in GitHub Desktop.
Save seibert/c3dfa376428f15d90d19 to your computer and use it in GitHub Desktop.
from pandas.computation import engines
from numba import vectorize
class HSAEngine(engines.AbstractEngine):
"""Evaluate an expression using the Numba target.
"""
has_neg_frac = False
_func_cache = {}
def __init__(self, expr):
super(HSAEngine, self).__init__(expr)
#print('__init__:', expr)
self._args = [n for n in expr.names if isinstance(n, str)]
function_name = '__numba_pandas_eval_ufunc'
function_str = '''def %s(%s):
return %s
''' % (function_name, ','.join(self._args), str(expr))
if function_str in HSAEngine._func_cache:
#print('cache hit')
self._ufunc = HSAEngine._func_cache[function_str]
else:
#print('cache miss')
scope = {}
exec(function_str, scope)
self._ufunc = vectorize(nopython=True)(scope[function_name])
HSAEngine._func_cache[function_str] = self._ufunc
def _evaluate(self):
env = self.expr.env
call_args = [env.resolve(name, False) for name in self._args]
return self._ufunc(*call_args)
engines._engines['hsa'] = HSAEngine
import engine_hsa # register new engine
import pandas as pd
a = pd.DataFrame(dict(x=[1,2,3,4], y=[2,4,6,8]))
print('Input:', type(a), '\n', a)
b = a.eval('x + y', engine='hsa')
b = a.eval('x + y', engine='hsa')
print('Output:', type(b), '\n', b)
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment