4
4
from __future__ import annotations
5
5
6
6
from decimal import Decimal
7
- from functools import partial
8
7
from typing import (
9
8
TYPE_CHECKING ,
10
9
overload ,
13
12
14
13
import numpy as np
15
14
16
- from pandas ._config import get_option
17
-
18
15
from pandas ._libs import lib
19
16
import pandas ._libs .missing as libmissing
20
17
from pandas ._libs .tslibs import (
64
61
isposinf_scalar = libmissing .isposinf_scalar
65
62
isneginf_scalar = libmissing .isneginf_scalar
66
63
67
- nan_checker = np .isnan
68
- INF_AS_NA = False
69
64
_dtype_object = np .dtype ("object" )
70
65
_dtype_str = np .dtype (str )
71
66
@@ -180,95 +175,57 @@ def isna(obj: object) -> bool | npt.NDArray[np.bool_] | NDFrame:
180
175
isnull = isna
181
176
182
177
183
- def _isna (obj , inf_as_na : bool = False ):
178
+ def _isna (obj ):
184
179
"""
185
- Detect missing values, treating None, NaN or NA as null. Infinite
186
- values will also be treated as null if inf_as_na is True.
180
+ Detect missing values, treating None, NaN or NA as null.
187
181
188
182
Parameters
189
183
----------
190
184
obj: ndarray or object value
191
185
Input array or scalar value.
192
- inf_as_na: bool
193
- Whether to treat infinity as null.
194
186
195
187
Returns
196
188
-------
197
189
boolean ndarray or boolean
198
190
"""
199
191
if is_scalar (obj ):
200
- return libmissing .checknull (obj , inf_as_na = inf_as_na )
192
+ return libmissing .checknull (obj )
201
193
elif isinstance (obj , ABCMultiIndex ):
202
194
raise NotImplementedError ("isna is not defined for MultiIndex" )
203
195
elif isinstance (obj , type ):
204
196
return False
205
197
elif isinstance (obj , (np .ndarray , ABCExtensionArray )):
206
- return _isna_array (obj , inf_as_na = inf_as_na )
198
+ return _isna_array (obj )
207
199
elif isinstance (obj , ABCIndex ):
208
200
# Try to use cached isna, which also short-circuits for integer dtypes
209
201
# and avoids materializing RangeIndex._values
210
202
if not obj ._can_hold_na :
211
203
return obj .isna ()
212
- return _isna_array (obj ._values , inf_as_na = inf_as_na )
204
+ return _isna_array (obj ._values )
213
205
214
206
elif isinstance (obj , ABCSeries ):
215
- result = _isna_array (obj ._values , inf_as_na = inf_as_na )
207
+ result = _isna_array (obj ._values )
216
208
# box
217
209
result = obj ._constructor (result , index = obj .index , name = obj .name , copy = False )
218
210
return result
219
211
elif isinstance (obj , ABCDataFrame ):
220
212
return obj .isna ()
221
213
elif isinstance (obj , list ):
222
- return _isna_array (np .asarray (obj , dtype = object ), inf_as_na = inf_as_na )
214
+ return _isna_array (np .asarray (obj , dtype = object ))
223
215
elif hasattr (obj , "__array__" ):
224
- return _isna_array (np .asarray (obj ), inf_as_na = inf_as_na )
216
+ return _isna_array (np .asarray (obj ))
225
217
else :
226
218
return False
227
219
228
220
229
- def _use_inf_as_na (key ) -> None :
230
- """
231
- Option change callback for na/inf behaviour.
232
-
233
- Choose which replacement for numpy.isnan / -numpy.isfinite is used.
234
-
235
- Parameters
236
- ----------
237
- flag: bool
238
- True means treat None, NaN, INF, -INF as null (old way),
239
- False means None and NaN are null, but INF, -INF are not null
240
- (new way).
241
-
242
- Notes
243
- -----
244
- This approach to setting global module values is discussed and
245
- approved here:
246
-
247
- * https://stackoverflow.com/questions/4859217/
248
- programmatically-creating-variables-in-python/4859312#4859312
249
- """
250
- inf_as_na = get_option (key )
251
- globals ()["_isna" ] = partial (_isna , inf_as_na = inf_as_na )
252
- if inf_as_na :
253
- globals ()["nan_checker" ] = lambda x : ~ np .isfinite (x )
254
- globals ()["INF_AS_NA" ] = True
255
- else :
256
- globals ()["nan_checker" ] = np .isnan
257
- globals ()["INF_AS_NA" ] = False
258
-
259
-
260
- def _isna_array (
261
- values : ArrayLike , inf_as_na : bool = False
262
- ) -> npt .NDArray [np .bool_ ] | NDFrame :
221
+ def _isna_array (values : ArrayLike ) -> npt .NDArray [np .bool_ ] | NDFrame :
263
222
"""
264
223
Return an array indicating which values of the input array are NaN / NA.
265
224
266
225
Parameters
267
226
----------
268
227
obj: ndarray or ExtensionArray
269
228
The input array whose elements are to be checked.
270
- inf_as_na: bool
271
- Whether or not to treat infinite values as NA.
272
229
273
230
Returns
274
231
-------
@@ -280,73 +237,47 @@ def _isna_array(
280
237
281
238
if not isinstance (values , np .ndarray ):
282
239
# i.e. ExtensionArray
283
- if inf_as_na and isinstance (dtype , CategoricalDtype ):
284
- result = libmissing .isnaobj (values .to_numpy (), inf_as_na = inf_as_na )
285
- else :
286
- # error: Incompatible types in assignment (expression has type
287
- # "Union[ndarray[Any, Any], ExtensionArraySupportsAnyAll]", variable has
288
- # type "ndarray[Any, dtype[bool_]]")
289
- result = values .isna () # type: ignore[assignment]
240
+ # error: Incompatible types in assignment (expression has type
241
+ # "Union[ndarray[Any, Any], ExtensionArraySupportsAnyAll]", variable has
242
+ # type "ndarray[Any, dtype[bool_]]")
243
+ result = values .isna () # type: ignore[assignment]
290
244
elif isinstance (values , np .rec .recarray ):
291
245
# GH 48526
292
- result = _isna_recarray_dtype (values , inf_as_na = inf_as_na )
246
+ result = _isna_recarray_dtype (values )
293
247
elif is_string_or_object_np_dtype (values .dtype ):
294
- result = _isna_string_dtype (values , inf_as_na = inf_as_na )
248
+ result = _isna_string_dtype (values )
295
249
elif dtype .kind in "mM" :
296
250
# this is the NaT pattern
297
251
result = values .view ("i8" ) == iNaT
298
252
else :
299
- if inf_as_na :
300
- result = ~ np .isfinite (values )
301
- else :
302
- result = np .isnan (values )
253
+ result = np .isnan (values )
303
254
304
255
return result
305
256
306
257
307
- def _isna_string_dtype (values : np .ndarray , inf_as_na : bool ) -> npt .NDArray [np .bool_ ]:
258
+ def _isna_string_dtype (values : np .ndarray ) -> npt .NDArray [np .bool_ ]:
308
259
# Working around NumPy ticket 1542
309
260
dtype = values .dtype
310
261
311
262
if dtype .kind in ("S" , "U" ):
312
263
result = np .zeros (values .shape , dtype = bool )
313
264
else :
314
265
if values .ndim in {1 , 2 }:
315
- result = libmissing .isnaobj (values , inf_as_na = inf_as_na )
266
+ result = libmissing .isnaobj (values )
316
267
else :
317
268
# 0-D, reached via e.g. mask_missing
318
- result = libmissing .isnaobj (values .ravel (), inf_as_na = inf_as_na )
269
+ result = libmissing .isnaobj (values .ravel ())
319
270
result = result .reshape (values .shape )
320
271
321
272
return result
322
273
323
274
324
- def _has_record_inf_value (record_as_array : np .ndarray ) -> np .bool_ :
325
- is_inf_in_record = np .zeros (len (record_as_array ), dtype = bool )
326
- for i , value in enumerate (record_as_array ):
327
- is_element_inf = False
328
- try :
329
- is_element_inf = np .isinf (value )
330
- except TypeError :
331
- is_element_inf = False
332
- is_inf_in_record [i ] = is_element_inf
333
-
334
- return np .any (is_inf_in_record )
335
-
336
-
337
- def _isna_recarray_dtype (
338
- values : np .rec .recarray , inf_as_na : bool
339
- ) -> npt .NDArray [np .bool_ ]:
275
+ def _isna_recarray_dtype (values : np .rec .recarray ) -> npt .NDArray [np .bool_ ]:
340
276
result = np .zeros (values .shape , dtype = bool )
341
277
for i , record in enumerate (values ):
342
278
record_as_array = np .array (record .tolist ())
343
279
does_record_contain_nan = isna_all (record_as_array )
344
- does_record_contain_inf = False
345
- if inf_as_na :
346
- does_record_contain_inf = bool (_has_record_inf_value (record_as_array ))
347
- result [i ] = np .any (
348
- np .logical_or (does_record_contain_nan , does_record_contain_inf )
349
- )
280
+ result [i ] = np .any (does_record_contain_nan )
350
281
351
282
return result
352
283
@@ -788,7 +719,7 @@ def isna_all(arr: ArrayLike) -> bool:
788
719
789
720
dtype = arr .dtype
790
721
if lib .is_np_dtype (dtype , "f" ):
791
- checker = nan_checker
722
+ checker = np . isnan
792
723
793
724
elif (lib .is_np_dtype (dtype , "mM" )) or isinstance (
794
725
dtype , (DatetimeTZDtype , PeriodDtype )
@@ -800,9 +731,7 @@ def isna_all(arr: ArrayLike) -> bool:
800
731
else :
801
732
# error: Incompatible types in assignment (expression has type "Callable[[Any],
802
733
# Any]", variable has type "ufunc")
803
- checker = lambda x : _isna_array ( # type: ignore[assignment]
804
- x , inf_as_na = INF_AS_NA
805
- )
734
+ checker = _isna_array # type: ignore[assignment]
806
735
807
736
return all (
808
737
checker (arr [i : i + chunk_len ]).all () for i in range (0 , total_len , chunk_len )
0 commit comments