Skip to content

Commit 3079aa0

Browse files
committed
Merge branch '2.3.x' of https://github.com/pandas-dev/pandas into 2.3.x
2 parents a993738 + fcc94eb commit 3079aa0

37 files changed

+242
-144
lines changed

.github/workflows/wheels.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
buildplat:
9595
- [ubuntu-22.04, manylinux_x86_64]
9696
- [ubuntu-22.04, musllinux_x86_64]
97-
- [macos-12, macosx_x86_64]
97+
- [macos-13, macosx_x86_64]
9898
# Note: M1 images on Github Actions start from macOS 14
9999
- [macos-14, macosx_arm64]
100100
- [windows-2022, win_amd64]

doc/source/whatsnew/v2.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Other enhancements
3434

3535
- The semantics for the ``copy`` keyword in ``__array__`` methods (i.e. called
3636
when using ``np.array()`` or ``np.asarray()`` on pandas objects) has been
37-
updated to work correctly with NumPy >= 2 (:issue:`57739`)
37+
updated to raise FutureWarning with NumPy >= 2 (:issue:`60340`)
3838
- The :meth:`~Series.sum` reduction is now implemented for ``StringDtype`` columns (:issue:`59853`)
3939
-
4040

pandas/_libs/index.pyx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -536,23 +536,15 @@ cdef class StringObjectEngine(ObjectEngine):
536536

537537
cdef:
538538
object na_value
539-
bint uses_na
540539

541540
def __init__(self, ndarray values, na_value):
542541
super().__init__(values)
543542
self.na_value = na_value
544-
self.uses_na = na_value is C_NA
545-
546-
cdef bint _checknull(self, object val):
547-
if self.uses_na:
548-
return val is C_NA
549-
else:
550-
return util.is_nan(val)
551543

552544
cdef _check_type(self, object val):
553545
if isinstance(val, str):
554546
return val
555-
elif self._checknull(val):
547+
elif checknull(val):
556548
return self.na_value
557549
else:
558550
raise KeyError(val)

pandas/core/arrays/arrow/array.py

