def broadcast(self, values : pdarray) -> pdarray: """ Fill each group's segment with a constant value. Parameters ---------- values : pdarray The values to put in each group's segment Returns ------- pdarray The broadcast values Raises ------ TypeError Raised if value is not a pdarray object ValueError Raised if the values array does not have one value per segment Notes ----- This function is a sparse analog of ``np.broadcast``. If a GroupBy object represents a sparse matrix (tensor), then this function takes a (dense) column vector and replicates each value to the non-zero elements in the corresponding row. The returned array is in permuted (grouped) order. To get back to the order of the array on which GroupBy was called, the user must invert the permutation (see below). Examples -------- >>> a = ak.array([0, 1, 0, 1, 0]) >>> values = ak.array([3, 5]) >>> g = ak.GroupBy(a) # Result is in grouped order >>> g.broadcast(values) array([3, 3, 3, 5, 5] >>> b = ak.zeros_like(a) # Result is in original order >>> b[g.permutation] = g.broadcast(values) >>> b array([3, 5, 3, 5, 3]) """ if not isinstance(values, pdarray): raise TypeError("Vals must be pdarray") if values.size != self.segments.size: raise ValueError("Must have one value per segment") temp = zeros(self.size, values.dtype) if values.size == 0: return temp diffs = concatenate((array([values[0]]), values[1:] - values[:-1])) temp[self.segments] = diffs return cumsum(temp)
def broadcast(self, values : pdarray) -> pdarray: """ Fill each group's segment with a constant value. Parameters ---------- values : pdarray The values to put in each group's segment Returns ------- pdarray The broadcast values Raises ------ TypeError Raised if value is not a pdarray object ValueError Raised if the values array does not have one value per segment Notes ----- This function is a sparse analog of ``np.broadcast``. If a GroupBy object represents a sparse matrix (tensor), then this function takes a (dense) column vector and replicates each value to the non-zero elements in the corresponding row. The returned array is in permuted (grouped) order. To get back to the order of the array on which GroupBy was called, the user must invert the permutation (see below). Examples -------- >>> a = ak.array([0, 1, 0, 1, 0]) >>> values = ak.array([3, 5]) >>> g = ak.GroupBy(a) # Result is in grouped order >>> g.broadcast(values) array([3, 3, 3, 5, 5] >>> b = ak.zeros_like(a) # Result is in original order >>> b[g.permutation] = g.broadcast(values) >>> b array([3, 5, 3, 5, 3]) >>> a = ak.randint(1,5,10) >>> a array([3, 1, 4, 4, 4, 1, 3, 3, 2, 2]) >>> g = ak.GroupBy(a) >>> keys,counts = g.count() >>> g.broadcast(counts > 2) array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1]) >>> g.broadcast(counts == 3) array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1]) >>> g.broadcast(counts < 4) array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) """ '''if values a boolean array, convert to an int64 array, which is needed for now because Arkouda does not support broadcasting of boolean arrays''' if values.dtype == np.bool: values = 1*values if values.size != self.segments.size: raise ValueError("Must have one value per segment") temp = zeros(self.size, values.dtype) if values.size == 0: return temp diffs = concatenate((array([values[0]]), values[1:] - values[:-1])) temp[self.segments] = diffs return cumsum(temp)