Skip to content

Commit 5c36932

Browse files
committed
Merge remote-tracking branch 'upstream/main' into sequence_to_td64ns
2 parents fc2405f + e49ab80 commit 5c36932

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+398
-171
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ci:
1919
skip: [pyright, mypy]
2020
repos:
2121
- repo: https://github.com/astral-sh/ruff-pre-commit
22-
rev: v0.6.9
22+
rev: v0.7.2
2323
hooks:
2424
- id: ruff
2525
args: [--exit-non-zero-on-fix]
@@ -74,7 +74,7 @@ repos:
7474
hooks:
7575
- id: isort
7676
- repo: https://github.com/asottile/pyupgrade
77-
rev: v3.17.0
77+
rev: v3.19.0
7878
hooks:
7979
- id: pyupgrade
8080
args: [--py310-plus]
@@ -95,7 +95,7 @@ repos:
9595
- id: sphinx-lint
9696
args: ["--enable", "all", "--disable", "line-too-long"]
9797
- repo: https://github.com/pre-commit/mirrors-clang-format
98-
rev: v19.1.1
98+
rev: v19.1.3
9999
hooks:
100100
- id: clang-format
101101
files: ^pandas/_libs/src|^pandas/_libs/include

doc/source/getting_started/comparison/comparison_with_r.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ In Python, this list would be a list of tuples, so
405405
a = list(enumerate(list(range(1, 5)) + [np.NAN]))
406406
pd.DataFrame(a)
407407
408-
For more details and examples see :ref:`the Into to Data Structures
408+
For more details and examples see :ref:`the Intro to Data Structures
409409
documentation <dsintro>`.
410410

411411
meltdf

doc/source/whatsnew/v2.3.0.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ enhancement1
3232
Other enhancements
3333
^^^^^^^^^^^^^^^^^^
3434

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

@@ -170,7 +173,8 @@ Styler
170173

171174
Other
172175
^^^^^
173-
-
176+
- Fixed usage of ``inspect`` when the optional dependencies ``pyarrow`` or ``jinja2``
177+
are not installed (:issue:`60196`)
174178
-
175179

176180
.. ---------------------------------------------------------------------------

doc/source/whatsnew/v3.0.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,9 @@ Other
772772
- Bug in :meth:`DataFrame.apply` where passing ``engine="numba"`` ignored ``args`` passed to the applied function (:issue:`58712`)
773773
- Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which caused an exception when using NumPy attributes via ``@`` notation, e.g., ``df.eval("@np.floor(a)")``. (:issue:`58041`)
774774
- Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which did not allow to use ``tan`` function. (:issue:`55091`)
775+
- Bug in :meth:`DataFrame.query` where using duplicate column names led to a ``TypeError``. (:issue:`59950`)
775776
- Bug in :meth:`DataFrame.query` which raised an exception or produced incorrect results when expressions contained backtick-quoted column names containing the hash character ``#``, backticks, or characters that fall outside the ASCII range (U+0001..U+007F). (:issue:`59285`) (:issue:`49633`)
777+
- Bug in :meth:`DataFrame.shift` where passing a ``freq`` on a DataFrame with no columns did not shift the index correctly. (:issue:`60102`)
776778
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)
777779
- Bug in :meth:`DataFrame.transform` that was returning the wrong order unless the index was monotonically increasing. (:issue:`57069`)
778780
- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`)

pandas/core/arrays/arrow/accessors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _is_valid_pyarrow_dtype(self, pyarrow_dtype) -> bool:
4646

4747
def _validate(self, data) -> None:
4848
dtype = data.dtype
49-
if not isinstance(dtype, ArrowDtype):
49+
if pa_version_under10p1 or not isinstance(dtype, ArrowDtype):
5050
# Raise AttributeError so that inspect can handle non-struct Series.
5151
raise AttributeError(self._validation_msg.format(dtype=dtype))
5252

pandas/core/arrays/arrow/array.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,16 @@ def __array__(
668668
self, dtype: NpDtype | None = None, copy: bool | None = None
669669
) -> np.ndarray:
670670
"""Correctly construct numpy arrays when passed to `np.asarray()`."""
671-
return self.to_numpy(dtype=dtype)
671+
if copy is False:
672+
# TODO: By using `zero_copy_only` it may be possible to implement this
673+
raise ValueError(
674+
"Unable to avoid copy while creating an array as requested."
675+
)
676+
elif copy is None:
677+
# `to_numpy(copy=False)` has the meaning of NumPy `copy=None`.
678+
copy = False
679+
680+
return self.to_numpy(dtype=dtype, copy=copy)
672681

