/usr/share/pyshared/pandas/util/map.py is in python-pandas 0.7.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import numpy as np
from pandas import _tseries as lib
from pandas import notnull, Series
from functools import wraps
class repeat(object):
def __init__(self, obj):
self.obj = obj
def __getitem__(self, i):
return self.obj
class azip(object):
def __init__(self, *args):
self.cols = []
for a in args:
if np.isscalar(a):
self.cols.append(repeat(a))
else:
self.cols.append(a)
def __getitem__(self, i):
return [col[i] for col in self.cols]
def map_iter_args(arr, f, otherargs, n_otherargs, required, n_results):
'''
Substitute for np.vectorize with pandas-friendly dtype inference
Parameters
----------
arr : ndarray
f : function
Returns
-------
mapped : ndarray
'''
n = len(arr)
result = np.empty((n, n_results), dtype=object)
for i, val in enumerate(arr):
args = otherargs[i]
if notnull(val) and all(notnull(args[r]) for r in required):
result[i] = f(val, *args)
else:
result[i] = [np.nan] * n_results
return [lib.maybe_convert_objects(col, try_float=0) for col in result.T]
def auto_map(arr, f, otherargs, n_results=1, required='all'):
if all(np.isscalar(a) for a in otherargs):
res = lib.map_infer(arr, lambda v: f(v, *otherargs))
return Series(res, index=arr.index, copy=False)
n_otherargs = len(otherargs)
if required == 'all':
required = list(range(n_otherargs))
res = map_iter_args(arr, f, azip(*otherargs), n_otherargs, required, n_results)
res = [Series(col, index=arr.index, copy=False) for col in res]
if n_results == 1:
return res[0]
return res
def mapwrap(f, n_results_default=1, required='all'):
@wraps(f)
def wrapped(arr, n_results=None, *otherargs):
n_results = n_results or n_results_default
return auto_map(arr, f, otherargs, n_results, required)
return wrapped
|