emlib/emlib.py
def NuN(value, Type=None):
    '''
    Tries to convert a string into a float, numpy.nan, or specified type.  Returns the conversion or the original string if failed.

    :param value: The string to convert.
    :param Type:  Optinal type to convert value to.
    :type value: str
    :type Type: float,int,str,...
    :returns: value
    :rtype: float,numpy.nan,str,...


    **Advantages**:
     - This function is useful in sanitizing text input files.

    **Drawbacks**:
     - Does not alert calling function if conversion failed.

    :Example:

    If value(str) is "NaN"  or "None", **NuN** will return *numpy.nan*

     >>> toTest = "NaN"
     >>> sanitized = NuN(toTest)
     >>> numpy.isnan(sanitized)
     True

    **NuN** will try to convert value (str) into a float.  If this is unsuccessful **NuN** will return the string.

     >>> a = "3.4"
     >>> b = "three point four"
     >>> a = NuN(a)
     >>> b = NuN(b)
     >>> type(a)
     <type 'float'>
     >>> type(b)
     <type 'str'>


    Empty strings will be returned as *numpy.nan*.  This is useful for importing data tables with missing values for cells.

     >>> empty = ''
     >>> sanitized = NuN(empty)
     >>> print sanitized
     nan
     >>> numpy.isnan(sanitized)
     True

    Occasionally we may want to import a series of text values as int instead of float.

     >>> string = "5"
     >>> integer = NuN(string, Type=int)
     >>> type(integer)
     <type 'int'>

    '''
    if not value:
        return np.nan
    if (value.lower() == 'nan') or (value.lower() == '') or (value.lower() == 'none'):
        return np.nan
    else:
        if Type:
            try:
                Type(value)
                return Type(value)
            except:
                return value
        else:
            try:
                float(value)
                return float(value)
            except ValueError:
                return value

    Tries to convert a string into a float, numpy.nan, or specified type.  Returns the conversion or the original string if failed.

    :param value: The string to convert.
    :param Type:  Optinal type to convert value to.
    :type value: str
    :type Type: float,int,str,...
    :returns: value
    :rtype: float,numpy.nan,str,...


    **Advantages**:
     - This function is useful in sanitizing text input files.

    **Drawbacks**:
     - Does not alert calling function if conversion failed.

    :Example:

    If value(str) is "NaN"  or "None", **NuN** will return *numpy.nan*

     >>> toTest = "NaN"
     >>> sanitized = NuN(toTest)
     >>> numpy.isnan(sanitized)
     True

    **NuN** will try to convert value (str) into a float.  If this is unsuccessful **NuN** will return the string.

     >>> a = "3.4"
     >>> b = "three point four"
     >>> a = NuN(a)
     >>> b = NuN(b)
     >>> type(a)
     <type 'float'>
     >>> type(b)
     <type 'str'>


    Empty strings will be returned as *numpy.nan*.  This is useful for importing data tables with missing values for cells.

     >>> empty = ''
     >>> sanitized = NuN(empty)
     >>> print sanitized
     nan
     >>> numpy.isnan(sanitized)
     True

    Occasionally we may want to import a series of text values as int instead of float.

     >>> string = "5"
     >>> integer = NuN(string, Type=int)
     >>> type(integer)
     <type 'int'>

    
Tries to convert a string into a float, numpy.nan, or specified type.  Returns the conversion or the original string if failed.
Tries to convert a string into a float numpy . nan or specified type . Returns the conversion or the original string if failed .
def NuN(value, Type=None):
    if not value:
        return np.nan
    if (value.lower() == 'nan') or (value.lower() == '') or (value.lower() == 'none'):
        return np.nan
    else:
        if Type:
            try:
                Type(value)
                return Type(value)
            except:
                return value
        else:
            try:
                float(value)
                return float(value)
            except ValueError:
                return value
def NuN ( value , Type = None ) : if not value : return np . nan if ( value . lower ( ) == 'nan' ) or ( value . lower ( ) == '' ) or ( value . lower ( ) == 'none' ) : return np . nan else : if Type : try : Type ( value ) return Type ( value ) except : return value else : try : float ( value ) return float ( value ) except ValueError : return value
Tries to convert a string into a float, numpy.nan, or specified type.
Tries to convert a string into a float numpy . nan or specified type .
'''
    Tries to convert a string into a float, numpy.nan, or specified type.  Returns the conversion or the original string if failed.

    :param value: The string to convert.
    :param Type:  Optinal type to convert value to.
    :type value: str
    :type Type: float,int,str,...
    :returns: value
    :rtype: float,numpy.nan,str,...


    **Advantages**:
     - This function is useful in sanitizing text input files.

    **Drawbacks**:
     - Does not alert calling function if conversion failed.

    :Example:

    If value(str) is "NaN"  or "None", **NuN** will return *numpy.nan*

     >>> toTest = "NaN"
     >>> sanitized = NuN(toTest)
     >>> numpy.isnan(sanitized)
     True

    **NuN** will try to convert value (str) into a float.  If this is unsuccessful **NuN** will return the string.

     >>> a = "3.4"
     >>> b = "three point four"
     >>> a = NuN(a)
     >>> b = NuN(b)
     >>> type(a)
     <type 'float'>
     >>> type(b)
     <type 'str'>


    Empty strings will be returned as *numpy.nan*.  This is useful for importing data tables with missing values for cells.

     >>> empty = ''
     >>> sanitized = NuN(empty)
     >>> print sanitized
     nan
     >>> numpy.isnan(sanitized)
     True

    Occasionally we may want to import a series of text values as int instead of float.

     >>> string = "5"
     >>> integer = NuN(string, Type=int)
     >>> type(integer)
     <type 'int'>

    '''
None
float,numpy.nan,str,...
**Advantages**:
 - This function is useful in sanitizing text input files.

**Drawbacks**:
 - Does not alert calling function if conversion failed.
value
The string to convert.
The string to convert .
Type
Optinal type to convert value to.
Optinal type to convert value to .
Example
If value(str) is "NaN"  or "None", **NuN** will return *numpy.nan
>>> toTest = "NaN"
>>> sanitized = NuN(toTest)
>>> numpy.isnan(sanitized)
True

NuN** will try to convert value (str) into a float.  If this is unsuccessful **NuN** will return the string.



Empty strings will be returned as *numpy.nan*.  This is useful for importing data tables with missing values for cells.

>>> empty = ''
>>> sanitized = NuN(empty)
>>> print sanitized
nan
>>> numpy.isnan(sanitized)
True

Occasionally we may want to import a series of text values as int instead of float.

>>> string = "5"
>>> integer = NuN(string, Type=int)
>>> type(integer)

If value ( str ) is " NaN " or " None " ** NuN ** will return * numpy . nan >>> toTest = " NaN " >>> sanitized = NuN ( toTest ) >>> numpy . isnan ( sanitized ) True NuN ** will try to convert value ( str ) into a float . If this is unsuccessful ** NuN ** will return the string . Empty strings will be returned as * numpy . nan * . This is useful for importing data tables with missing values for cells . >>> empty = ' ' >>> sanitized = NuN ( empty ) >>> print sanitized nan >>> numpy . isnan ( sanitized ) True Occasionally we may want to import a series of text values as int instead of float . >>> string = " 5 " >>> integer = NuN ( string Type = int ) >>> type ( integer )