checktype#

checktype(obj=None, objtype=None, subtype=None, die=False)[source]#

A convenience function for checking instances. If objtype is a type, then this function works exactly like isinstance(). But, it can also be one of the following strings:

  • ‘str’, ‘string’: string or bytes object

  • ‘num’, ‘number’: any kind of number

  • ‘arr’, ‘array’: a Numpy array (equivalent to np.ndarray)

  • ‘listlike’: a list, tuple, or array

  • ‘arraylike’: a list, tuple, or array with numeric entries

  • ‘none’: a None object

If subtype is not None, then checktype will iterate over the object and check recursively that each element matches the subtype.

Parameters:
  • obj (any) – the object to check the type of

  • objtype (str or type) – the type to confirm the object belongs to

  • subtype (str or type) – optionally check the subtype if the object is iterable

  • die (bool) – whether or not to raise an exception if the object is the wrong type

Examples:

sc.checktype(rand(10), 'array', 'number') # Returns True
sc.checktype(['a','b','c'], 'listlike') # Returns True
sc.checktype(['a','b','c'], 'arraylike') # Returns False
sc.checktype([{'a':3}], list, dict) # Returns True
New in version 2.0.1: pd.Series considered ‘array-like’
New in version 3.0.0: allow list (in addition to tuple) of types; allow checking for NoneType
New in version 3.1.3: handle exceptions when casting to “arraylike”