class PandasLabelEncoder(BaseEstimator, TransformerMixin):

    def fit(self, y):
        self.classes_ = unique1d(y)
return self

def transform(self, y):
    s = pd.Series(y).astype('category', categories=self.classes_)
return s.cat.codes
How to speed LabelEncoder up recoding a categorical variable into integers