示例#1
0
 def aggregate(self, func, axis=0, *args, **kwargs):
     if isinstance(func, list) and len(func) > 1:
         # Aggregate each column separately, then stick them all together.
         rows = [self.agg([f], *args, **kwargs) for f in func]
         return frame_base.DeferredFrame.wrap(
             expressions.ComputedExpression('join_aggregate',
                                            lambda *rows: pd.concat(rows),
                                            [row._expr for row in rows]))
     else:
         # We're only handling a single column.
         base_func = func[0] if isinstance(func, list) else func
         if _is_associative(base_func) and not args and not kwargs:
             intermediate = expressions.elementwise_expression(
                 'pre_aggregate',
                 lambda s: s.agg([base_func], *args, **kwargs),
                 [self._expr])
             allow_nonparallel_final = True
         else:
             intermediate = self._expr
             allow_nonparallel_final = None  # i.e. don't change the value
         with expressions.allow_non_parallel_operations(
                 allow_nonparallel_final):
             return frame_base.DeferredFrame.wrap(
                 expressions.ComputedExpression(
                     'aggregate',
                     lambda s: s.agg(func, *args, **kwargs), [intermediate],
                     preserves_partition_by=partitionings.Singleton(),
                     requires_partition_by=partitionings.Singleton()))
示例#2
0
    def wrapper(*args, **kwargs):
        for key, values in restrictions.items():
            if key in kwargs:
                value = kwargs[key]
            else:
                try:
                    # pylint: disable=deprecated-method
                    ix = inspect.getargspec(func).args.index(key)
                except ValueError:
                    # TODO: fix for delegation?
                    continue
                if len(args) <= ix:
                    continue
                value = args[ix]
            if not isinstance(values, list):
                values = [values]
            if value not in values:
                raise NotImplementedError('%s=%s not supported for %s' %
                                          (key, value, name))
        deferred_arg_indices = []
        deferred_arg_exprs = []
        constant_args = [None] * len(args)
        for ix, arg in enumerate(args):
            if isinstance(arg, DeferredFrame):
                deferred_arg_indices.append(ix)
                deferred_arg_exprs.append(arg._expr)
            elif isinstance(arg, pd.core.generic.NDFrame):
                deferred_arg_indices.append(ix)
                deferred_arg_exprs.append(
                    expressions.ConstantExpression(arg, arg[0:0]))
            else:
                constant_args[ix] = arg

        if inplace:
            actual_func = copy_and_mutate(func)
        else:
            actual_func = func

        def apply(*actual_args):
            full_args = list(constant_args)
            for ix, arg in zip(deferred_arg_indices, actual_args):
                full_args[ix] = arg
            return actual_func(*full_args, **kwargs)

        result_expr = expressions.elementwise_expression(
            name, apply, deferred_arg_exprs)
        if inplace:
            args[0]._expr = result_expr
            return args[0]

        else:
            return DeferredFrame.wrap(result_expr)