def vectorize (f):
from functools import wraps
try:
from pandas import Series, unique
@wraps(f)
def vectorized_f (x):
# If we're given a scalar value, then simply return it.
if not hasattr(x,'__len__'):
return f(x)
# Get unique values
inputs = unique(x)
outputs = map(f,inputs)
table = dict(zip(inputs,outputs))
result = Series(x).map(table)
return result.values
except ImportError:
def cached_f(x, cache={}):
if x not in cache:
cache[x] = f(x)
return cache[x]
@wraps(f)
def vectorized_f (x):
# If we're given a scalar value, then simply return it.
if not hasattr(x,'__len__'):
return cached_f(x)
return map(cached_f,x)
return vectorized_f
# The type of data returned by the Buffer iterator.
评论列表
文章目录