toarray#

toarray(x, keepnone=False, asobject=True, dtype=None, **kwargs)[source]#

Small function to ensure consistent format for things that should be arrays (note: sc.toarray() and sc.promotetoarray() are identical).

Very similar to numpy.array(), with the main difference being that sc.toarray(3) will return np.array([3]) (i.e. a 1-d array that can be iterated over), while np.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 return np.array([]) or np.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 to np.array().
New in version 2.0.1: added support for pandas Series and DataFrame
New in version 3.1.0: “asobject” argument; cast mixed-type arrays to object rather than string by default