def test_map_(): for coltype in (MixedColumn, FloatColumn, IntColumn): dm = DataMatrix(length=2, default_col_type=coltype) dm.a = 1, 2 dm.a = fnc.map_(lambda x: x * 2, dm.a) eq_(dm.a, [2, 4]) ok_(isinstance(dm.a, coltype)) dm = fnc.map_(lambda **d: {'a': 0}, dm) eq_(dm.a, [0, 0]) ok_(isinstance(dm.a, coltype))
def _map(series, fnc_, **kwdict): """ visible: False desc: Applies a function to each cell. Or, if a numpy array is passed, only that array is processed. arguments: series: desc: A signal to apply the function to, or a numpy array. type: [SeriesColumn, ndarray] fnc_: desc: The function to apply. keyword-dict: kwdict: A dict with keyword arguments for fnc. returns: desc: A new signal. type: [SeriesColumn, ndarray] """ f = lambda a: fnc_(a, **kwdict) if isinstance(series, _SeriesColumn): return fnc.map_(f, series) if isinstance(series, np.ndarray): return f(series) try: len(series) except TypeError: raise TypeError(u'Expects a SeriesColumn or an iterable object') return f(np.array(series))