Repository: dirty-cat/dirty_cat

Issue:
Better error message when SuperVectorizer's transform method is called before fit
Scikit-learn has a mechanism to check that an estimator is fitted, and if not raise a good error message. We should use it.

Discussion:

Patch:
diff --git a/dirty_cat/super_vectorizer.py b/dirty_cat/super_vectorizer.py
index 84b39c9..dcbf1ab 100644
--- a/dirty_cat/super_vectorizer.py
+++ b/dirty_cat/super_vectorizer.py
@@ -171,8 +171,6 @@ class SuperVectorizer(ColumnTransformer):
         self.transformer_weights = transformer_weights
         self.verbose = verbose
 
-        self.columns_ = []
-
     def _auto_cast_array(self, X):
         """
         Takes an array and tries to convert its columns to the best possible


Test patch:
diff --git a/dirty_cat/test/test_super_vectorizer.py b/dirty_cat/test/test_super_vectorizer.py
index 2f34d8e..5cb10c5 100644
--- a/dirty_cat/test/test_super_vectorizer.py
+++ b/dirty_cat/test/test_super_vectorizer.py
@@ -3,6 +3,8 @@ import sklearn
 import pandas as pd
 
 from sklearn.preprocessing import StandardScaler
+from sklearn.utils.validation import check_is_fitted
+from sklearn.exceptions import NotFittedError
 
 from distutils.version import LooseVersion
 
@@ -130,6 +132,18 @@ def test_get_feature_names():
     assert vectorizer_w_drop.get_feature_names() == expected_feature_names_drop
 
 
+def test_fit():
+    # Simply checks sklearn's `check_is_fitted` function raises an error if
+    # the SuperVectorizer is instantiated but not fitted.
+    # See GH#193
+    sup_vec = SuperVectorizer()
+    with pytest.raises(NotFittedError):
+        if LooseVersion(sklearn.__version__) >= LooseVersion('0.22'):
+            assert check_is_fitted(sup_vec)
+        else:
+            assert check_is_fitted(sup_vec, attributes=dir(sup_vec))
+
+
 if __name__ == '__main__':
     print('start test_super_vectorizer')
     test_super_vectorizer()
@@ -137,5 +151,8 @@ if __name__ == '__main__':
     print('start test_get_feature_names')
     test_get_feature_names()
     print('test_get_feature_names passed')
+    print('start test_fit')
+    test_fit()
+    print('test_fit passed')
 
     print('Done')