#NEWLINE# def _recode( x , levels ) : #NEWLINE# #INDENT# ' Recode categorial data to int factor.\n\n    Parameters\n    ----------\n    x : array-like\n        array like object supporting with numpy array methods of categorially\n        coded data.\n    levels : dict\n        mapping of labels to integer-codings\n\n    Returns\n    -------\n    out : instance numpy.ndarray\n\n    ' #NEWLINE# from pandas import Series #NEWLINE# name = None #NEWLINE# if isinstance ( x , Series ) : #NEWLINE# #INDENT# name = x . name #NEWLINE# x = x . values #NEWLINE# #UNINDENT# if ( x . dtype . type not in [ np . str_ , np . object_ ] ) : #NEWLINE# #INDENT# raise ValueError ( 'This is not a categorial factor. Array of str type required.' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not isinstance ( levels , dict ) ) : #NEWLINE# #INDENT# raise ValueError ( 'This is not a valid value for levels. Dict required.' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not ( np . unique ( x ) == np . unique ( list ( iterkeys ( levels ) ) ) ) . all ( ) ) : #NEWLINE# #INDENT# raise ValueError ( 'The levels do not match the array values.' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# out = np . empty ( x . shape [ 0 ] , dtype = np . int ) #NEWLINE# for ( level , coding ) in iteritems ( levels ) : #NEWLINE# #INDENT# out [ ( x == level ) ] = coding #NEWLINE# #UNINDENT# if name : #NEWLINE# #INDENT# out = Series ( out ) #NEWLINE# out . name = name #NEWLINE# #UNINDENT# return out