Skip to content

FIX change writeability only if not already writeable #29103

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/whats_new/v1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ Changelog
grids that have heterogeneous parameter values.
:pr:`29078` by :user:`Loïc Estève <lesteve>`

:mod:`sklearn.utils`
....................

- |Fix| Fix a regression in :func:`utils.validation.check_array` that may have caused
it to raise an unexpected error about writeability on pandas dataframes.
:pr:`19103` by :user:`Jérémie du Boisberranger <jeremiedbb>`.

.. _changes_1_5:

Expand Down
6 changes: 5 additions & 1 deletion sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,11 @@ def is_sparse(dtype):
# See https://pandas.pydata.org/docs/dev/user_guide/copy_on_write.html#read-only-numpy-arrays
# for more details about pandas copy-on-write mechanism, that is enabled by
# default in pandas 3.0.0.dev.
if _is_pandas_df_or_series(array_orig) and hasattr(array, "flags"):
if (
_is_pandas_df_or_series(array_orig)
and hasattr(array, "flags")
and not array.flags.writeable
Copy link
Contributor

Choose a reason for hiding this comment

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

If the array is read from a read only buffer such that its writeable is expected to be False as we discussed, won't this be an issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is and I opened #29018 to treat this case correctly for arrays and dataframes. See in particular this #29018 (comment) where I show that our current code fails for a very simple use case.

Copy link
Contributor

@OmarManzoor OmarManzoor Jun 4, 2024

Choose a reason for hiding this comment

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

Can we try this or will this result in other issues or have unnecessary memory problems?

try:
    array.flags.writeable = True
except ValueError:
    array = array.copy()
    array.flags.writeable = True

Copy link
Member Author

Choose a reason for hiding this comment

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

I ruled this out because I think that we should not change the writeability of the user's input data to prevent catastrophic mistakes. Let's discuss this kind of questions in #29018.

Copy link
Contributor

@OmarManzoor OmarManzoor Jun 4, 2024

Choose a reason for hiding this comment

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

Okay I had a look at the other PR and some of the associated discussion. Since the actual solution is being implemented there, I think this may be okay as a temporary workaround. Maybe we could raise an exception with a more informative message for the case where the array is from a read only buffer?

):
array.flags.writeable = True

return array
Expand Down
Loading