Skip to content

Commit f0bec10

Browse files
committed
move imports on top, add better warning message, remove error raise
1 parent 2eb106c commit f0bec10

File tree

9 files changed

+52
-83
lines changed

9 files changed

+52
-83
lines changed

pandas/core/arrays/arrow/array.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
cast,
1313
)
1414
import unicodedata
15+
import warnings
1516

1617
import numpy as np
1718

@@ -28,6 +29,7 @@
2829
pa_version_under13p0,
2930
)
3031
from pandas.util._decorators import doc
32+
from pandas.util._exceptions import find_stack_level
3133
from pandas.util._validators import validate_fillna_kwargs
3234

3335
from pandas.core.dtypes.cast import (
@@ -663,20 +665,15 @@ def __array__(
663665
) -> np.ndarray:
664666
"""Correctly construct numpy arrays when passed to `np.asarray()`."""
665667
if copy is False:
666-
import warnings
667-
668-
from pandas.util._exceptions import find_stack_level
669-
670668
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",
669+
"Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
670+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
671+
"is not possible, Pandas will follow this behavior starting with "
672+
"version 3.0. This conversion to NumPy requires a copy, but "
673+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead.",
673674
FutureWarning,
674675
stacklevel=find_stack_level(),
675676
)
676-
# TODO: By using `zero_copy_only` it may be possible to implement this
677-
raise ValueError(
678-
"Unable to avoid copy while creating an array as requested."
679-
)
680677
elif copy is None:
681678
# `to_numpy(copy=False)` has the meaning of NumPy `copy=None`.
682679
copy = False

pandas/core/arrays/categorical.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,19 +1672,15 @@ 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-
16791675
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",
1676+
"Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
1677+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
1678+
"is not possible, Pandas will follow this behavior starting with "
1679+
"version 3.0. This conversion to NumPy requires a copy, but "
1680+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead.",
16821681
FutureWarning,
16831682
stacklevel=find_stack_level(),
16841683
)
1685-
raise ValueError(
1686-
"Unable to avoid copy while creating an array as requested."
1687-
)
16881684

16891685
ret = take_nd(self.categories._values, self._codes)
16901686
# When we're a Categorical[ExtensionArray], like Interval,

pandas/core/arrays/datetimelike.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,17 @@ 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-
366362
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",
363+
"Starting on NumPy 2.0, the behavior of the 'copy' keyword has "
364+
"changed and passing 'copy=False' raises an error when a zero-copy "
365+
"NumPy array is not possible, Pandas will follow this behavior "
366+
"starting with version 3.0. This conversion to NumPy requires a "
367+
"copy, but 'copy=False' was passed. Consider using "
368+
"'np.asarray(..)' instead.",
369369
FutureWarning,
370370
stacklevel=find_stack_level(),
371371
)
372-
raise ValueError(
373-
"Unable to avoid copy while creating an array as requested."
374-
)
372+
375373
return np.array(list(self), dtype=object)
376374

377375
if copy is True:

pandas/core/arrays/interval.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from pandas.compat.numpy import function as nv
4343
from pandas.errors import IntCastingNaNError
4444
from pandas.util._decorators import Appender
45+
from pandas.util._exceptions import find_stack_level
4546

4647
from pandas.core.dtypes.cast import (
4748
LossySetitemError,
@@ -1575,19 +1576,15 @@ def __array__(
15751576
objects (with dtype='object')
15761577
"""
15771578
if copy is False:
1578-
import warnings
1579-
1580-
from pandas.util._exceptions import find_stack_level
1581-
15821579
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",
1580+
"Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
1581+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
1582+
"is not possible, Pandas will follow this behavior starting with "
1583+
"version 3.0. This conversion to NumPy requires a copy, but "
1584+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead.",
15851585
FutureWarning,
15861586
stacklevel=find_stack_level(),
15871587
)
1588-
raise ValueError(
1589-
"Unable to avoid copy while creating an array as requested."
1590-
)
15911588

15921589
left = self._left
15931590
right = self._right

pandas/core/arrays/masked.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
)
3939
from pandas.errors import AbstractMethodError
4040
from pandas.util._decorators import doc
41+
from pandas.util._exceptions import find_stack_level
4142
from pandas.util._validators import validate_fillna_kwargs
4243

4344
from pandas.core.dtypes.base import ExtensionDtype
@@ -605,19 +606,15 @@ def __array__(
605606
# special case, here we can simply return the underlying data
606607
return np.array(self._data, dtype=dtype, copy=copy)
607608

608-
import warnings
609-
610-
from pandas.util._exceptions import find_stack_level
611-
612609
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",
610+
"Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
611+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
612+
"is not possible, Pandas will follow this behavior starting with "
613+
"version 3.0. This conversion to NumPy requires a copy, but "
614+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead.",
615615
FutureWarning,
616616
stacklevel=find_stack_level(),
617617
)
618-
raise ValueError(
619-
"Unable to avoid copy while creating an array as requested."
620-
)
621618

622619
if copy is None:
623620
copy = False # The NumPy copy=False meaning is different here.

pandas/core/arrays/period.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,19 +415,15 @@ 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-
422418
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",
419+
"Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
420+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
421+
"is not possible, Pandas will follow this behavior starting with "
422+
"version 3.0. This conversion to NumPy requires a copy, but "
423+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead.",
425424
FutureWarning,
426425
stacklevel=find_stack_level(),
427426
)
428-
raise ValueError(
429-
"Unable to avoid copy while creating an array as requested."
430-
)
431427

432428
if dtype == bool:
433429
return ~self._isnan

pandas/core/arrays/sparse/array.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -562,19 +562,15 @@ 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-
569565
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",
566+
"Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
567+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
568+
"is not possible, Pandas will follow this behavior starting with "
569+
"version 3.0. This conversion to NumPy requires a copy, but "
570+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead.",
572571
FutureWarning,
573572
stacklevel=find_stack_level(),
574573
)
575-
raise ValueError(
576-
"Unable to avoid copy while creating an array as requested."
577-
)
578574

579575
fill_value = self.fill_value
580576

pandas/core/generic.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,20 +2151,16 @@ def __array__(
21512151
) -> np.ndarray:
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
2154-
# 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-
2154+
# and np.array(values, copy=False) will not raise a warning
21592155
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.",
2156+
"Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
2157+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
2158+
"is not possible, Pandas will follow this behavior starting with "
2159+
"version 3.0. This conversion to NumPy requires a copy, but "
2160+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead.",
21622161
FutureWarning,
21632162
stacklevel=find_stack_level(),
21642163
)
2165-
raise ValueError(
2166-
"Unable to avoid copy while creating an array as requested."
2167-
)
21682164
values = self._values
21692165
if copy is None:
21702166
# Note: branch avoids `copy=None` for NumPy 1.x support

pandas/core/indexes/multi.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,19 +1314,15 @@ 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-
13211317
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",
1318+
"Starting on NumPy 2.0, the behavior of the 'copy' keyword has changed "
1319+
"and passing 'copy=False' raises an error when a zero-copy NumPy array "
1320+
"is not possible, Pandas will follow this behavior starting with "
1321+
"version 3.0. This conversion to NumPy requires a copy, but "
1322+
"'copy=False' was passed. Consider using 'np.asarray(..)' instead.",
13241323
FutureWarning,
13251324
stacklevel=find_stack_level(),
13261325
)
1327-
raise ValueError(
1328-
"Unable to avoid copy while creating an array as requested."
1329-
)
13301326
if copy is True:
13311327
# explicit np.array call to ensure a copy is made and unique objects
13321328
# are returned, because self.values is cached

0 commit comments

Comments
 (0)