Skip to content

31290 #31537

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

Closed
wants to merge 3 commits into from
Closed

31290 #31537

wants to merge 3 commits into from

Conversation

cboseak
Copy link

@cboseak cboseak commented Jun 12, 2025

Reference Issues/PRs

Fixes #31290


What does this implement/fix? Explain your changes.

This PR fixes an issue where _safe_indexing triggers a SettingWithCopyWarning when used with a slice on pandas DataFrames.

The problem:

When _safe_indexing is used with a slice (e.g., slice(0, 2)) on a pandas DataFrame, it returns a view rather than a copy. If the user then modifies this view, pandas raises a SettingWithCopyWarning because it's unclear whether the modification should affect the original DataFrame.

The fix:

I modified the _pandas_indexing function in sklearn/utils/_indexing.py to explicitly create a copy when a slice is used:

# When using a slice, make a copy to avoid SettingWithCopyWarning
if isinstance(key, slice):
    result = result.copy()

This ensures that when _safe_indexing is used with a slice, it always returns a copy of the data, not a view. This prevents the SettingWithCopyWarning from being raised when the result is modified.

This change maintains the existing behavior of _safe_indexing (it still returns the same data), but ensures that the returned object is always a copy when using slices, which is consistent with the behavior when using other types of indexers.


Any other comments?

  • I debated on whether to use .copy(deep=True) or .copy() but decided on .copy() because of the performance overhead of deep copying. The trade off is that deep copy would be safer but at the cost of performance.
  • I saw a mention of this being a private function so the fix may not be needed but still decided to submit the PR since its a defensive programming move and shouldn't cause any issues
  • I saw another guy comment on this issue (30+ days ago) and debated on whether to work on it (didn't want to step on toes) but didn't see any activity after that.

Copy link

✔️ Linting Passed

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

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

@lucyleeow
Copy link
Member

lucyleeow commented Jun 13, 2025

@cboseak I'm going to close this just because I don't think we have yet decided on the best way forward for #31537

Note for future, please follow these guidelines for contributing: https://scikit-learn.org/dev/developers/contributing.html#pull-request-checklist

@lucyleeow lucyleeow closed this Jun 13, 2025
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.

_safe_indexing triggers SettingWithCopyWarning when used with slice
2 participants