forked from skrub-data/skrub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DropNullColumn transformer to remove columns that contain only …
…nulls (skrub-data#1115)
- Loading branch information
Showing
7 changed files
with
225 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# drop columns that contain all null values | ||
from sklearn.utils.validation import check_is_fitted | ||
|
||
from . import _dataframe as sbd | ||
from ._on_each_column import SingleColumnTransformer | ||
|
||
__all__ = ["DropColumnIfNull"] | ||
|
||
|
||
class DropColumnIfNull(SingleColumnTransformer): | ||
"""Drop a single column if it contains only Null, NaN, or a mixture of null | ||
values. If at least one non-null value is found, the column is kept.""" | ||
|
||
def fit_transform(self, column, y=None): | ||
"""Fit the encoder and transform a column. | ||
Parameters | ||
---------- | ||
column : Pandas or Polars series. The input column to check. | ||
y : None. Ignored. | ||
Returns | ||
------- | ||
The input column, or an empty list if the column contains only null values. | ||
""" | ||
del y | ||
|
||
self.drop_ = sbd.is_all_null(column) | ||
|
||
return self.transform(column) | ||
|
||
def transform(self, column): | ||
"""Transform a column. | ||
Parameters: | ||
----------- | ||
column : Pandas or Polars series. The input column to check. | ||
Returns | ||
------- | ||
column | ||
The input column, or an empty list if the column contains only null values. | ||
""" | ||
check_is_fitted(self) | ||
|
||
if self.drop_: | ||
return [] | ||
return column |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from skrub import _dataframe as sbd | ||
from skrub._drop_column_if_null import DropColumnIfNull | ||
|
||
|
||
@pytest.fixture | ||
def drop_null_table(df_module): | ||
return df_module.make_dataframe( | ||
{ | ||
"idx": [ | ||
1, | ||
2, | ||
3, | ||
], | ||
"value_nan": [ | ||
np.nan, | ||
np.nan, | ||
np.nan, | ||
], | ||
"value_null": [ | ||
None, | ||
None, | ||
None, | ||
], | ||
"value_almost_nan": [ | ||
2.5, | ||
np.nan, | ||
np.nan, | ||
], | ||
"value_almost_null": [ | ||
"almost", | ||
None, | ||
None, | ||
], | ||
"mixed_null": [None, np.nan, None], | ||
} | ||
) | ||
|
||
|
||
def test_single_column(drop_null_table, df_module): | ||
"""Check that null columns are dropped and non-null columns are kept.""" | ||
dn = DropColumnIfNull() | ||
assert dn.fit_transform(sbd.col(drop_null_table, "value_nan")) == [] | ||
assert dn.fit_transform(sbd.col(drop_null_table, "value_null")) == [] | ||
assert dn.fit_transform(sbd.col(drop_null_table, "mixed_null")) == [] | ||
|
||
df_module.assert_column_equal( | ||
dn.fit_transform(sbd.col(drop_null_table, "idx")), | ||
df_module.make_column("idx", [1, 2, 3]), | ||
) | ||
|
||
df_module.assert_column_equal( | ||
dn.fit_transform(sbd.col(drop_null_table, "value_almost_nan")), | ||
df_module.make_column("value_almost_nan", [2.5, np.nan, np.nan]), | ||
) | ||
|
||
df_module.assert_column_equal( | ||
dn.fit_transform(sbd.col(drop_null_table, "value_almost_null")), | ||
df_module.make_column("value_almost_null", ["almost", None, None]), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters