class sanitize(data=None, returninds=False, replacenans=None, defaultval=None, die=True, verbose=False, label=None)[source]#

Sanitize input to remove NaNs. (NB: sc.sanitize() and sc.rmnans() are aliases.)

Returns an array with the sanitized data. If replacenans=True, the sanitized array is of the same length/size as data. If replacenans=False, the sanitized array may be shorter than data.

  • data (arr/list) – array or list with numbers to be sanitized

  • returninds (bool) – whether to return indices of non-nan/valid elements, indices are with respect the shape of data

  • replacenans (float/str) – whether to replace the NaNs with the specified value, or if True or a string, using interpolation

  • defaultval (float) – value to return if the sanitized array is empty

  • die (bool) – whether to raise an exception if the sanitization failed (otherwise return an empty array)

  • verbose (bool) – whether to print out a warning if no valid values are found

  • label (str) – human readable label for data (for use with verbose mode only)


data = [3, 4, np.nan, 8, 2, np.nan, np.nan, 8]
sanitized1, inds = sc.sanitize(data, returninds=True) # Remove NaNs
sanitized2 = sc.sanitize(data, replacenans=True) # Replace NaNs using nearest neighbor interpolation
sanitized3 = sc.sanitize(data, replacenans='nearest') # Eequivalent to replacenans=True
sanitized4 = sc.sanitize(data, replacenans='linear') # Replace NaNs using linear interpolation
sanitized5 = sc.sanitize(data, replacenans=0) # Replace NaNs with 0
New in version 2.0.0: handle multidimensional arrays
New in version 3.0.0: return zero-length arrays if all NaN