Skip to content

API: preprocessing.cleaning

skyulf.preprocessing.cleaning

Cleaning nodes package.

Importing this package registers all cleaning nodes (text, invalid-value, value, and alias replacement) and re-exports their public classes plus the shared alias-mapping constants for backward compatibility.

InvalidValueReplacementApplier

Bases: BaseApplier

Source code in skyulf-core/skyulf/preprocessing/cleaning/invalid_value.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
class InvalidValueReplacementApplier(BaseApplier):
    @apply_method
    def apply(self, X: Any, _y: Any, params: dict[str, Any]) -> Any:  # pylint: disable=arguments-differ
        return apply_dual_engine(X, params, self._apply_polars, self._apply_pandas)

    @staticmethod
    def _apply_polars(X: Any, _y: Any, params: dict[str, Any]) -> tuple[Any, Any]:
        import polars as pl

        valid = resolve_valid_columns(X, params.get("columns", []))
        if not valid:
            return X, _y

        replace_inf = params.get("replace_inf", False)
        replace_neg_inf = params.get("replace_neg_inf", False)
        rule = params.get("rule")
        final_replacement = _resolve_invalid_replacement(params)
        min_value = params.get("min_value")
        max_value = params.get("max_value")

        exprs = []
        for col in valid:
            expr = pl.col(col)
            expr = _invalid_inf_replacement_polars(
                expr, replace_inf, replace_neg_inf, final_replacement
            )
            expr = _invalid_rule_polars(expr, rule, final_replacement, min_value, max_value)
            exprs.append(expr.alias(col))
        return X.with_columns(exprs), _y

    @staticmethod
    def _apply_pandas_column(
        df_out: Any,
        col: str,
        replace_inf: bool,
        replace_neg_inf: bool,
        rule: Any,
        final_replacement: Any,
        min_value: Any,
        max_value: Any,
    ) -> None:
        """Coerce, replace inf/-inf, and apply the invalid-value rule for a single column in-place."""
        to_replace = []
        if replace_inf:
            to_replace.append(np.inf)
        if replace_neg_inf:
            to_replace.append(-np.inf)
        # Skip entirely when no rule/inf-replacement is configured for this
        # column -- a true no-op, matching the polars path. Previously this
        # unconditionally ran pd.to_numeric(..., errors="coerce"), which
        # silently NaN'd out non-numeric columns even when nothing was
        # actually configured to change.
        if not to_replace and rule is None:
            return
        df_out[col] = pd.to_numeric(df_out[col], errors="coerce")
        if to_replace:
            df_out[col] = df_out[col].replace(to_replace, final_replacement)
        mask = _invalid_rule_pandas_mask(df_out[col], rule, min_value, max_value)
        if mask is not None:
            df_out.loc[mask, col] = final_replacement

    @staticmethod
    def _apply_pandas(X: Any, _y: Any, params: dict[str, Any]) -> tuple[Any, Any]:
        valid = resolve_valid_columns(X, params.get("columns", []))
        if not valid:
            return X, _y

        replace_inf = params.get("replace_inf", False)
        replace_neg_inf = params.get("replace_neg_inf", False)
        rule = params.get("rule")
        final_replacement = _resolve_invalid_replacement(params)
        min_value = params.get("min_value")
        max_value = params.get("max_value")

        df_out = X.copy()
        for col in valid:
            InvalidValueReplacementApplier._apply_pandas_column(
                df_out,
                col,
                replace_inf,
                replace_neg_inf,
                rule,
                final_replacement,
                min_value,
                max_value,
            )
        return df_out, _y