forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissing.pyx
320 lines (263 loc) · 7.69 KB
/
missing.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# -*- coding: utf-8 -*-
import cython
from cython import Py_ssize_t
import numpy as np
cimport numpy as cnp
from numpy cimport ndarray, int64_t, uint8_t, float64_t
cnp.import_array()
cimport pandas._libs.util as util
from pandas._libs.tslibs.np_datetime cimport (
get_timedelta64_value, get_datetime64_value)
from pandas._libs.tslibs.nattype cimport checknull_with_nat
from pandas._libs.tslibs.nattype import NaT
cdef float64_t INF = <float64_t>np.inf
cdef float64_t NEGINF = -INF
cdef int64_t NPY_NAT = util.get_nat()
cdef inline bint _check_all_nulls(object val):
""" utility to check if a value is any type of null """
res: bint
if isinstance(val, (float, complex)):
res = val != val
elif val is NaT:
res = 1
elif val is None:
res = 1
elif util.is_datetime64_object(val):
res = get_datetime64_value(val) == NPY_NAT
elif util.is_timedelta64_object(val):
res = get_timedelta64_value(val) == NPY_NAT
else:
res = 0
return res
cpdef bint checknull(object val):
"""
Return boolean describing of the input is NA-like, defined here as any
of:
- None
- nan
- NaT
- np.datetime64 representation of NaT
- np.timedelta64 representation of NaT
Parameters
----------
val : object
Returns
-------
result : bool
Notes
-----
The difference between `checknull` and `checknull_old` is that `checknull`
does *not* consider INF or NEGINF to be NA.
"""
if util.is_float_object(val) or util.is_complex_object(val):
return val != val # and val != INF and val != NEGINF
elif util.is_datetime64_object(val):
return get_datetime64_value(val) == NPY_NAT
elif val is NaT:
return True
elif util.is_timedelta64_object(val):
return get_timedelta64_value(val) == NPY_NAT
elif util.is_array(val):
return False
else:
return val is None or util.is_nan(val)
cpdef bint checknull_old(object val):
"""
Return boolean describing of the input is NA-like, defined here as any
of:
- None
- nan
- INF
- NEGINF
- NaT
- np.datetime64 representation of NaT
- np.timedelta64 representation of NaT
Parameters
----------
val : object
Returns
-------
result : bool
Notes
-----
The difference between `checknull` and `checknull_old` is that `checknull`
does *not* consider INF or NEGINF to be NA.
"""
if util.is_float_object(val) or util.is_complex_object(val):
return val != val or val == INF or val == NEGINF
elif util.is_datetime64_object(val):
return get_datetime64_value(val) == NPY_NAT
elif val is NaT:
return True
elif util.is_timedelta64_object(val):
return get_timedelta64_value(val) == NPY_NAT
elif util.is_array(val):
return False
else:
return val is None or util.is_nan(val)
cdef inline bint _check_none_nan_inf_neginf(object val):
try:
return val is None or (isinstance(val, float) and
(val != val or val == INF or val == NEGINF))
except ValueError:
return False
@cython.wraparound(False)
@cython.boundscheck(False)
cpdef ndarray[uint8_t] isnaobj(ndarray arr):
"""
Return boolean mask denoting which elements of a 1-D array are na-like,
according to the criteria defined in `_check_all_nulls`:
- None
- nan
- NaT
- np.datetime64 representation of NaT
- np.timedelta64 representation of NaT
Parameters
----------
arr : ndarray
Returns
-------
result : ndarray (dtype=np.bool_)
"""
cdef:
Py_ssize_t i, n
object val
ndarray[uint8_t] result
assert arr.ndim == 1, "'arr' must be 1-D."
n = len(arr)
result = np.empty(n, dtype=np.uint8)
for i in range(n):
val = arr[i]
result[i] = _check_all_nulls(val)
return result.view(np.bool_)
@cython.wraparound(False)
@cython.boundscheck(False)
def isnaobj_old(ndarray arr):
"""
Return boolean mask denoting which elements of a 1-D array are na-like,
defined as being any of:
- None
- nan
- INF
- NEGINF
- NaT
Parameters
----------
arr : ndarray
Returns
-------
result : ndarray (dtype=np.bool_)
"""
cdef:
Py_ssize_t i, n
object val
ndarray[uint8_t] result
assert arr.ndim == 1, "'arr' must be 1-D."
n = len(arr)
result = np.zeros(n, dtype=np.uint8)
for i in range(n):
val = arr[i]
result[i] = val is NaT or _check_none_nan_inf_neginf(val)
return result.view(np.bool_)
@cython.wraparound(False)
@cython.boundscheck(False)
def isnaobj2d(ndarray arr):
"""
Return boolean mask denoting which elements of a 2-D array are na-like,
according to the criteria defined in `checknull`:
- None
- nan
- NaT
- np.datetime64 representation of NaT
- np.timedelta64 representation of NaT
Parameters
----------
arr : ndarray
Returns
-------
result : ndarray (dtype=np.bool_)
Notes
-----
The difference between `isnaobj2d` and `isnaobj2d_old` is that `isnaobj2d`
does *not* consider INF or NEGINF to be NA.
"""
cdef:
Py_ssize_t i, j, n, m
object val
ndarray[uint8_t, ndim=2] result
assert arr.ndim == 2, "'arr' must be 2-D."
n, m = (<object>arr).shape
result = np.zeros((n, m), dtype=np.uint8)
for i in range(n):
for j in range(m):
val = arr[i, j]
if checknull(val):
result[i, j] = 1
return result.view(np.bool_)
@cython.wraparound(False)
@cython.boundscheck(False)
def isnaobj2d_old(ndarray arr):
"""
Return boolean mask denoting which elements of a 2-D array are na-like,
according to the criteria defined in `checknull_old`:
- None
- nan
- INF
- NEGINF
- NaT
- np.datetime64 representation of NaT
- np.timedelta64 representation of NaT
Parameters
----------
arr : ndarray
Returns
-------
result : ndarray (dtype=np.bool_)
Notes
-----
The difference between `isnaobj2d` and `isnaobj2d_old` is that `isnaobj2d`
does *not* consider INF or NEGINF to be NA.
"""
cdef:
Py_ssize_t i, j, n, m
object val
ndarray[uint8_t, ndim=2] result
assert arr.ndim == 2, "'arr' must be 2-D."
n, m = (<object>arr).shape
result = np.zeros((n, m), dtype=np.uint8)
for i in range(n):
for j in range(m):
val = arr[i, j]
if checknull_old(val):
result[i, j] = 1
return result.view(np.bool_)
def isposinf_scalar(val: object) -> bool:
if util.is_float_object(val) and val == INF:
return True
else:
return False
def isneginf_scalar(val: object) -> bool:
if util.is_float_object(val) and val == NEGINF:
return True
else:
return False
cdef inline bint is_null_datetime64(v):
# determine if we have a null for a datetime (or integer versions),
# excluding np.timedelta64('nat')
if checknull_with_nat(v):
return True
elif util.is_datetime64_object(v):
return v.view('int64') == NPY_NAT
return False
cdef inline bint is_null_timedelta64(v):
# determine if we have a null for a timedelta (or integer versions),
# excluding np.datetime64('nat')
if checknull_with_nat(v):
return True
elif util.is_timedelta64_object(v):
return v.view('int64') == NPY_NAT
return False
cdef inline bint is_null_period(v):
# determine if we have a null for a Period (or integer versions),
# excluding np.datetime64('nat') and np.timedelta64('nat')
return checknull_with_nat(v)