Skip to content

Commit 2eb106c

Browse files
committed
add FutureWarning to __array__
1 parent 112c2e9 commit 2eb106c

File tree

9 files changed

+91
-0
lines changed

9 files changed

+91
-0
lines changed

pandas/core/arrays/arrow/array.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,16 @@ def __array__(
663663
) -> np.ndarray:
664664
"""Correctly construct numpy arrays when passed to `np.asarray()`."""
665665
if copy is False:
666+
import warnings
667+
668+
from pandas.util._exceptions import find_stack_level
669+
670+
warnings.warn(
671+
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
672+
"raise an error when a zero-copy numpy array is not possible",
673+
FutureWarning,
674+
stacklevel=find_stack_level(),
675+
)
666676
# TODO: By using `zero_copy_only` it may be possible to implement this
667677
raise ValueError(
668678
"Unable to avoid copy while creating an array as requested."

pandas/core/arrays/categorical.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,16 @@ def __array__(
16721672
array(['a', 'b'], dtype=object)
16731673
"""
16741674
if copy is False:
1675+
import warnings
1676+
1677+
from pandas.util._exceptions import find_stack_level
1678+
1679+
warnings.warn(
1680+
"Numpy>=2.0 changed copy keyword's behavior, making copy=False "
1681+
"raise an error when a zero-copy numpy array is not possible",
1682+
FutureWarning,
1683+
stacklevel=find_stack_level(),
1684+
)
16751685
raise ValueError(
16761686
"Unable to avoid copy while creating an array as requested."
16771687
)

pandas/core/arrays/datetimelike.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,16 @@ def __array__(
359359
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
360360
if is_object_dtype(dtype):
361361
if copy is False:
362+
import warnings
363+
364+
from pandas.util._exceptions import find_stack_level
365+
366+
warnings.warn(
367+
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
368+
"raise an error when a zero-copy numpy array is not possible",
369+
FutureWarning,
370+
stacklevel=find_stack_level(),
371+
)
362372
raise ValueError(
363373
"Unable to avoid copy while creating an array as requested."
364374
)

pandas/core/arrays/interval.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,16 @@ def __array__(
15751575
objects (with dtype='object')
15761576
"""
15771577
if copy is False:
1578+
import warnings
1579+
1580+
from pandas.util._exceptions import find_stack_level
1581+
1582+
warnings.warn(
1583+
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
1584+
"raise an error when a zero-copy numpy array is not possible",
1585+
FutureWarning,
1586+
stacklevel=find_stack_level(),
1587+
)
15781588
raise ValueError(
15791589
"Unable to avoid copy while creating an array as requested."
15801590
)

pandas/core/arrays/masked.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,17 @@ def __array__(
604604
if not self._hasna:
605605
# special case, here we can simply return the underlying data
606606
return np.array(self._data, dtype=dtype, copy=copy)
607+
608+
import warnings
609+
610+
from pandas.util._exceptions import find_stack_level
611+
612+
warnings.warn(
613+
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
614+
"raise an error when a zero-copy numpy array is not possible",
615+
FutureWarning,
616+
stacklevel=find_stack_level(),
617+
)
607618
raise ValueError(
608619
"Unable to avoid copy while creating an array as requested."
609620
)

pandas/core/arrays/period.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ def __array__(
415415
return np.array(self.asi8, dtype=dtype)
416416

417417
if copy is False:
418+
import warnings
419+
420+
from pandas.util._exceptions import find_stack_level
421+
422+
warnings.warn(
423+
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
424+
"raise an error when a zero-copy numpy array is not possible",
425+
FutureWarning,
426+
stacklevel=find_stack_level(),
427+
)
418428
raise ValueError(
419429
"Unable to avoid copy while creating an array as requested."
420430
)

pandas/core/arrays/sparse/array.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,16 @@ def __array__(
562562
return self.sp_values
563563

564564
if copy is False:
565+
import warnings
566+
567+
from pandas.util._exceptions import find_stack_level
568+
569+
warnings.warn(
570+
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
571+
"raise an error when a zero-copy numpy array is not possible",
572+
FutureWarning,
573+
stacklevel=find_stack_level(),
574+
)
565575
raise ValueError(
566576
"Unable to avoid copy while creating an array as requested."
567577
)

pandas/core/generic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,6 +2152,16 @@ def __array__(
21522152
if copy is False and not self._mgr.is_single_block and not self.empty:
21532153
# check this manually, otherwise ._values will already return a copy
21542154
# and np.array(values, copy=False) will not raise an error
2155+
import warnings
2156+
2157+
from pandas.util._exceptions import find_stack_level
2158+
2159+
warnings.warn(
2160+
"Numpy>=2.0 changed the copy keyword behavior, making copy=False"
2161+
"raise an error when a zero-copy numpy array is not possible.",
2162+
FutureWarning,
2163+
stacklevel=find_stack_level(),
2164+
)
21552165
raise ValueError(
21562166
"Unable to avoid copy while creating an array as requested."
21572167
)

pandas/core/indexes/multi.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,16 @@ def __array__(self, dtype=None, copy=None) -> np.ndarray:
13141314
"""the array interface, return my values"""
13151315
if copy is False:
13161316
# self.values is always a newly construct array, so raise.
1317+
import warnings
1318+
1319+
from pandas.util._exceptions import find_stack_level
1320+
1321+
warnings.warn(
1322+
"Numpy>=2.0 changed copy keyword's behavior, making copy=False"
1323+
"raise an error when a zero-copy numpy array is not possible",
1324+
FutureWarning,
1325+
stacklevel=find_stack_level(),
1326+
)
13171327
raise ValueError(
13181328
"Unable to avoid copy while creating an array as requested."
13191329
)

0 commit comments

Comments
 (0)