Skip to content

Commit 0b5c12d

Browse files
authored
CLN: follow-ups (pandas-dev#40000)
1 parent 8a307c1 commit 0b5c12d

File tree

8 files changed

+38
-22
lines changed

8 files changed

+38
-22
lines changed

.pre-commit-config.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ repos:
163163
entry: np\.bool[^_8]
164164
language: pygrep
165165
types_or: [python, cython, rst]
166+
- id: np-object
167+
name: Check for use of np.object instead of np.object_
168+
entry: np\.object[^_8]
169+
language: pygrep
170+
types_or: [python, cython, rst]
166171
- id: no-os-remove
167172
name: Check code for instances of os.remove
168173
entry: os\.remove

asv_bench/benchmarks/algos/isin.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def time_isin(self, dtype, exponent, title):
104104

105105
class IsinWithRandomFloat:
106106
params = [
107-
[np.float64, np.object],
107+
[np.float64, np.object_],
108108
[
109109
1_300,
110110
2_000,
@@ -134,7 +134,7 @@ def time_isin(self, dtype, size, title):
134134

135135
class IsinWithArangeSorted:
136136
params = [
137-
[np.float64, np.int64, np.uint64, np.object],
137+
[np.float64, np.int64, np.uint64, np.object_],
138138
[
139139
1_000,
140140
2_000,
@@ -155,7 +155,7 @@ def time_isin(self, dtype, size):
155155

156156
class IsinWithArange:
157157
params = [
158-
[np.float64, np.int64, np.uint64, np.object],
158+
[np.float64, np.int64, np.uint64, np.object_],
159159
[
160160
1_000,
161161
2_000,

doc/source/whatsnew/v0.24.0.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -1755,8 +1755,8 @@ Missing
17551755

17561756
- Bug in :func:`DataFrame.fillna` where a ``ValueError`` would raise when one column contained a ``datetime64[ns, tz]`` dtype (:issue:`15522`)
17571757
- Bug in :func:`Series.hasnans` that could be incorrectly cached and return incorrect answers if null elements are introduced after an initial call (:issue:`19700`)
1758-
- :func:`Series.isin` now treats all NaN-floats as equal also for ``np.object``-dtype. This behavior is consistent with the behavior for float64 (:issue:`22119`)
1759-
- :func:`unique` no longer mangles NaN-floats and the ``NaT``-object for ``np.object``-dtype, i.e. ``NaT`` is no longer coerced to a NaN-value and is treated as a different entity. (:issue:`22295`)
1758+
- :func:`Series.isin` now treats all NaN-floats as equal also for ``np.object_``-dtype. This behavior is consistent with the behavior for float64 (:issue:`22119`)
1759+
- :func:`unique` no longer mangles NaN-floats and the ``NaT``-object for ``np.object_``-dtype, i.e. ``NaT`` is no longer coerced to a NaN-value and is treated as a different entity. (:issue:`22295`)
17601760
- :class:`DataFrame` and :class:`Series` now properly handle numpy masked arrays with hardened masks. Previously, constructing a DataFrame or Series from a masked array with a hard mask would create a pandas object containing the underlying value, rather than the expected NaN. (:issue:`24574`)
17611761
- Bug in :class:`DataFrame` constructor where ``dtype`` argument was not honored when handling numpy masked record arrays. (:issue:`24874`)
17621762

pandas/_libs/lib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1468,7 +1468,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str:
14681468
if is_decimal_array(values):
14691469
return "decimal"
14701470

1471-
elif is_complex(val):
1471+
elif util.is_complex_object(val):
14721472
if is_complex_array(values):
14731473
return "complex"
14741474

pandas/core/array_algos/take.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from typing import Optional
24

35
import numpy as np
@@ -6,6 +8,7 @@
68
algos as libalgos,
79
lib,
810
)
11+
from pandas._typing import ArrayLike
912

1013
from pandas.core.dtypes.cast import maybe_promote
1114
from pandas.core.dtypes.common import (
@@ -14,20 +17,17 @@
1417
)
1518
from pandas.core.dtypes.missing import na_value_for_dtype
1619

17-
from pandas.core.construction import (
18-
ensure_wrapped_if_datetimelike,
19-
extract_array,
20-
)
20+
from pandas.core.construction import ensure_wrapped_if_datetimelike
2121

2222

2323
def take_nd(
24-
arr,
24+
arr: ArrayLike,
2525
indexer,
2626
axis: int = 0,
2727
out: Optional[np.ndarray] = None,
2828
fill_value=lib.no_default,
2929
allow_fill: bool = True,
30-
):
30+
) -> ArrayLike:
3131

3232
"""
3333
Specialized Cython take which sets NaN values in one pass
@@ -37,7 +37,7 @@ def take_nd(
3737
3838
Parameters
3939
----------
40-
arr : array-like
40+
arr : np.ndarray or ExtensionArray
4141
Input array.
4242
indexer : ndarray
4343
1-D array of indices to take, subarrays corresponding to -1 value
@@ -57,20 +57,29 @@ def take_nd(
5757
5858
Returns
5959
-------
60-
subarray : array-like
60+
subarray : np.ndarray or ExtensionArray
6161
May be the same type as the input, or cast to an ndarray.
6262
"""
6363
if fill_value is lib.no_default:
6464
fill_value = na_value_for_dtype(arr.dtype, compat=False)
6565

66-
arr = extract_array(arr, extract_numpy=True)
67-
6866
if not isinstance(arr, np.ndarray):
6967
# i.e. ExtensionArray,
7068
# includes for EA to catch DatetimeArray, TimedeltaArray
7169
return arr.take(indexer, fill_value=fill_value, allow_fill=allow_fill)
7270

7371
arr = np.asarray(arr)
72+
return _take_nd_ndarray(arr, indexer, axis, out, fill_value, allow_fill)
73+
74+
75+
def _take_nd_ndarray(
76+
arr: np.ndarray,
77+
indexer,
78+
axis: int,
79+
out: Optional[np.ndarray],
80+
fill_value,
81+
allow_fill: bool,
82+
) -> np.ndarray:
7483

7584
indexer, dtype, fill_value, mask_info = _take_preprocess_indexer_and_fill_value(
7685
arr, indexer, axis, out, fill_value, allow_fill

pandas/core/internals/blocks.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,6 @@ def coerce_to_target_dtype(self, other) -> Block:
11861186

11871187
return self.astype(new_dtype, copy=False)
11881188

1189-
@final
11901189
def interpolate(
11911190
self,
11921191
method: str = "pad",
@@ -1293,11 +1292,10 @@ def _interpolate(
12931292

12941293
# only deal with floats
12951294
if self.dtype.kind != "f":
1296-
if self.dtype.kind not in ["i", "u"]:
1297-
return [self]
1298-
data = data.astype(np.float64)
1295+
# bc we already checked that can_hold_na, we dont have int dtype here
1296+
return [self]
12991297

1300-
if fill_value is None:
1298+
if is_valid_na_for_dtype(fill_value, self.dtype):
13011299
fill_value = self.fill_value
13021300

13031301
if method in ("krogh", "piecewise_polynomial", "pchip"):

pandas/core/reshape/merge.py

+4
Original file line numberDiff line numberDiff line change
@@ -874,12 +874,16 @@ def _maybe_add_join_keys(self, result, left_indexer, right_indexer):
874874
if take_left is None:
875875
lvals = result[name]._values
876876
else:
877+
# TODO: can we pin down take_left's type earlier?
878+
take_left = extract_array(take_left, extract_numpy=True)
877879
lfill = na_value_for_dtype(take_left.dtype)
878880
lvals = algos.take_nd(take_left, left_indexer, fill_value=lfill)
879881

880882
if take_right is None:
881883
rvals = result[name]._values
882884
else:
885+
# TODO: can we pin down take_right's type earlier?
886+
take_right = extract_array(take_right, extract_numpy=True)
883887
rfill = na_value_for_dtype(take_right.dtype)
884888
rvals = algos.take_nd(take_right, right_indexer, fill_value=rfill)
885889

pandas/tests/resample/test_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def test_resample_empty_dtypes(index, dtype, resample_method):
218218
getattr(empty_series_dti.resample("d"), resample_method)()
219219
except DataError:
220220
# Ignore these since some combinations are invalid
221-
# (ex: doing mean with dtype of np.object)
221+
# (ex: doing mean with dtype of np.object_)
222222
pass
223223

224224

0 commit comments

Comments
 (0)