1
1
"""
2
2
missing types & inference
3
3
"""
4
+ from functools import partial
5
+
4
6
import numpy as np
5
7
6
8
from pandas ._config import get_option
@@ -124,61 +126,44 @@ def isna(obj):
124
126
isnull = isna
125
127
126
128
127
- def _isna_new (obj ):
128
-
129
- if is_scalar (obj ):
130
- return libmissing .checknull (obj )
131
- # hack (for now) because MI registers as ndarray
132
- elif isinstance (obj , ABCMultiIndex ):
133
- raise NotImplementedError ("isna is not defined for MultiIndex" )
134
- elif isinstance (obj , type ):
135
- return False
136
- elif isinstance (obj , (ABCSeries , np .ndarray , ABCIndexClass , ABCExtensionArray )):
137
- return _isna_ndarraylike (obj , old = False )
138
- elif isinstance (obj , ABCDataFrame ):
139
- return obj .isna ()
140
- elif isinstance (obj , list ):
141
- return _isna_ndarraylike (np .asarray (obj , dtype = object ), old = False )
142
- elif hasattr (obj , "__array__" ):
143
- return _isna_ndarraylike (np .asarray (obj ), old = False )
144
- else :
145
- return False
146
-
147
-
148
- def _isna_old (obj ):
129
+ def _isna (obj , inf_as_na : bool = False ):
149
130
"""
150
- Detect missing values, treating None, NaN, INF, -INF as null.
131
+ Detect missing values, treating None, NaN or NA as null. Infinite
132
+ values will also be treated as null if inf_as_na is True.
151
133
152
134
Parameters
153
135
----------
154
- arr: ndarray or object value
136
+ obj: ndarray or object value
137
+ Input array or scalar value.
138
+ inf_as_na: bool
139
+ Whether to treat infinity as null.
155
140
156
141
Returns
157
142
-------
158
143
boolean ndarray or boolean
159
144
"""
160
145
if is_scalar (obj ):
161
- return libmissing .checknull_old (obj )
146
+ if inf_as_na :
147
+ return libmissing .checknull_old (obj )
148
+ else :
149
+ return libmissing .checknull (obj )
162
150
# hack (for now) because MI registers as ndarray
163
151
elif isinstance (obj , ABCMultiIndex ):
164
152
raise NotImplementedError ("isna is not defined for MultiIndex" )
165
153
elif isinstance (obj , type ):
166
154
return False
167
155
elif isinstance (obj , (ABCSeries , np .ndarray , ABCIndexClass , ABCExtensionArray )):
168
- return _isna_ndarraylike (obj , old = True )
156
+ return _isna_ndarraylike (obj , inf_as_na = inf_as_na )
169
157
elif isinstance (obj , ABCDataFrame ):
170
158
return obj .isna ()
171
159
elif isinstance (obj , list ):
172
- return _isna_ndarraylike (np .asarray (obj , dtype = object ), old = True )
160
+ return _isna_ndarraylike (np .asarray (obj , dtype = object ), inf_as_na = inf_as_na )
173
161
elif hasattr (obj , "__array__" ):
174
- return _isna_ndarraylike (np .asarray (obj ), old = True )
162
+ return _isna_ndarraylike (np .asarray (obj ), inf_as_na = inf_as_na )
175
163
else :
176
164
return False
177
165
178
166
179
- _isna = _isna_new
180
-
181
-
182
167
def _use_inf_as_na (key ):
183
168
"""
184
169
Option change callback for na/inf behaviour.
@@ -200,22 +185,19 @@ def _use_inf_as_na(key):
200
185
* https://stackoverflow.com/questions/4859217/
201
186
programmatically-creating-variables-in-python/4859312#4859312
202
187
"""
203
- flag = get_option (key )
204
- if flag :
205
- globals ()["_isna" ] = _isna_old
206
- else :
207
- globals ()["_isna" ] = _isna_new
188
+ inf_as_na = get_option (key )
189
+ globals ()["_isna" ] = partial (_isna , inf_as_na = inf_as_na )
208
190
209
191
210
- def _isna_ndarraylike (obj , old : bool = False ):
192
+ def _isna_ndarraylike (obj , inf_as_na : bool = False ):
211
193
"""
212
194
Return an array indicating which values of the input array are NaN / NA.
213
195
214
196
Parameters
215
197
----------
216
198
obj: array-like
217
199
The input array whose elements are to be checked.
218
- old : bool
200
+ inf_as_na : bool
219
201
Whether or not to treat infinite values as NA.
220
202
221
203
Returns
@@ -227,17 +209,17 @@ def _isna_ndarraylike(obj, old: bool = False):
227
209
dtype = values .dtype
228
210
229
211
if is_extension_array_dtype (dtype ):
230
- if old :
212
+ if inf_as_na :
231
213
result = values .isna () | (values == - np .inf ) | (values == np .inf )
232
214
else :
233
215
result = values .isna ()
234
216
elif is_string_dtype (dtype ):
235
- result = _isna_string_dtype (values , dtype , old = old )
217
+ result = _isna_string_dtype (values , dtype , inf_as_na = inf_as_na )
236
218
elif needs_i8_conversion (dtype ):
237
219
# this is the NaT pattern
238
220
result = values .view ("i8" ) == iNaT
239
221
else :
240
- if old :
222
+ if inf_as_na :
241
223
result = ~ np .isfinite (values )
242
224
else :
243
225
result = np .isnan (values )
@@ -249,15 +231,17 @@ def _isna_ndarraylike(obj, old: bool = False):
249
231
return result
250
232
251
233
252
- def _isna_string_dtype (values : np .ndarray , dtype : np .dtype , old : bool ) -> np .ndarray :
234
+ def _isna_string_dtype (
235
+ values : np .ndarray , dtype : np .dtype , inf_as_na : bool
236
+ ) -> np .ndarray :
253
237
# Working around NumPy ticket 1542
254
238
shape = values .shape
255
239
256
240
if is_string_like_dtype (dtype ):
257
241
result = np .zeros (values .shape , dtype = bool )
258
242
else :
259
243
result = np .empty (shape , dtype = bool )
260
- if old :
244
+ if inf_as_na :
261
245
vec = libmissing .isnaobj_old (values .ravel ())
262
246
else :
263
247
vec = libmissing .isnaobj (values .ravel ())
0 commit comments