Skip to content

Commit

Permalink
add additional validation on type equivalence for pandastypeselector …
Browse files Browse the repository at this point in the history
…in fit and transform (#220)
  • Loading branch information
MBrouns authored and koaning committed Oct 18, 2019
1 parent 4c26a12 commit c68fbe7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sklego/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def fit(self, X, y=None):
"""
self._check_X_for_type(X)
self.type_columns_ = list(X.select_dtypes(include=self.include, exclude=self.exclude))

self.X_dtypes_ = X.dtypes
if len(self.type_columns_) == 0:
raise ValueError(f'Provided type(s) results in empty dateframe')

Expand All @@ -96,10 +96,14 @@ def transform(self, X):
:param X: pandas dataframe to select dtypes for
"""
check_is_fitted(self, 'type_columns_')
check_is_fitted(self, ['type_columns_', 'X_dtypes_'])
if (self.X_dtypes_ != X.dtypes).any():
raise ValueError(f'Column dtypes were not equal during fit and transform. Fit types: \n'
f'{self.X_dtypes_}\n'
f'transform: \n'
f'{X.dtypes}')

self._check_X_for_type(X)

transformed_df = X.select_dtypes(include=self.include, exclude=self.exclude)

if set(list(transformed_df)) != set(self.type_columns_):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_preprocessing/test_pandastypeselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ def test_get_params_np(include, exclude):
}


def test_value_error_differrent_dtyes():
fit_df = pd.DataFrame({
'a': [1, 2, 3],
'b': [4, 5, 6]
})
transform_df = pd.DataFrame({
'a': [4, 5, 6],
'b': ['4', '5', '6']
})
transformer = PandasTypeSelector(exclude=['category']).fit(fit_df)

with pytest.raises(ValueError):
transformer.transform(transform_df)


def test_value_error_empty(random_xy_dataset_regr):
transformer = PandasTypeSelector(exclude=['number'])
X, y = random_xy_dataset_regr
Expand Down

0 comments on commit c68fbe7

Please sign in to comment.