Skip to content
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

[MRG+1] Fix safe_indexing with read-only indices #9507

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions sklearn/utils/__init__.py
Expand Up @@ -142,6 +142,8 @@ def safe_indexing(X, indices):
not supported.
"""
if hasattr(X, "iloc"):
# Work-around for indexing with read-only indices in pandas
indices = indices if indices.flags.writeable else indices.copy()
# Pandas Dataframes and Series
try:
return X.iloc[indices]
Expand Down
13 changes: 9 additions & 4 deletions sklearn/utils/tests/test_utils.py
@@ -1,4 +1,4 @@
from itertools import chain
from itertools import chain, product
import warnings

import numpy as np
Expand Down Expand Up @@ -200,10 +200,15 @@ def test_safe_indexing_pandas():
# this happens in joblib memmapping
X.setflags(write=False)
X_df_readonly = pd.DataFrame(X)
with warnings.catch_warnings(record=True):
X_df_ro_indexed = safe_indexing(X_df_readonly, inds)
inds_readonly = inds.copy()
inds_readonly.setflags(write=False)

assert_array_equal(np.array(X_df_ro_indexed), X_indexed)
for this_df, this_inds in product([X_df, X_df_readonly],
[inds, inds_readonly]):
with warnings.catch_warnings(record=True):
X_df_indexed = safe_indexing(this_df, this_inds)

assert_array_equal(np.array(X_df_indexed), X_indexed)


def test_safe_indexing_mock_pandas():
Expand Down