Skip to content

Commit d17476d

Browse files
PERF/CLN: restrict _isna_ndarraylike to actual ndarray/EA (#40254)
1 parent bff0afd commit d17476d

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

pandas/core/dtypes/missing.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,22 @@ def _isna(obj, inf_as_na: bool = False):
163163
raise NotImplementedError("isna is not defined for MultiIndex")
164164
elif isinstance(obj, type):
165165
return False
166-
elif isinstance(obj, (ABCSeries, np.ndarray, ABCIndex, ABCExtensionArray)):
167-
return _isna_ndarraylike(obj, inf_as_na=inf_as_na)
166+
elif isinstance(obj, (np.ndarray, ABCExtensionArray)):
167+
return _isna_array(obj, inf_as_na=inf_as_na)
168+
elif isinstance(obj, (ABCSeries, ABCIndex)):
169+
result = _isna_array(obj._values, inf_as_na=inf_as_na)
170+
# box
171+
if isinstance(obj, ABCSeries):
172+
result = obj._constructor(
173+
result, index=obj.index, name=obj.name, copy=False
174+
)
175+
return result
168176
elif isinstance(obj, ABCDataFrame):
169177
return obj.isna()
170178
elif isinstance(obj, list):
171-
return _isna_ndarraylike(np.asarray(obj, dtype=object), inf_as_na=inf_as_na)
179+
return _isna_array(np.asarray(obj, dtype=object), inf_as_na=inf_as_na)
172180
elif hasattr(obj, "__array__"):
173-
return _isna_ndarraylike(np.asarray(obj), inf_as_na=inf_as_na)
181+
return _isna_array(np.asarray(obj), inf_as_na=inf_as_na)
174182
else:
175183
return False
176184

@@ -206,13 +214,13 @@ def _use_inf_as_na(key):
206214
globals()["INF_AS_NA"] = False
207215

208216

209-
def _isna_ndarraylike(obj, inf_as_na: bool = False):
217+
def _isna_array(values: ArrayLike, inf_as_na: bool = False):
210218
"""
211219
Return an array indicating which values of the input array are NaN / NA.
212220
213221
Parameters
214222
----------
215-
obj: array-like
223+
obj: ndarray or ExtensionArray
216224
The input array whose elements are to be checked.
217225
inf_as_na: bool
218226
Whether or not to treat infinite values as NA.
@@ -222,7 +230,6 @@ def _isna_ndarraylike(obj, inf_as_na: bool = False):
222230
array-like
223231
Array of boolean values denoting the NA status of each element.
224232
"""
225-
values = getattr(obj, "_values", obj)
226233
dtype = values.dtype
227234

228235
if is_extension_array_dtype(dtype):
@@ -241,10 +248,6 @@ def _isna_ndarraylike(obj, inf_as_na: bool = False):
241248
else:
242249
result = np.isnan(values)
243250

244-
# box
245-
if isinstance(obj, ABCSeries):
246-
result = obj._constructor(result, index=obj.index, name=obj.name, copy=False)
247-
248251
return result
249252

250253

@@ -650,7 +653,7 @@ def isna_all(arr: ArrayLike) -> bool:
650653
checker = lambda x: np.asarray(x.view("i8")) == iNaT
651654

652655
else:
653-
checker = lambda x: _isna_ndarraylike(x, inf_as_na=INF_AS_NA)
656+
checker = lambda x: _isna_array(x, inf_as_na=INF_AS_NA)
654657

655658
return all(
656659
checker(arr[i : i + chunk_len]).all() for i in range(0, total_len, chunk_len)

0 commit comments

Comments
 (0)