nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: dirty-cat/dirty_cat
Pull request #475 (2023-01-24 13:26:58)

More informative error when n_samples < n_components in GapEncoder
Solves #474

Previously, an error was raised from Kmeans, and talked about `n_clusters` instead of `n_components`. 

LeoGrin: I think this deserves a "No changelog needed", because it's a very small change.

Patch:
diff --git a/dirty_cat/_gap_encoder.py b/dirty_cat/_gap_encoder.py
index 4aa3d17..014d6bd 100644
--- a/dirty_cat/_gap_encoder.py
+++ b/dirty_cat/_gap_encoder.py
@@ -777,6 +777,11 @@ class GapEncoder(BaseEstimator, TransformerMixin):
             Fitted :class:`~dirty_cat.GapEncoder` instance (self).
         """
 
+        # Check that n_samples >= n_components
+        if len(X) < self.n_components:
+            raise ValueError(
+                f"n_samples={len(X)} should be >= n_components={self.n_components}. "
+            )
         # Copy parameter rho
         self.rho_ = self.rho
         # If X is a dataframe, store its column names


Test patch:
diff --git a/dirty_cat/tests/test_gap_encoder.py b/dirty_cat/tests/test_gap_encoder.py
index e39e798..77c2c1f 100644
--- a/dirty_cat/tests/test_gap_encoder.py
+++ b/dirty_cat/tests/test_gap_encoder.py
@@ -197,13 +197,22 @@ def test_missing_values(missing: str) -> None:
         ):
             enc.fit_transform(observations)
 
+
 def test_check_fitted_gap_encoder():
     """Test that calling transform before fit raises an error"""
     X = np.array([["alice"], ["bob"]])
     enc = GapEncoder(n_components=2, random_state=42)
     with pytest.raises(NotFittedError):
         enc.transform(X)
-    
+
     # Check that it works after fit
     enc.fit(X)
     enc.transform(X)
+
+
+def test_small_sample():
+    """Test that having n_samples < n_components raises an error"""
+    X = np.array([["alice"], ["bob"]])
+    enc = GapEncoder(n_components=3, random_state=42)
+    with pytest.raises(ValueError, match="should be >= n_components"):
+        enc.fit_transform(X)