promotetoarray#
- promotetoarray(x, keepnone=False, asobject=True, dtype=None, **kwargs)#
Small function to ensure consistent format for things that should be arrays (note:
sc.toarray()
andsc.promotetoarray()
are identical).Very similar to
numpy.array()
, with the main difference being thatsc.toarray(3)
will returnnp.array([3])
(i.e. a 1-d array that can be iterated over), whilenp.array(3)
will return a 0-d array that can’t be iterated over.- Parameters:
x (any) – a number or list of numbers
keepnone (bool) – whether
sc.toarray(None)
should returnnp.array([])
ornp.array([None], dtype=object)
asobject (bool) – whether to prefer to coerce arrays to object type rather than string
kwargs (dict) – passed to
numpy.array()
Examples:
sc.toarray(5) # Returns np.array([5]) sc.toarray([3,5]) # Returns np.array([3,5]) sc.toarray(None, skipnone=True) # Returns np.array([]) sc.toarray([1, 'foo']) # Returns np.array([1, 'foo'], dtype=object)
New in version 1.1.0: replaced “skipnone” with “keepnone”; allowed passing kwargs tonp.array()
.New in version 2.0.1: added support for pandas Series and DataFrameNew in version 3.1.0: “asobject” argument; cast mixed-type arrays to object rather than string by default