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

Merged

Conversation

Roshangoli
Copy link
Contributor

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 Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: 5e235e6. Link to the linter CI: here

Copy link
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should enable pre-commit hooks to fix your linting issues in your commits.

Comment on lines 293 to 295
raise ValueError(
f"String indexing (indices={indices}) is not supported with 'axis=0'. Did you mean to use axis=1 for column selection?"
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValueError(
f"String indexing (indices={indices}) is not supported with 'axis=0'. Did you mean to use axis=1 for column selection?"
)
raise ValueError(
f"String indexing (indices={indices}) is not supported with 'axis=0'. "
"Did you mean to use axis=1 for column selection?"
)

Copy link
Member

@jeremiedbb jeremiedbb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Roshangoli

@jeremiedbb jeremiedbb enabled auto-merge (squash) July 2, 2025 16:43
@jeremiedbb jeremiedbb merged commit e9402fa into scikit-learn:main Jul 2, 2025
39 of 40 checks passed
@jeremiedbb jeremiedbb mentioned this pull request Jul 15, 2025
13 tasks
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.

3 participants