Skip to content

Commit e96627f

Browse files
committed
Merge branch 'main' into ref-gpr_index
2 parents 05f5485 + 1efc2d7 commit e96627f

File tree

16 files changed

+88
-179
lines changed

16 files changed

+88
-179
lines changed

Diff for: ci/code_checks.sh

-3
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,7 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
597597
pandas.api.types.is_datetime64_dtype \
598598
pandas.api.types.is_datetime64_ns_dtype \
599599
pandas.api.types.is_datetime64tz_dtype \
600-
pandas.api.types.is_dict_like \
601-
pandas.api.types.is_file_like \
602600
pandas.api.types.is_float_dtype \
603-
pandas.api.types.is_hashable \
604601
pandas.api.types.is_int64_dtype \
605602
pandas.api.types.is_integer_dtype \
606603
pandas.api.types.is_interval_dtype \

Diff for: doc/source/whatsnew/v2.0.0.rst

+7-7
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,14 @@ Below is a possibly non-exhaustive list of changes:
6565

6666
1. Instantiating using a numpy numeric array now follows the dtype of the numpy array.
6767
Previously, all indexes created from numpy numeric arrays were forced to 64-bit. Now,
68-
the index dtype follows the dtype of the numpy array. For example, it would for all
69-
signed integer arrays previously return an index with ``int64`` dtype, but will now
70-
reuse the dtype of the supplied numpy array. So ``Index(np.array([1, 2, 3]))`` will be ``int32`` on 32-bit systems.
68+
for example, ``Index(np.array([1, 2, 3]))`` will be ``int32`` on 32-bit systems, where
69+
it previously would have been ``int64``` even on 32-bit systems.
7170
Instantiating :class:`Index` using a list of numbers will still return 64bit dtypes,
7271
e.g. ``Index([1, 2, 3])`` will have a ``int64`` dtype, which is the same as previously.
73-
2. The various numeric datetime attributes of :class:`DateTimeIndex` (:attr:`~Date_TimeIndex.day`,
74-
:attr:`~DateTimeIndex.month`, :attr:`~DateTimeIndex.year` etc.) were previously in of
72+
2. The various numeric datetime attributes of :class:`DatetimeIndex` (:attr:`~DatetimeIndex.day`,
73+
:attr:`~DatetimeIndex.month`, :attr:`~DatetimeIndex.year` etc.) were previously in of
7574
dtype ``int64``, while they were ``int32`` for :class:`DatetimeArray`. They are now
76-
``int32`` on ``DateTimeIndex`` also:
75+
``int32`` on ``DatetimeIndex`` also:
7776

7877
.. ipython:: python
7978
@@ -92,7 +91,7 @@ Below is a possibly non-exhaustive list of changes:
9291
([3.0, 1.0, 2.0], ([1, 0, 0], [0, 2, 3])), shape=(3, 4)
9392
)
9493
ser = pd.Series.sparse.from_coo(A)
95-
ser.index.dtype
94+
ser.index.dtypes
9695
9796
4. :class:`Index` cannot be instantiated using a float16 dtype. Previously instantiating
9897
an :class:`Index` using dtype ``float16`` resulted in a :class:`Float64Index` with a
@@ -1325,6 +1324,7 @@ ExtensionArray
13251324
- Bug in :meth:`api.types.is_numeric_dtype` where a custom :class:`ExtensionDtype` would not return ``True`` if ``_is_numeric`` returned ``True`` (:issue:`50563`)
13261325
- Bug in :meth:`api.types.is_integer_dtype`, :meth:`api.types.is_unsigned_integer_dtype`, :meth:`api.types.is_signed_integer_dtype`, :meth:`api.types.is_float_dtype` where a custom :class:`ExtensionDtype` would not return ``True`` if ``kind`` returned the corresponding NumPy type (:issue:`50667`)
13271326
- Bug in :class:`Series` constructor unnecessarily overflowing for nullable unsigned integer dtypes (:issue:`38798`, :issue:`25880`)
1327+
- Bug in setting non-string value into ``StringArray`` raising ``ValueError`` instead of ``TypeError`` (:issue:`49632`)
13281328

13291329
Styler
13301330
^^^^^^

Diff for: pandas/core/array_algos/replace.py

+1-12
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
)
2020

2121
from pandas.core.dtypes.common import (
22-
is_datetimelike_v_numeric,
23-
is_numeric_v_string_like,
2422
is_re,
2523
is_re_compilable,
2624
is_scalar,
@@ -44,7 +42,7 @@ def should_use_regex(regex: bool, to_replace: Any) -> bool:
4442

4543
def compare_or_regex_search(
4644
a: ArrayLike, b: Scalar | Pattern, regex: bool, mask: npt.NDArray[np.bool_]
47-
) -> ArrayLike | bool:
45+
) -> ArrayLike:
4846
"""
4947
Compare two array-like inputs of the same shape or two scalar values
5048
@@ -95,15 +93,6 @@ def _check_comparison_types(
9593
if isinstance(a, np.ndarray):
9694
a = a[mask]
9795

98-
if is_numeric_v_string_like(a, b):
99-
# GH#29553 avoid deprecation warnings from numpy
100-
return np.zeros(a.shape, dtype=bool)
101-
102-
elif is_datetimelike_v_numeric(a, b):
103-
# GH#29553 avoid deprecation warnings from numpy
104-
_check_comparison_types(False, a, b)
105-
return False
106-
10796
result = op(a)
10897

10998
if isinstance(result, np.ndarray) and mask is not None:

Diff for: pandas/core/arrays/numpy_.py

+16
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
)
1717
from pandas.compat.numpy import function as nv
1818

19+
from pandas.core.dtypes.astype import astype_array
1920
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
21+
from pandas.core.dtypes.common import (
22+
is_dtype_equal,
23+
pandas_dtype,
24+
)
2025
from pandas.core.dtypes.dtypes import PandasDtype
2126
from pandas.core.dtypes.missing import isna
2227

@@ -185,6 +190,17 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
185190
# ------------------------------------------------------------------------
186191
# Pandas ExtensionArray Interface
187192

193+
def astype(self, dtype, copy: bool = True):
194+
dtype = pandas_dtype(dtype)
195+
196+
if is_dtype_equal(dtype, self.dtype):
197+
if copy:
198+
return self.copy()
199+
return self
200+
201+
result = astype_array(self._ndarray, dtype=dtype, copy=copy)
202+
return result
203+
188204
def isna(self) -> np.ndarray:
189205
return isna(self._ndarray)
190206

Diff for: pandas/core/arrays/string_.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,14 @@ def __setitem__(self, key, value):
414414
if isna(value):
415415
value = libmissing.NA
416416
elif not isinstance(value, str):
417-
raise ValueError(
417+
raise TypeError(
418418
f"Cannot set non-string value '{value}' into a StringArray."
419419
)
420420
else:
421421
if not is_array_like(value):
422422
value = np.asarray(value, dtype=object)
423423
if len(value) and not lib.is_string_array(value, skipna=True):
424-
raise ValueError("Must provide strings.")
424+
raise TypeError("Must provide strings.")
425425

426426
value[isna(value)] = libmissing.NA
427427

@@ -454,7 +454,8 @@ def astype(self, dtype, copy: bool = True):
454454
values = arr.astype(dtype.numpy_dtype)
455455
return FloatingArray(values, mask, copy=False)
456456
elif isinstance(dtype, ExtensionDtype):
457-
return super().astype(dtype, copy=copy)
457+
# Skip the PandasArray.astype method
458+
return ExtensionArray.astype(self, dtype, copy)
458459
elif np.issubdtype(dtype, np.floating):
459460
arr = self._ndarray.copy()
460461
mask = self.isna()

Diff for: pandas/core/arrays/string_arrow.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ def _maybe_convert_setitem_value(self, value):
163163
if isna(value):
164164
value = None
165165
elif not isinstance(value, str):
166-
raise ValueError("Scalar must be NA or str")
166+
raise TypeError("Scalar must be NA or str")
167167
else:
168168
value = np.array(value, dtype=object, copy=True)
169169
value[isna(value)] = None
170170
for v in value:
171171
if not (v is None or isinstance(v, str)):
172-
raise ValueError("Scalar must be NA or str")
172+
raise TypeError("Scalar must be NA or str")
173173
return super()._maybe_convert_setitem_value(value)
174174

175175
def isin(self, values) -> npt.NDArray[np.bool_]:

Diff for: pandas/core/dtypes/common.py

-60
Original file line numberDiff line numberDiff line change
@@ -1067,64 +1067,6 @@ def is_numeric_v_string_like(a: ArrayLike, b) -> bool:
10671067
)
10681068

10691069

1070-
# This exists to silence numpy deprecation warnings, see GH#29553
1071-
def is_datetimelike_v_numeric(a, b) -> bool:
1072-
"""
1073-
Check if we are comparing a datetime-like object to a numeric object.
1074-
By "numeric," we mean an object that is either of an int or float dtype.
1075-
1076-
Parameters
1077-
----------
1078-
a : array-like, scalar
1079-
The first object to check.
1080-
b : array-like, scalar
1081-
The second object to check.
1082-
1083-
Returns
1084-
-------
1085-
boolean
1086-
Whether we return a comparing a datetime-like to a numeric object.
1087-
1088-
Examples
1089-
--------
1090-
>>> from datetime import datetime
1091-
>>> dt = np.datetime64(datetime(2017, 1, 1))
1092-
>>>
1093-
>>> is_datetimelike_v_numeric(1, 1)
1094-
False
1095-
>>> is_datetimelike_v_numeric(dt, dt)
1096-
False
1097-
>>> is_datetimelike_v_numeric(1, dt)
1098-
True
1099-
>>> is_datetimelike_v_numeric(dt, 1) # symmetric check
1100-
True
1101-
>>> is_datetimelike_v_numeric(np.array([dt]), 1)
1102-
True
1103-
>>> is_datetimelike_v_numeric(np.array([1]), dt)
1104-
True
1105-
>>> is_datetimelike_v_numeric(np.array([dt]), np.array([1]))
1106-
True
1107-
>>> is_datetimelike_v_numeric(np.array([1]), np.array([2]))
1108-
False
1109-
>>> is_datetimelike_v_numeric(np.array([dt]), np.array([dt]))
1110-
False
1111-
"""
1112-
if not hasattr(a, "dtype"):
1113-
a = np.asarray(a)
1114-
if not hasattr(b, "dtype"):
1115-
b = np.asarray(b)
1116-
1117-
def is_numeric(x):
1118-
"""
1119-
Check if an object has a numeric dtype (i.e. integer or float).
1120-
"""
1121-
return is_integer_dtype(x) or is_float_dtype(x)
1122-
1123-
return (needs_i8_conversion(a) and is_numeric(b)) or (
1124-
needs_i8_conversion(b) and is_numeric(a)
1125-
)
1126-
1127-
11281070
def needs_i8_conversion(arr_or_dtype) -> bool:
11291071
"""
11301072
Check whether the array or dtype should be converted to int64.
@@ -1790,7 +1732,6 @@ def is_all_strings(value: ArrayLike) -> bool:
17901732
"is_datetime64_dtype",
17911733
"is_datetime64_ns_dtype",
17921734
"is_datetime64tz_dtype",
1793-
"is_datetimelike_v_numeric",
17941735
"is_datetime_or_timedelta_dtype",
17951736
"is_decimal",
17961737
"is_dict_like",
@@ -1809,7 +1750,6 @@ def is_all_strings(value: ArrayLike) -> bool:
18091750
"is_number",
18101751
"is_numeric_dtype",
18111752
"is_any_numeric_dtype",
1812-
"is_numeric_v_string_like",
18131753
"is_object_dtype",
18141754
"is_period_dtype",
18151755
"is_re",

Diff for: pandas/core/dtypes/inference.py

+3
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def is_file_like(obj) -> bool:
119119
Examples
120120
--------
121121
>>> import io
122+
>>> from pandas.api.types import is_file_like
122123
>>> buffer = io.StringIO("data")
123124
>>> is_file_like(buffer)
124125
True
@@ -275,6 +276,7 @@ def is_dict_like(obj) -> bool:
275276
276277
Examples
277278
--------
279+
>>> from pandas.api.types import is_dict_like
278280
>>> is_dict_like({1: 2})
279281
True
280282
>>> is_dict_like([1, 2, 3])
@@ -336,6 +338,7 @@ def is_hashable(obj) -> bool:
336338
Examples
337339
--------
338340
>>> import collections
341+
>>> from pandas.api.types import is_hashable
339342
>>> a = ([],)
340343
>>> isinstance(a, collections.abc.Hashable)
341344
True

Diff for: pandas/core/dtypes/missing.py

-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
is_bool_dtype,
2929
is_categorical_dtype,
3030
is_complex_dtype,
31-
is_datetimelike_v_numeric,
3231
is_dtype_equal,
3332
is_extension_array_dtype,
3433
is_float_dtype,
@@ -505,8 +504,6 @@ def array_equivalent(
505504
# fastpath when we require that the dtypes match (Block.equals)
506505
if left.dtype.kind in ["f", "c"]:
507506
return _array_equivalent_float(left, right)
508-
elif is_datetimelike_v_numeric(left.dtype, right.dtype):
509-
return False
510507
elif needs_i8_conversion(left.dtype):
511508
return _array_equivalent_datetimelike(left, right)
512509
elif is_string_or_object_np_dtype(left.dtype):
@@ -529,10 +526,6 @@ def array_equivalent(
529526
return True
530527
return ((left == right) | (isna(left) & isna(right))).all()
531528

532-
elif is_datetimelike_v_numeric(left, right):
533-
# GH#29553 avoid numpy deprecation warning
534-
return False
535-
536529
elif needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype):
537530
# datetime64, timedelta64, Period
538531
if not is_dtype_equal(left.dtype, right.dtype):

0 commit comments

Comments
 (0)