Skip to content

Commit e9402fa

Browse files
ENH improve error message for string indexing on axis=0 (scikit-learn#31494)
Co-authored-by: Jérémie du Boisberranger <jeremie@probabl.ai>
1 parent 54751c5 commit e9402fa

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

sklearn/utils/_indexing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ def _safe_indexing(X, indices, *, axis=0):
302302
indices_dtype = _determine_key_type(indices)
303303

304304
if axis == 0 and indices_dtype == "str":
305-
raise ValueError("String indexing is not supported with 'axis=0'")
305+
raise ValueError(
306+
f"String indexing (indices={indices}) is not supported with 'axis=0'. "
307+
"Did you mean to use axis=1 for column selection?"
308+
)
306309

307310
if axis == 1 and isinstance(X, list):
308311
raise ValueError("axis=1 is not supported for lists")

sklearn/utils/tests/test_indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def test_safe_indexing_1d_array_error(X_constructor):
362362
def test_safe_indexing_container_axis_0_unsupported_type():
363363
indices = ["col_1", "col_2"]
364364
array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
365-
err_msg = "String indexing is not supported with 'axis=0'"
365+
err_msg = r"String indexing.*is not supported with 'axis=0'"
366366
with pytest.raises(ValueError, match=err_msg):
367367
_safe_indexing(array, indices, axis=0)
368368

0 commit comments

Comments
 (0)