def encode(df, encoding='utf8', verbosity=1):
    if verbosity > 0:
        pbar = progressbar.ProgressBar(maxval=df.shape[1])
        pbar.start()
    for colnum, col in enumerate(df.columns):
        if isinstance(df[col], pd.Series):
            if verbosity:
                pbar.update(colnum)
            if df[col].dtype in (np.dtype('object'), np.dtype('U'), np.dtype('S')) and any(isinstance(obj, basestring) for obj in df[col]):
                strmask = np.array([isinstance(obj, basestring) for obj in df[col]])
                series = df[col].copy()
                try:
                    series[strmask] = np.char.encode(series[strmask].values.astype('U'))
                except TypeError:
                    print("Unable to convert {} elements starting at position {} in column {}".format(
                          sum(strmask), [i for i, b in enumerate(strmask) if b][:1], col))
                    raise
                except (UnicodeDecodeError, UnicodeEncodeError):
                    try:
                        series[strmask] = np.array([eval(s, {}, {}) for s in series[strmask]])
                    except (SyntaxError, UnicodeDecodeError, UnicodeEncodeError):
                        newseries = []
                        for s in series[strmask]:
                            try:
                                newseries += [s.encode('utf8')]
                            except:
                                print(u'Had trouble encoding {} so used repr to turn it into {}'.format(s, repr(transcode_unicode(s))))
                                newseries += [transcode_unicode(s)]
                        series[strmask] = np.array(newseries).astype('O')
                df[col] = series
    if verbosity:
        pbar.finish()
    return df