Skip to content

DEPR: Raise FutureWarning about raising an error in __array__ when copy=False cannot be honored #60395

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
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,16 @@ def __array__(
) -> np.ndarray:
"""Correctly construct numpy arrays when passed to `np.asarray()`."""
if copy is False:
import warnings

from pandas.util._exceptions import find_stack_level
Copy link
Member

Choose a reason for hiding this comment

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

You can move those warnings to the top of the file


warnings.warn(
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
"raise an error when a zero-copy numpy array is not possible",
FutureWarning,
stacklevel=find_stack_level(),
)
# TODO: By using `zero_copy_only` it may be possible to implement this
raise ValueError(
Copy link
Member

Choose a reason for hiding this comment

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

This error should then be removed (we are adding the warning instead of the error, to warn people it will error in the future).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha, I initially thought we would raise both an error and a warning.

"Unable to avoid copy while creating an array as requested."
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,16 @@ def __array__(
array(['a', 'b'], dtype=object)
"""
if copy is False:
import warnings

from pandas.util._exceptions import find_stack_level
Copy link
Member

Choose a reason for hiding this comment

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

Same here about the imports (and similar for next cases)


warnings.warn(
"Numpy>=2.0 changed copy keyword's behavior, making copy=False "
"raise an error when a zero-copy numpy array is not possible",
FutureWarning,
stacklevel=find_stack_level(),
)
raise ValueError(
Copy link
Member

Choose a reason for hiding this comment

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

And same for the errors

"Unable to avoid copy while creating an array as requested."
)
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ def __array__(
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
if is_object_dtype(dtype):
if copy is False:
import warnings

from pandas.util._exceptions import find_stack_level

warnings.warn(
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
"raise an error when a zero-copy numpy array is not possible",
FutureWarning,
stacklevel=find_stack_level(),
)
raise ValueError(
"Unable to avoid copy while creating an array as requested."
)
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,16 @@ def __array__(
objects (with dtype='object')
"""
if copy is False:
import warnings

from pandas.util._exceptions import find_stack_level

warnings.warn(
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
"raise an error when a zero-copy numpy array is not possible",
FutureWarning,
stacklevel=find_stack_level(),
)
raise ValueError(
"Unable to avoid copy while creating an array as requested."
)
Expand Down
11 changes: 11 additions & 0 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,17 @@ def __array__(
if not self._hasna:
# special case, here we can simply return the underlying data
return np.array(self._data, dtype=dtype, copy=copy)

import warnings

from pandas.util._exceptions import find_stack_level

warnings.warn(
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
"raise an error when a zero-copy numpy array is not possible",
FutureWarning,
stacklevel=find_stack_level(),
)
raise ValueError(
"Unable to avoid copy while creating an array as requested."
)
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,16 @@ def __array__(
return np.array(self.asi8, dtype=dtype)

if copy is False:
import warnings

from pandas.util._exceptions import find_stack_level

warnings.warn(
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
"raise an error when a zero-copy numpy array is not possible",
FutureWarning,
stacklevel=find_stack_level(),
)
raise ValueError(
"Unable to avoid copy while creating an array as requested."
)
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,16 @@ def __array__(
return self.sp_values

if copy is False:
import warnings

from pandas.util._exceptions import find_stack_level

warnings.warn(
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
"raise an error when a zero-copy numpy array is not possible",
FutureWarning,
stacklevel=find_stack_level(),
)
raise ValueError(
"Unable to avoid copy while creating an array as requested."
)
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,16 @@ def __array__(
if copy is False and not self._mgr.is_single_block and not self.empty:
# check this manually, otherwise ._values will already return a copy
# and np.array(values, copy=False) will not raise an error
import warnings

from pandas.util._exceptions import find_stack_level

warnings.warn(
"Numpy>=2.0 changed the copy keyword behavior, making copy=False"
"raise an error when a zero-copy numpy array is not possible.",
FutureWarning,
stacklevel=find_stack_level(),
)
raise ValueError(
"Unable to avoid copy while creating an array as requested."
)
Expand Down
10 changes: 10 additions & 0 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,16 @@ def __array__(self, dtype=None, copy=None) -> np.ndarray:
"""the array interface, return my values"""
if copy is False:
# self.values is always a newly construct array, so raise.
import warnings

from pandas.util._exceptions import find_stack_level

warnings.warn(
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
"raise an error when a zero-copy numpy array is not possible",
FutureWarning,
stacklevel=find_stack_level(),
)
raise ValueError(
"Unable to avoid copy while creating an array as requested."
)
Expand Down