def coerce_dtypes(df, dtypes):
 for c in df.columns:
  if ((c in dtypes) and (df.dtypes[c] != dtypes[c])):
   if (np.issubdtype(df.dtypes[c], np.floating) and np.issubdtype(dtypes[c], np.integer)):
    if (df[c] % 1).any():
     msg = "Runtime type mismatch. Add {'%s': float} to dtype= keyword in read_csv/read_table"
     raise TypeError((msg % c))
   df[c] = df[c].astype(dtypes[c])

def coerce_dtypes ( df , dtypes ) : for c in df . columns : if ( ( c in dtypes ) and ( df . dtypes [ c ] != dtypes [ c ] ) ) : if ( np . issubdtype ( df . dtypes [ c ] , np . floating ) and np . issubdtype ( dtypes [ c ] , np . integer ) ) : if ( df [ c ] % 1 ) . any ( ) : msg = "Runtime type mismatch. Add {'%s': float} to dtype= keyword in read_csv/read_table" raise TypeError ( ( msg % c ) ) df [ c ] = df [ c ] . astype ( dtypes [ c ] )
coerce dataframe to dtypes safely operates in place parameters df: pandas dataframe dtypes: dict like {x: float} .