Skip to content

Commit

Permalink
add FutureWarning to __array__
Browse files Browse the repository at this point in the history
  • Loading branch information
KevsterAmp committed Nov 22, 2024
1 parent 112c2e9 commit 2eb106c
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 0 deletions.
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

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(
"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

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/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

0 comments on commit 2eb106c

Please sign in to comment.