Lines changed: 14 additions & 3 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,9 +665,15 @@ def __array__(
663665
) -> np.ndarray:
664666
"""Correctly construct numpy arrays when passed to `np.asarray()`."""
665667
if copy is False:
666-
# TODO: By using `zero_copy_only` it may be possible to implement this
667-
raise ValueError(
668-
"Unable to avoid copy while creating an array as requested."
668+
warnings.warn(
669+
"Starting with NumPy 2.0, the behavior of the 'copy' keyword has "
670+
"changed and passing 'copy=False' raises an error when returning "
671+
"a zero-copy NumPy array is not possible. pandas will follow "
672+
"this behavior starting with pandas 3.0.\nThis conversion to "
673+
"NumPy requires a copy, but 'copy=False' was passed. Consider "
674+
"using 'np.asarray(..)' instead.",
675+
FutureWarning,
676+
stacklevel=find_stack_level(),
669677
)
670678
elif copy is None:
671679
# `to_numpy(copy=False)` has the meaning of NumPy `copy=None`.
@@ -2150,6 +2158,9 @@ def interpolate(
21502158
See NDFrame.interpolate.__doc__.
21512159
"""
21522160
# NB: we return type(self) even if copy=False
2161+
if not self.dtype._is_numeric:
2162+
raise TypeError(f"Cannot interpolate with {self.dtype} dtype")
2163+
21532164
mask = self.isna()
21542165
if self.dtype.kind == "f":
21552166
data = self._pa_array.to_numpy()

pandas/core/arrays/categorical.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,8 +1672,15 @@ def __array__(
16721672
array(['a', 'b'], dtype=object)
16731673
"""
16741674
if copy is False:
1675-
raise ValueError(
1676-
"Unable to avoid copy while creating an array as requested."
1675+
warnings.warn(
1676+
"Starting with NumPy 2.0, the behavior of the 'copy' keyword has "
1677+
"changed and passing 'copy=False' raises an error when returning "
1678+
"a zero-copy NumPy array is not possible. pandas will follow "
1679+
"this behavior starting with pandas 3.0.\nThis conversion to "
1680+
"NumPy requires a copy, but 'copy=False' was passed. Consider "
1681+
"using 'np.asarray(..)' instead.",
1682+
FutureWarning,
1683+
stacklevel=find_stack_level(),
16771684
)
16781685

16791686
ret = take_nd(self.categories._values, self._codes)

pandas/core/arrays/datetimelike.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,17 @@ def __array__(
359359
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
360360
if is_object_dtype(dtype):
361361
if copy is False:
362-
raise ValueError(
363-
"Unable to avoid copy while creating an array as requested."
362+
warnings.warn(
363+
"Starting with NumPy 2.0, the behavior of the 'copy' keyword has "
364+
"changed and passing 'copy=False' raises an error when returning "
365+
"a zero-copy NumPy array is not possible. pandas will follow this "
366+
"behavior starting with pandas 3.0.\nThis conversion to NumPy "
367+
"requires a copy, but 'copy=False' was passed. Consider using "
368+
"'np.asarray(..)' instead.",
369+
FutureWarning,
370+
stacklevel=find_stack_level(),
364371
)
372+
365373
return np.array(list(self), dtype=object)
366374

367375
if copy is True:

pandas/core/arrays/interval.py

Lines changed: 10 additions & 2 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,8 +1576,15 @@ def __array__(
15751576
objects (with dtype='object')
15761577
"""
15771578
if copy is False:
1578-
raise ValueError(
1579-
"Unable to avoid copy while creating an array as requested."
1579+
warnings.warn(
1580+
"Starting with NumPy 2.0, the behavior of the 'copy' keyword has "
1581+
"changed and passing 'copy=False' raises an error when returning "
1582+
"a zero-copy NumPy array is not possible. pandas will follow "
1583+
"this behavior starting with pandas 3.0.\nThis conversion to "
1584+
"NumPy requires a copy, but 'copy=False' was passed. Consider "
1585+
"using 'np.asarray(..)' instead.",
1586+
FutureWarning,
1587+
stacklevel=find_stack_level(),
15801588
)
15811589

15821590
left = self._left

pandas/core/arrays/masked.py

Lines changed: 11 additions & 2 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
@@ -604,8 +605,16 @@ def __array__(
604605
if not self._hasna:
605606
# special case, here we can simply return the underlying data
606607
return np.array(self._data, dtype=dtype, copy=copy)
607-
raise ValueError(
608-
"Unable to avoid copy while creating an array as requested."
608+
609+
warnings.warn(
610+
"Starting with NumPy 2.0, the behavior of the 'copy' keyword has "
611+
"changed and passing 'copy=False' raises an error when returning "
612+
"a zero-copy NumPy array is not possible. pandas will follow "
613+
"this behavior starting with pandas 3.0.\nThis conversion to "
614+
"NumPy requires a copy, but 'copy=False' was passed. Consider "
615+
"using 'np.asarray(..)' instead.",
616+
FutureWarning,
617+
stacklevel=find_stack_level(),
609618
)
610619

611620
if copy is None:

pandas/core/arrays/numpy_.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ def interpolate(
287287
See NDFrame.interpolate.__doc__.
288288
"""
289289
# NB: we return type(self) even if copy=False
290+
if not self.dtype._is_numeric:
291+
raise TypeError(f"Cannot interpolate with {self.dtype} dtype")
292+
290293
if not copy:
291294
out_data = self._ndarray
292295
else:

pandas/core/arrays/period.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,15 @@ def __array__(
415415
return np.array(self.asi8, dtype=dtype)
416416

417417
if copy is False:
418-
raise ValueError(
419-
"Unable to avoid copy while creating an array as requested."
418+
warnings.warn(
419+
"Starting with NumPy 2.0, the behavior of the 'copy' keyword has "
420+
"changed and passing 'copy=False' raises an error when returning "
421+
"a zero-copy NumPy array is not possible. pandas will follow "
422+
"this behavior starting with pandas 3.0.\nThis conversion to "
423+
"NumPy requires a copy, but 'copy=False' was passed. Consider "
424+
"using 'np.asarray(..)' instead.",
425+
FutureWarning,
426+
stacklevel=find_stack_level(),
420427
)
421428

422429
if dtype == bool:

0 commit comments

Comments
 (0)