673682
def __invert__(self) -> Self:
674683
# This is a bit wise op for integer types
@@ -734,7 +743,7 @@ def _cmp_method(self, other, op) -> ArrowExtensionArray:
734743
try:
735744
result[valid] = op(np_array[valid], other)
736745
except TypeError:
737-
result = ops.invalid_comparison(np_array, other, op)
746+
result = ops.invalid_comparison(self, other, op)
738747
result = pa.array(result, type=pa.bool_())
739748
result = pc.if_else(valid, result, None)
740749
else:

pandas/core/arrays/categorical.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,12 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
579579
raise ValueError("Cannot convert float NaN to integer")
580580

581581
elif len(self.codes) == 0 or len(self.categories) == 0:
582-
result = np.array(
583-
self,
584-
dtype=dtype,
585-
copy=copy,
586-
)
582+
# For NumPy 1.x compatibility we cannot use copy=None. And
583+
# `copy=False` has the meaning of `copy=None` here:
584+
if not copy:
585+
result = np.asarray(self, dtype=dtype)
586+
else:
587+
result = np.array(self, dtype=dtype)
587588

588589
else:
589590
# GH8628 (PERF): astype category codes instead of astyping array
@@ -1663,7 +1664,7 @@ def __array__(
16631664
Specifies the the dtype for the array.
16641665
16651666
copy : bool or None, optional
1666-
Unused.
1667+
See :func:`numpy.asarray`.
16671668
16681669
Returns
16691670
-------
@@ -1686,13 +1687,18 @@ def __array__(
16861687
>>> np.asarray(cat)
16871688
array(['a', 'b'], dtype=object)
16881689
"""
1690+
if copy is False:
1691+
raise ValueError(
1692+
"Unable to avoid copy while creating an array as requested."
1693+
)
1694+
16891695
ret = take_nd(self.categories._values, self._codes)
1690-
if dtype and np.dtype(dtype) != self.categories.dtype:
1691-
return np.asarray(ret, dtype)
16921696
# When we're a Categorical[ExtensionArray], like Interval,
16931697
# we need to ensure __array__ gets all the way to an
16941698
# ndarray.
1695-
return np.asarray(ret)
1699+
1700+
# `take_nd` should already make a copy, so don't force again.
1701+
return np.asarray(ret, dtype=dtype)
16961702

16971703
def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
16981704
# for binary ops, use our custom dunder methods

pandas/core/arrays/datetimelike.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,14 @@ def __array__(
359359
) -> np.ndarray:
360360
# used for Timedelta/DatetimeArray, overwritten by PeriodArray
361361
if is_object_dtype(dtype):
362+
if copy is False:
363+
raise ValueError(
364+
"Unable to avoid copy while creating an array as requested."
365+
)
362366
return np.array(list(self), dtype=object)
367+
368+
if copy is True:
369+
return np.array(self._ndarray, dtype=dtype)
363370
return self._ndarray
364371

365372
@overload

pandas/core/arrays/interval.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,11 @@ def __array__(
16221622
Return the IntervalArray's data as a numpy array of Interval
16231623
objects (with dtype='object')
16241624
"""
1625+
if copy is False:
1626+
raise ValueError(
1627+
"Unable to avoid copy while creating an array as requested."
1628+
)
1629+
16251630
left = self._left
16261631
right = self._right
16271632
mask = self.isna()

pandas/core/arrays/masked.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,17 @@ def __array__(
581581
the array interface, return my values
582582
We return an object array here to preserve our scalar values
583583
"""
584-
return self.to_numpy(dtype=dtype)
584+
if copy is False:
585+
if not self._hasna:
586+
# special case, here we can simply return the underlying data
587+
return np.array(self._data, dtype=dtype, copy=copy)
588+
raise ValueError(
589+
"Unable to avoid copy while creating an array as requested."
590+
)
591+
592+
if copy is None:
593+
copy = False # The NumPy copy=False meaning is different here.
594+
return self.to_numpy(dtype=dtype, copy=copy)
585595

586596
_HANDLED_TYPES: tuple[type, ...]
587597

0 commit comments

Comments
 (0)