16
16
ensure_object ,
17
17
is_bool_dtype ,
18
18
is_complex_dtype ,
19
- is_datetime64_dtype ,
20
- is_datetime64tz_dtype ,
21
19
is_datetimelike_v_numeric ,
22
20
is_dtype_equal ,
23
21
is_extension_array_dtype ,
24
22
is_float_dtype ,
25
23
is_integer_dtype ,
26
24
is_object_dtype ,
27
- is_period_dtype ,
28
25
is_scalar ,
29
26
is_string_dtype ,
30
27
is_string_like_dtype ,
31
- is_timedelta64_dtype ,
32
28
needs_i8_conversion ,
33
29
pandas_dtype ,
34
30
)
35
31
from pandas .core .dtypes .generic import (
36
32
ABCDataFrame ,
37
- ABCDatetimeArray ,
38
33
ABCExtensionArray ,
39
34
ABCIndexClass ,
40
35
ABCMultiIndex ,
41
36
ABCSeries ,
42
- ABCTimedeltaArray ,
43
37
)
44
38
from pandas .core .dtypes .inference import is_list_like
45
39
@@ -139,17 +133,7 @@ def _isna_new(obj):
139
133
raise NotImplementedError ("isna is not defined for MultiIndex" )
140
134
elif isinstance (obj , type ):
141
135
return False
142
- elif isinstance (
143
- obj ,
144
- (
145
- ABCSeries ,
146
- np .ndarray ,
147
- ABCIndexClass ,
148
- ABCExtensionArray ,
149
- ABCDatetimeArray ,
150
- ABCTimedeltaArray ,
151
- ),
152
- ):
136
+ elif isinstance (obj , (ABCSeries , np .ndarray , ABCIndexClass , ABCExtensionArray )):
153
137
return _isna_ndarraylike (obj )
154
138
elif isinstance (obj , ABCDataFrame ):
155
139
return obj .isna ()
@@ -158,7 +142,7 @@ def _isna_new(obj):
158
142
elif hasattr (obj , "__array__" ):
159
143
return _isna_ndarraylike (np .asarray (obj ))
160
144
else :
161
- return obj is None
145
+ return False
162
146
163
147
164
148
def _isna_old (obj ):
@@ -189,7 +173,7 @@ def _isna_old(obj):
189
173
elif hasattr (obj , "__array__" ):
190
174
return _isna_ndarraylike_old (np .asarray (obj ))
191
175
else :
192
- return obj is None
176
+ return False
193
177
194
178
195
179
_isna = _isna_new
@@ -224,37 +208,14 @@ def _use_inf_as_na(key):
224
208
225
209
226
210
def _isna_ndarraylike (obj ):
227
- is_extension = is_extension_array_dtype (obj )
228
-
229
- if not is_extension :
230
- # Avoid accessing `.values` on things like
231
- # PeriodIndex, which may be expensive.
232
- values = getattr (obj , "_values" , obj )
233
- else :
234
- values = obj
235
-
211
+ is_extension = is_extension_array_dtype (obj .dtype )
212
+ values = getattr (obj , "_values" , obj )
236
213
dtype = values .dtype
237
214
238
215
if is_extension :
239
- if isinstance (obj , (ABCIndexClass , ABCSeries )):
240
- values = obj ._values
241
- else :
242
- values = obj
243
216
result = values .isna ()
244
- elif isinstance (obj , ABCDatetimeArray ):
245
- return obj .isna ()
246
217
elif is_string_dtype (dtype ):
247
- # Working around NumPy ticket 1542
248
- shape = values .shape
249
-
250
- if is_string_like_dtype (dtype ):
251
- # object array of strings
252
- result = np .zeros (values .shape , dtype = bool )
253
- else :
254
- # object array of non-strings
255
- result = np .empty (shape , dtype = bool )
256
- vec = libmissing .isnaobj (values .ravel ())
257
- result [...] = vec .reshape (shape )
218
+ result = _isna_string_dtype (values , dtype , old = False )
258
219
259
220
elif needs_i8_conversion (dtype ):
260
221
# this is the NaT pattern
@@ -274,17 +235,9 @@ def _isna_ndarraylike_old(obj):
274
235
dtype = values .dtype
275
236
276
237
if is_string_dtype (dtype ):
277
- # Working around NumPy ticket 1542
278
- shape = values .shape
279
-
280
- if is_string_like_dtype (dtype ):
281
- result = np .zeros (values .shape , dtype = bool )
282
- else :
283
- result = np .empty (shape , dtype = bool )
284
- vec = libmissing .isnaobj_old (values .ravel ())
285
- result [:] = vec .reshape (shape )
238
+ result = _isna_string_dtype (values , dtype , old = True )
286
239
287
- elif is_datetime64_dtype (dtype ):
240
+ elif needs_i8_conversion (dtype ):
288
241
# this is the NaT pattern
289
242
result = values .view ("i8" ) == iNaT
290
243
else :
@@ -297,6 +250,24 @@ def _isna_ndarraylike_old(obj):
297
250
return result
298
251
299
252
253
+ def _isna_string_dtype (values : np .ndarray , dtype : np .dtype , old : bool ) -> np .ndarray :
254
+ # Working around NumPy ticket 1542
255
+ shape = values .shape
256
+
257
+ if is_string_like_dtype (dtype ):
258
+ result = np .zeros (values .shape , dtype = bool )
259
+ else :
260
+ result = np .empty (shape , dtype = bool )
261
+ if old :
262
+ vec = libmissing .isnaobj_old (values .ravel ())
263
+ else :
264
+ vec = libmissing .isnaobj (values .ravel ())
265
+
266
+ result [...] = vec .reshape (shape )
267
+
268
+ return result
269
+
270
+
300
271
def notna (obj ):
301
272
"""
302
273
Detect non-missing values for an array-like object.
@@ -556,12 +527,7 @@ def na_value_for_dtype(dtype, compat: bool = True):
556
527
557
528
if is_extension_array_dtype (dtype ):
558
529
return dtype .na_value
559
- if (
560
- is_datetime64_dtype (dtype )
561
- or is_datetime64tz_dtype (dtype )
562
- or is_timedelta64_dtype (dtype )
563
- or is_period_dtype (dtype )
564
- ):
530
+ if needs_i8_conversion (dtype ):
565
531
return NaT
566
532
elif is_float_dtype (dtype ):
567
533
return np .nan
0 commit comments