def sort(self): __doc__ = sort.__doc__ idxperm = argsort(self.categories) inverse = zeros_like(idxperm) inverse[idxperm] = arange(idxperm.size) newvals = inverse[self.codes] return Categorical.from_codes(newvals, self.categories[idxperm])
def argsort(self): __doc__ = argsort.__doc__ idxperm = argsort(self.categories) inverse = zeros_like(idxperm) inverse[idxperm] = arange(idxperm.size) newvals = inverse[self.codes] return argsort(newvals)
def concatenate(arrays : Sequence[Union[pdarray,Strings]]) -> Union[pdarray,Strings]: """ Concatenate an iterable of ``pdarray`` or ``Strings`` objects into one ``pdarray`` or ``Strings`` object, respectively. Parameters ---------- arrays : Sequence[Union[pdarray,Strings]] The pdarrays or Strings to concatenate. For pdarrays, all must have same dtype. Returns ------- Union[pdarray,Strings] Single pdarray or Strings object containing all values, returned in the original order Raises ------ ValueError Raised if arrays is empty or if 1..n pdarrays have differing dtypes TypeError Raised if arrays is not a pdarrays or Strings iterable RuntimeError Raised if 1..n array elements are dtypes for which concatenate has not been implemented. Notes ----- ak.concatenate is not supported for bool or float64 pdarrays Examples -------- >>> ak.concatenate([ak.array([1, 2, 3]), ak.array([4, 5, 6])]) array([1, 2, 3, 4, 5, 6]) >>> ak.concatenate([ak.array([True,False,True]),ak.array([False,True,True])]) array([True, False, True, False, True, True]) >>> ak.concatenate([ak.array(['one','two']),ak.array(['three','four','five'])]) array(['one', 'two', 'three', 'four', 'five']) """ size = 0 objtype = None dtype = None names = [] if len(cast(list,arrays)) < 1: raise ValueError("concatenate called on empty iterable") if len(cast(list,arrays)) == 1: # there are no arrays to concatenate, so just return arrays param return cast(Union[pdarray,Strings],arrays[0]) for a in arrays: if not isinstance(a, pdarray) and not isinstance(a, Strings): raise TypeError(("arrays must be an iterable of pdarrays" " or Strings")) if objtype == None: objtype = a.objtype if objtype == "pdarray": if dtype == None: dtype = a.dtype elif dtype != a.dtype: raise ValueError("All pdarrays must have same dtype") names.append(cast(pdarray,a).name) elif objtype == "str": names.append('{}+{}'.format(cast(Strings,a).offsets.name, cast(Strings,a).bytes.name)) else: raise NotImplementedError(("concatenate not implemented " + "for object type {}".format(objtype))) size += a.size if size == 0: if objtype == "pdarray": return zeros_like(cast(pdarray,arrays[0])) else: return arrays[0] repMsg = generic_msg("concatenate {} {} {}".\ format(len(cast(list,arrays)), objtype, ' '.join(names))) if objtype == "pdarray": return create_pdarray(cast(str,repMsg)) elif objtype == "str": return Strings(*(cast(str,repMsg).split('+'))) else: raise TypeError('arrays must be an array of pdarray or Strings objects')
def concatenate( arrays: Sequence[Union[pdarray, Strings, 'Categorical']], #type: ignore ordered: bool = True ) -> Union[pdarray, Strings, 'Categorical']: #type: ignore """ Concatenate a list or tuple of ``pdarray`` or ``Strings`` objects into one ``pdarray`` or ``Strings`` object, respectively. Parameters ---------- arrays : Sequence[Union[pdarray,Strings,Categorical]] The arrays to concatenate. Must all have same dtype. ordered : bool If True (default), the arrays will be appended in the order given. If False, array data may be interleaved in blocks, which can greatly improve performance but results in non-deterministic ordering of elements. Returns ------- Union[pdarray,Strings,Categorical] Single pdarray or Strings object containing all values, returned in the original order Raises ------ ValueError Raised if arrays is empty or if 1..n pdarrays have differing dtypes TypeError Raised if arrays is not a pdarrays or Strings python Sequence such as a list or tuple RuntimeError Raised if 1..n array elements are dtypes for which concatenate has not been implemented. Examples -------- >>> ak.concatenate([ak.array([1, 2, 3]), ak.array([4, 5, 6])]) array([1, 2, 3, 4, 5, 6]) >>> ak.concatenate([ak.array([True,False,True]),ak.array([False,True,True])]) array([True, False, True, False, True, True]) >>> ak.concatenate([ak.array(['one','two']),ak.array(['three','four','five'])]) array(['one', 'two', 'three', 'four', 'five']) """ from arkouda.categorical import Categorical as Categorical_ size = 0 objtype = None dtype = None names = [] if ordered: mode = 'append' else: mode = 'interleave' if len(arrays) < 1: raise ValueError("concatenate called on empty iterable") if len(arrays) == 1: return cast(Union[pdarray, Strings, Categorical_], arrays[0]) if hasattr(arrays[0], 'concatenate'): return cast( Sequence[Categorical_], cast(Categorical_, arrays[0]).concatenate(cast(Sequence[Categorical_], arrays[1:]), ordered=ordered)) for a in arrays: if not isinstance(a, pdarray) and not isinstance(a, Strings): raise TypeError(("arrays must be an iterable of pdarrays" " or Strings")) if objtype == None: objtype = a.objtype if objtype == "pdarray": if dtype == None: dtype = a.dtype elif dtype != a.dtype: raise ValueError("All pdarrays must have same dtype") names.append(cast(pdarray, a).name) elif objtype == "str": names.append('{}+{}'.format( cast(Strings, a).entry.name, "legacy_placeholder")) else: raise NotImplementedError(("concatenate not implemented " + "for object type {}".format(objtype))) size += a.size if size == 0: if objtype == "pdarray": return zeros_like(cast(pdarray, arrays[0])) else: return arrays[0] repMsg = generic_msg(cmd="concatenate", args="{} {} {} {}".\ format(len(arrays), objtype, mode, ' '.join(names))) if objtype == "pdarray": return create_pdarray(cast(str, repMsg)) elif objtype == "str": # ConcatenateMsg returns created attrib(name)+created nbytes=123 return Strings.from_return_msg(cast(str, repMsg)) else: raise TypeError( 'arrays must be an array of pdarray or Strings objects')
def concatenate(arrays : Iterable[Union[pdarray,Strings]]) \ -> Union[pdarray,Strings]: """ Concatenate an iterable of ``pdarray`` objects into one ``pdarray``. Parameters ---------- arrays : iterable of ``pdarray`` or Strings or Categorical The arrays to concatenate. Must all have same dtype. Returns ------- pdarray Single array containing all values, in original order Raises ------ ValueError Raised if arrays is empty or if 1..n pdarrays have differing dtypes TypeError Raised if arrays is not a pdarrays or Strings iterable NotImplementedError Raised if 1..n array elements are not dtypes for which concatenate has not been implemented. Examples -------- >>> ak.concatenate([ak.array([1, 2, 3]), ak.array([4, 5, 6])]) array([1, 2, 3, 4, 5, 6]) """ size = 0 objtype = None dtype = None names = [] if len(arrays) < 1: raise ValueError("concatenate called on empty iterable") if len(arrays) == 1: return arrays[0] if hasattr(arrays[0], 'concatenate'): return arrays[0].concatenate(arrays[1:]) for a in arrays: if not isinstance(a, pdarray) and not isinstance(a, Strings): raise TypeError(("arrays must be an iterable of pdarrays" " or Strings")) if objtype == None: objtype = a.objtype if objtype == "pdarray": if dtype == None: dtype = a.dtype elif dtype != a.dtype: raise ValueError("All pdarrays must have same dtype") names.append(a.name) elif objtype == "str": names.append('{}+{}'.format(a.offsets.name, a.bytes.name)) else: raise NotImplementedError(("concatenate not implemented " + "for object type {}".format(objtype))) size += a.size if size == 0: if objtype == "pdarray": return zeros_like(arrays[0]) else: return arrays[0] repMsg = generic_msg("concatenate {} {} {}".\ format(len(arrays), objtype, ' '.join(names))) if objtype == "pdarray": return create_pdarray(repMsg) elif objtype == "str": return Strings(*(repMsg.split('+')))