Skip to content

Commit 80f923e

Browse files
ENH num_features for a 1d collection of dicts is undefined (#19740)
Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
1 parent 4dfdfb4 commit 80f923e

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

sklearn/utils/tests/test_validation.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,9 +1345,10 @@ def test_num_features(constructor_name):
13451345
[1, 2, 3],
13461346
["a", "b", "c"],
13471347
[False, True, False],
1348-
[1.0, 3.4, 4.0]
1348+
[1.0, 3.4, 4.0],
1349+
[{"a": 1}, {"b": 2}, {"c": 3}],
13491350
],
1350-
ids=["int", "str", "bool", "float"]
1351+
ids=["int", "str", "bool", "float", "dict"]
13511352
)
13521353
@pytest.mark.parametrize("constructor_name", [
13531354
"list", "tuple", "array", "series"
@@ -1368,6 +1369,8 @@ def test_num_features_errors_1d_containers(X, constructor_name):
13681369
message += " with shape (3,)"
13691370
elif isinstance(X[0], str):
13701371
message += " where the samples are of type str"
1372+
elif isinstance(X[0], dict):
1373+
message += " where the samples are of type dict"
13711374
with pytest.raises(TypeError, match=re.escape(message)):
13721375
_num_features(X)
13731376

sklearn/utils/validation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ def _num_features(X):
226226

227227
first_sample = X[0]
228228

229-
# Do not consider an array-like of strings to be a 2D array
230-
if isinstance(first_sample, (str, bytes)):
229+
# Do not consider an array-like of strings or dicts to be a 2D array
230+
if isinstance(first_sample, (str, bytes, dict)):
231231
message += (f" where the samples are of type "
232232
f"{type(first_sample).__qualname__}")
233233
raise TypeError(message)

0 commit comments

Comments
 (0)