def value_counts(pda): """ Count the occurrences of the unique values of an array. Parameters ---------- pda : pdarray, int64 The array of values to count Returns ------- unique_values : pdarray, int64 The unique values, sorted in ascending order counts : pdarray, int64 The number of times the corresponding unique value occurs See Also -------- unique, histogram Notes ----- This function differs from ``histogram()`` in that it only returns counts for values that are present, leaving out empty "bins". Examples -------- >>> A = ak.array([2, 0, 2, 4, 0, 0]) >>> ak.value_counts(A) (array([0, 2, 4]), array([3, 2, 1])) """ return unique(pda, return_counts=True)
def value_counts(pda: pdarray) -> Union[Categorical, # type: ignore Tuple[Union[pdarray, Strings], Optional[pdarray]]]: """ Count the occurrences of the unique values of an array. Parameters ---------- pda : pdarray, int64 The array of values to count Returns ------- unique_values : pdarray, int64 or Strings The unique values, sorted in ascending order counts : pdarray, int64 The number of times the corresponding unique value occurs Raises ------ TypeError Raised if the parameter is not a pdarray See Also -------- unique, histogram Notes ----- This function differs from ``histogram()`` in that it only returns counts for values that are present, leaving out empty "bins". This function delegates all logic to the unique() method where the return_counts parameter is set to True. Examples -------- >>> A = ak.array([2, 0, 2, 4, 0, 0]) >>> ak.value_counts(A) (array([0, 2, 4]), array([3, 2, 1])) """ return unique(pda, return_counts=True)
def _binop(self, other: Union[Categorical, str_scalars], op: str_scalars) -> pdarray: """ Executes the requested binop on this Categorical instance and returns the results within a pdarray object. Parameters ---------- other : Union[Categorical,str_scalars] the other object is a Categorical object or string scalar op : str_scalars name of the binary operation to be performed Returns ------- pdarray encapsulating the results of the requested binop Raises - ----- ValueError Raised if (1) the op is not in the self.BinOps set, or (2) if the sizes of this and the other instance don't match RuntimeError Raised if a server-side error is thrown while executing the binary operation """ if op not in self.BinOps: raise NotImplementedError("Categorical: unsupported operator: {}".\ format(op)) if np.isscalar(other) and resolve_scalar_dtype(other) == "str": idxresult = self.categories._binop(other, op) return idxresult[self.codes] if self.size != cast(Categorical, other).size: raise ValueError("Categorical {}: size mismatch {} {}".\ format(op, self.size, cast(Categorical,other).size)) if isinstance(other, Categorical): if (self.categories.size == other.categories.size) and (self.categories == other.categories).all(): # Because categories are identical, codes can be compared directly return self.codes._binop(other.codes, op) else: # Remap both codes to the union of categories union = unique( concatenate((self.categories, other.categories), ordered=False)) newinds = arange(union.size) # Inds of self.categories in unioned categories selfnewinds = newinds[in1d(union, self.categories)] # Need a permutation and segments to broadcast new codes if self.permutation is None or self.segments is None: g = GroupBy(self.codes) self.permutation = g.permutation self.segments = g.segments # Form new codes by broadcasting new indices for unioned categories selfnewcodes = broadcast(self.segments, selfnewinds, self.size, self.permutation) # Repeat for other othernewinds = newinds[in1d(union, other.categories)] if other.permutation is None or other.segments is None: g = GroupBy(other.codes) other.permutation = g.permutation other.segments = g.segments othernewcodes = broadcast(other.segments, othernewinds, other.size, other.permutation) # selfnewcodes and othernewcodes now refer to same unioned categories # and can be compared directly return selfnewcodes._binop(othernewcodes, op) else: raise NotImplementedError( ("Operations between Categorical and " + "non-Categorical not yet implemented. " + "Consider converting operands to Categorical."))