How can I reverse ordinal encoding within a sklearn pipeline?

I am trying to clean a dataset that contains a lot of missing values from different features that are both numerical and categorical. My idea is the following:

  1. Use OrdinalEncoder to have only numerical values and keep the missing values as NaN (can’t with OneHotEncoder since it creates new columns which stand as NaN)
  2. Use KNNImputer to impute the missing values
  3. Reverse the encoding since there is no reasonable ground to draw some order in the categories

Here is my code so far:

import pandas as pd
import numpy as np
from sklearn import set_config
set_config(transform_output="pandas")
from sklearn.preprocessing import OrdinalEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import KNNImputer
from sklearn.base import BaseEstimator, TransformerMixin


class RoundingToIntegerTransformer(BaseEstimator, TransformerMixin):
    def fit(self, X, y=None):
        return self

    def transform(self, X):
        return np.round(X)  

class ReverseOrdinalEncoder(BaseEstimator, TransformerMixin):
    def __init__(self, encoder_name, categorical_columns):
        self.encoder_name = encoder_name
        self.categorical_columns = categorical_columns

    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):
        X_ = X.copy()
        encoder = self.encoder_name
        X_categorical = X_[categorical_columns]  
        X_categorical = encoder.inverse_transform(X_categorical)
        X_[categorical_columns] = X_categorical
        return X_

data = pd.DataFrame({
    'Category1': ['a', 'b', 'a', np.nan, 'c', 'b'],
    'Category2': ['x', 'y', 'x', 'z', np.nan, 'y'],
    'Numerical1': [1.1, np.nan, 3.3, 4.4, 5.5, np.nan],
    'Numerical2': [np.nan, 2.2, np.nan, 4.4, 5.5, 6.6]
})

numerical_columns = data.select_dtypes(include='number').columns.tolist()
categorical_columns = data.select_dtypes(include='object').columns.tolist()

ordinal_encoder = OrdinalEncoder(encoded_missing_value=np.nan)

first_encoder = ColumnTransformer(transformers=[
    ('ordinal_cat', ordinal_encoder, categorical_columns)
],
 remainder='passthrough',
 verbose_feature_names_out=False)

imputer = Pipeline([
    ('KNN_imputing', KNNImputer()),
    ('rounding',  ColumnTransformer(transformers=[('round_cat', RoundingToIntegerTransformer() , categorical_columns)],
                                    remainder='passthrough',
                                    verbose_feature_names_out=False))
])

reverse_encoder = ReverseOrdinalEncoder(
    encoder_name=first_encoder.transformers[0][1], 
    categorical_columns=categorical_columns
)

preprocessor = Pipeline([
    ('encoding', first_encoder),
    ('imputing', imputer),
    ('decoding', reverse_encoder)
])

I have been trying for some time but I never succeeded, I always receive the following error:

NotFittedError: This OrdinalEncoder instance is not fitted yet. Call ‘fit’ with appropriate arguments before using this estimator.

I understand the error but I thought that since the ordinal encoder appears before in the pipeline, it would be fitted when it is then used in the reverse encoder. Is there any way to make that work?

Thank you very much

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật