44from __future__ import annotations
55
66from decimal import Decimal
7- from functools import partial
87from typing import (
98 TYPE_CHECKING ,
109 overload ,
1312
1413import numpy as np
1514
16- from pandas ._config import get_option
17-
1815from pandas ._libs import lib
1916import pandas ._libs .missing as libmissing
2017from pandas ._libs .tslibs import (
6461isposinf_scalar = libmissing .isposinf_scalar
6562isneginf_scalar = libmissing .isneginf_scalar
6663
67- nan_checker = np .isnan
68- INF_AS_NA = False
6964_dtype_object = np .dtype ("object" )
7065_dtype_str = np .dtype (str )
7166
@@ -180,95 +175,57 @@ def isna(obj: object) -> bool | npt.NDArray[np.bool_] | NDFrame:
180175isnull = isna
181176
182177
183- def _isna (obj , inf_as_na : bool = False ):
178+ def _isna (obj ):
184179 """
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.
187181
188182 Parameters
189183 ----------
190184 obj: ndarray or object value
191185 Input array or scalar value.
192- inf_as_na: bool
193- Whether to treat infinity as null.
194186
195187 Returns
196188 -------
197189 boolean ndarray or boolean
198190 """
199191 if is_scalar (obj ):
200- return libmissing .checknull (obj , inf_as_na = inf_as_na )
192+ return libmissing .checknull (obj )
201193 elif isinstance (obj , ABCMultiIndex ):
202194 raise NotImplementedError ("isna is not defined for MultiIndex" )
203195 elif isinstance (obj , type ):
204196 return False
205197 elif isinstance (obj , (np .ndarray , ABCExtensionArray )):
206- return _isna_array (obj , inf_as_na = inf_as_na )
198+ return _isna_array (obj )
207199 elif isinstance (obj , ABCIndex ):
208200 # Try to use cached isna, which also short-circuits for integer dtypes
209201 # and avoids materializing RangeIndex._values
210202 if not obj ._can_hold_na :
211203 return obj .isna ()
212- return _isna_array (obj ._values , inf_as_na = inf_as_na )
204+ return _isna_array (obj ._values )
213205
214206 elif isinstance (obj , ABCSeries ):
215- result = _isna_array (obj ._values , inf_as_na = inf_as_na )
207+ result = _isna_array (obj ._values )
216208 # box
217209 result = obj ._constructor (result , index = obj .index , name = obj .name , copy = False )
218210 return result
219211 elif isinstance (obj , ABCDataFrame ):
220212 return obj .isna ()
221213 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 ))
223215 elif hasattr (obj , "__array__" ):
224- return _isna_array (np .asarray (obj ), inf_as_na = inf_as_na )
216+ return _isna_array (np .asarray (obj ))
225217 else :
226218 return False
227219
228220
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 :
263222 """
264223 Return an array indicating which values of the input array are NaN / NA.
265224
266225 Parameters
267226 ----------
268227 obj: ndarray or ExtensionArray
269228 The input array whose elements are to be checked.
270- inf_as_na: bool
271- Whether or not to treat infinite values as NA.
272229
273230 Returns
274231 -------
@@ -280,73 +237,47 @@ def _isna_array(
280237
281238 if not isinstance (values , np .ndarray ):
282239 # 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]
290244 elif isinstance (values , np .rec .recarray ):
291245 # GH 48526
292- result = _isna_recarray_dtype (values , inf_as_na = inf_as_na )
246+ result = _isna_recarray_dtype (values )
293247 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 )
295249 elif dtype .kind in "mM" :
296250 # this is the NaT pattern
297251 result = values .view ("i8" ) == iNaT
298252 else :
299- if inf_as_na :
300- result = ~ np .isfinite (values )
301- else :
302- result = np .isnan (values )
253+ result = np .isnan (values )
303254
304255 return result
305256
306257
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_ ]:
308259 # Working around NumPy ticket 1542
309260 dtype = values .dtype
310261
311262 if dtype .kind in ("S" , "U" ):
312263 result = np .zeros (values .shape , dtype = bool )
313264 else :
314265 if values .ndim in {1 , 2 }:
315- result = libmissing .isnaobj (values , inf_as_na = inf_as_na )
266+ result = libmissing .isnaobj (values )
316267 else :
317268 # 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 ())
319270 result = result .reshape (values .shape )
320271
321272 return result
322273
323274
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_ ]:
340276 result = np .zeros (values .shape , dtype = bool )
341277 for i , record in enumerate (values ):
342278 record_as_array = np .array (record .tolist ())
343279 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 )
350281
351282 return result
352283
@@ -788,7 +719,7 @@ def isna_all(arr: ArrayLike) -> bool:
788719
789720 dtype = arr .dtype
790721 if lib .is_np_dtype (dtype , "f" ):
791- checker = nan_checker
722+ checker = np . isnan
792723
793724 elif (lib .is_np_dtype (dtype , "mM" )) or isinstance (
794725 dtype , (DatetimeTZDtype , PeriodDtype )
@@ -800,9 +731,7 @@ def isna_all(arr: ArrayLike) -> bool:
800731 else :
801732 # error: Incompatible types in assignment (expression has type "Callable[[Any],
802733 # 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]
806735
807736 return all (
808737 checker (arr [i : i + chunk_len ]).all () for i in range (0 , total_len , chunk_len )
0 commit comments