def compute(self, context, expr): entity = context.entity baseperiod = entity.base_period period = context.period - 1 typemap = {bool: int, int: int, float: float} res_type = typemap[getdtype(expr, context)] res_size = len(entity.array) sum_values = np.zeros(res_size, dtype=res_type) id_to_rownum = context.id_to_rownum while period >= baseperiod: ids, values = self.value_for_period(expr, period, context, fill=None) # filter out lines which are present because there was a value for # that individual at that period but not for that column acceptable_rows = hasvalue(values) acceptable_ids = ids[acceptable_rows] if len(acceptable_ids): acceptable_values = values[acceptable_rows] value_rows = id_to_rownum[acceptable_ids] period_value = np.zeros(res_size, dtype=np.float) safe_put(period_value, value_rows, acceptable_values) sum_values += period_value period -= 1 return sum_values
def compute(self, context, expr, filter=None, weights=None, skip_na=True): filter_expr = self._getfilter(context, filter) if filter_expr is not None: expr = BinaryOp('*', expr, filter_expr) if weights is not None: expr_dtype = getdtype(expr, context) # missing (-1) * weight should be missing (-1) if skip_na and np.issubdtype(expr_dtype, np.integer): # expr = where(expr != -1, expr * weights, -1) missing_int = missing_values[int] expr = exprmisc.Where(ComparisonOp('!=', expr, missing_int), BinaryOp('*', expr, weights), missing_int) else: # expr = expr * weights expr = BinaryOp('*', expr, weights) values = expr_eval(expr, context) values = np.asarray(values) return na_sum(values) if skip_na else np.sum(values)
def _getfilter(context, filter): ctx_filter = context.filter_expr # FIXME: this is a hack and shows that the not_hashable filter_expr in # context is not really a good solution. We should rather add a flag # in the context "ishardsubset" or something like that. if filter is not_hashable: filter_expr = ctx_filter elif ctx_filter is not_hashable: filter_expr = filter elif filter is not None and ctx_filter is not None: filter_expr = LogicalOp('&', ctx_filter, filter) elif filter is not None: filter_expr = filter elif ctx_filter is not None: filter_expr = ctx_filter else: filter_expr = None if filter_expr is not None and \ getdtype(filter_expr, context) is not bool: raise Exception("filter must be a boolean expression") return filter_expr
def dtype(self, context): target_context = self.target_context(context) return getdtype(self.target_expr, target_context)
def add_filter(expr, filter): if filter is not None: missing_value = missing_values[getdtype(expr, None)] return Where(filter, expr, missing_value) else: return expr
def dtype(self, context): assert getdtype(self.cond, context) == bool return coerce_types(context, self.iftrue, self.iffalse)
def dtype(self, context): # TODO: merge this typemap with tsum's typemap = {bool: int, int: int, float: float} return typemap[getdtype(self.args[0], context)]
def dtype(self, context): assert getdtype(self.args[0], context) == bool return int