Skip to content

Commit 9a8e835

Browse files
author
Lisa McBride
committed
exposed the list of values to be considered as null in the Cleaner
1 parent b779a0d commit 9a8e835

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

skrub/_clean_null_strings.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,19 @@ class CleanNullStrings(SingleColumnTransformer):
231231
...
232232
skrub._single_column_transformer.RejectColumn: Column 's' does not contain strings.
233233
"""
234+
def __init__(
235+
self,
236+
null_strings=None,
237+
):
238+
self.null_strings = null_strings
234239

235240
def fit_transform(self, column, y=None):
236241
del y
242+
self.null_list_ = STR_NA_VALUES
243+
if self.null_strings is not None:
244+
if not (isinstance(self.null_strings, str) or (isinstance(self.null_strings, list) and all(isinstance(i, str) for i in self.null_strings))):
245+
raise TypeError("Expected either a string or a list of strictly strings.")
246+
self.null_list_ += self.null_strings
237247
if not (sbd.is_pandas_object(column) or sbd.is_string(column)):
238248
raise RejectColumn(f"Column {sbd.name(column)!r} does not contain strings.")
239249
return self.transform(column)
@@ -242,5 +252,5 @@ def transform(self, column):
242252
if not (sbd.is_pandas_object(column) or sbd.is_string(column)):
243253
return column
244254
column = _trim_whitespace_only(column)
245-
column = sbd.replace(column, STR_NA_VALUES, sbd.null_value_for(column))
255+
column = sbd.replace(column, self.null_list_, sbd.null_value_for(column))
246256
return column

skrub/_table_vectorizer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def _check_transformer(transformer):
115115
def _get_preprocessors(
116116
*,
117117
cols,
118+
null_strings,
118119
drop_null_fraction,
119120
drop_if_unique,
120121
drop_if_constant,
@@ -125,7 +126,9 @@ def _get_preprocessors(
125126
):
126127
steps = [CheckInputDataFrame()]
127128
transformers = [
128-
CleanNullStrings(),
129+
CleanNullStrings(null_strings=null_strings,
130+
131+
),
129132
DropUninformative(
130133
drop_null_fraction=drop_null_fraction,
131134
drop_if_constant=drop_if_constant,
@@ -321,6 +324,7 @@ class Cleaner(TransformerMixin, BaseEstimator):
321324

322325
def __init__(
323326
self,
327+
null_strings=None,
324328
drop_null_fraction=1.0,
325329
drop_if_constant=False,
326330
drop_if_unique=False,
@@ -329,6 +333,7 @@ def __init__(
329333
cast_to_str=False,
330334
n_jobs=1,
331335
):
336+
self.null_strings = null_strings
332337
self.drop_null_fraction = drop_null_fraction
333338
self.drop_if_constant = drop_if_constant
334339
self.drop_if_unique = drop_if_unique
@@ -365,6 +370,7 @@ def fit_transform(self, X, y=None):
365370

366371
all_steps = _get_preprocessors(
367372
cols=s.all(),
373+
null_strings=self.null_strings,
368374
drop_null_fraction=self.drop_null_fraction,
369375
drop_if_constant=self.drop_if_constant,
370376
drop_if_unique=self.drop_if_unique,

0 commit comments

Comments
 (0)