title: How do I Label Encode using the Pipeline API?
body: <p>I want to incorporate Label Encoding through the scikit learn pipeline. Unfortunately, LabelEncoder() is broken with the pipeline API so that's not an option right now. I tried creating my own class which calls .map() to map categories to labels:</p>
  
  <pre><code>from sklearn.base import TransformerMixin
  from sklearn.base import BaseEstimator
  
  class RatingEncoder(BaseEstimator, TransformerMixin):
      """Takes in dataframe, converts all categorical Ratings columns into numerical Ratings columns
      via label-encoding"""
  
      def __init__(self):
          pass
  
      def fit(self, df, y=None):
          return self
  
      def transform(self, df, y=None):
          """"Transform all of the categorical ratings columns into numerical ratings columns"""
          for feature in df.columns:
              df[feature] = df[feature].map({
                  "Po"   : 1,
                  "Fa"   : 2,
                  "TA"   : 3,
                  "Gd"   : 4,
                  "Ex"   : 5,
              })
          return df
  </code></pre>
  
  <p>Then, I set up the following pipeline:</p>
  
  <pre><code>def select_numeric_features(df):
      return df.select_dtypes(include=np.number).columns
  
  def select_categorical_features(df):
      return df.select_dtypes(exclude=np.number).columns
  
  def select_rated_features(df):
      rated_features = []
      for column in df:
          # This criteria determines if a column is a 'rated column'
          if any(df[column] == 'TA'):
              rated_features.append(column)
      return rated_features
  
  pipeline = make_column_transformer(
  
      (RatingsTransformer(), select_rated_features),
      (SimpleImputer(strategy='constant', fill_value='None'), select_categorical_features),
      (SimpleImputer(strategy='constant', fill_value=0), select_numeric_features),
      remainder='passthrough'
  
  )
  </code></pre>
  
  <p>The problem with this is that after the RatingsTransformer() step, the categorical 'ratings' columns are supposed to become numerical columns. However, this change doesn't show up in the column selection part of the column transformer, so <code>select_numerical_features</code> and <code>select_categorical_features</code> will choose the incorrect 'ratings' columns as if they had no been mapped from categories to values. Basically, the column transformer isn't using columns that were updated in the middle of the pipeline. Any workaround for this? Or, is there a simpler solution to LabelEncoding using the pipeline API?</p>