Skip to content

FIX _safe_indexing: improve error message for string indexing on axis=0 #31494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Roshangoli
Copy link

This PR makes the ValueError from sklearn.utils._indexing._safe_indexing more informative
when a string index is passed with axis=0. The new message shows the actual value of
indices and suggests using axis=1 for column selection.

  • Modified sklearn/utils/_indexing.py to raise:
    ValueError(f"String indexing (indices={indices}) is not supported with 'axis=0'. "
    "Did you mean to use axis=1 for column selection?")
  • Updated the test in sklearn/utils/tests/test_indexing.py to match this new message
    (using a regex).

All existing indexing tests now pass. The only local test failure is test_openmp_parallelism_enabled,
which can be ignored on macOS; CI will run on Linux with OpenMP support.

Copy link

github-actions bot commented Jun 6, 2025

❌ Linting issues

This PR is introducing linting issues. Here's a summary of the issues. Note that you can avoid having linting issues by enabling pre-commit hooks. Instructions to enable them can be found here.

You can see the details of the linting issues under the lint job here


ruff check

ruff detected issues. Please run ruff check --fix --output-format=full locally, fix the remaining issues, and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.11.7.


sklearn/utils/_indexing.py:294:89: E501 Line too long (131 > 88)
    |
292 |     if axis == 0 and indices_dtype == "str":
293 |           raise ValueError(
294 |             f"String indexing (indices={indices}) is not supported with 'axis=0'. Did you mean to use axis=1 for column selection?"
    |                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ E501
295 |         )
    |

Found 1 error.

ruff format

ruff detected issues. Please run ruff format locally and push the changes. Here you can see the detected issues. Note that the installed ruff version is ruff=0.11.7.


--- sklearn/utils/_indexing.py
+++ sklearn/utils/_indexing.py
@@ -290,7 +290,7 @@
     indices_dtype = _determine_key_type(indices)
 
     if axis == 0 and indices_dtype == "str":
-          raise ValueError(
+        raise ValueError(
             f"String indexing (indices={indices}) is not supported with 'axis=0'. Did you mean to use axis=1 for column selection?"
         )
 

1 file would be reformatted, 923 files already formatted

Generated for commit: cfd4b0e. Link to the linter CI: here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant