#NEWLINE# def categorize( df , columns = None , **kwargs ) : #NEWLINE# #INDENT# '\n    Convert columns of dataframe to category dtype\n\n    This aids performance, both in-memory and in spilling to disk\n    ' #NEWLINE# if ( columns is None ) : #NEWLINE# #INDENT# dtypes = df . dtypes #NEWLINE# columns = [ name for ( name , dt ) in zip ( dtypes . index , dtypes . values ) if ( dt == 'O' ) ] #NEWLINE# #UNINDENT# if ( not isinstance ( columns , ( list , tuple ) ) ) : #NEWLINE# #INDENT# columns = [ columns ] #NEWLINE# #UNINDENT# distincts = [ df [ col ] . drop_duplicates ( ) for col in columns ] #NEWLINE# values = compute ( * distincts , ** kwargs ) #NEWLINE# func = partial ( _categorize_block , categories = dict ( zip ( columns , values ) ) ) #NEWLINE# return df . map_partitions ( func , columns = df . columns )