22
22
PyTuple_New)
23
23
from cpython cimport PyFloat_Check
24
24
cimport cpython
25
+ cdef double NAN = <double> np.nan
25
26
26
27
import numpy as np
27
28
isnan = np.isnan
@@ -881,7 +882,7 @@ def group_add_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
881
882
Only aggregates on axis=0
882
883
'''
883
884
cdef:
884
- Py_ssize_t i, j, N, K, lab
885
+ Py_ssize_t i, j, N, K, lab, lcounts = len(counts)
885
886
%(dest_type2)s val, count
886
887
ndarray[%(dest_type2)s, ndim=2] sumx, nobs
887
888
@@ -894,39 +895,45 @@ def group_add_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
894
895
N, K = (<object> values).shape
895
896
896
897
if K > 1:
897
- for i in range(N):
898
- lab = labels[i]
899
- if lab < 0:
900
- continue
901
898
902
- counts[lab] += 1
903
- for j in range(K):
904
- val = values[i, j]
899
+ with nogil:
900
+ for i in range(N):
901
+ lab = labels[i]
902
+ if lab < 0:
903
+ continue
904
+
905
+ counts[lab] += 1
906
+ for j in range(K):
907
+ val = values[i, j]
908
+
909
+ # not nan
910
+ if val == val:
911
+ nobs[lab, j] += 1
912
+ sumx[lab, j] += val
905
913
906
- # not nan
907
- if val == val:
908
- nobs[lab, j] += 1
909
- sumx[lab, j] += val
910
914
else:
911
- for i in range(N):
912
- lab = labels[i]
913
- if lab < 0:
914
- continue
915
915
916
- counts[lab] += 1
917
- val = values[i, 0]
916
+ with nogil:
917
+ for i in range(N):
918
+ lab = labels[i]
919
+ if lab < 0:
920
+ continue
918
921
919
- # not nan
920
- if val == val:
921
- nobs[lab, 0] += 1
922
- sumx[lab, 0] += val
922
+ counts[lab] += 1
923
+ val = values[i, 0]
923
924
924
- for i in range(len(counts)):
925
- for j in range(K):
926
- if nobs[i, j] == 0:
927
- out[i, j] = nan
928
- else:
929
- out[i, j] = sumx[i, j]
925
+ # not nan
926
+ if val == val:
927
+ nobs[lab, 0] += 1
928
+ sumx[lab, 0] += val
929
+
930
+ with nogil:
931
+ for i in range(lcounts):
932
+ for j in range(K):
933
+ if nobs[i, j] == 0:
934
+ out[i, j] = NAN
935
+ else:
936
+ out[i, j] = sumx[i, j]
930
937
"""
931
938
932
939
group_add_bin_template = """@cython.boundscheck(False)
@@ -982,7 +989,7 @@ def group_add_bin_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
982
989
for i in range(ngroups):
983
990
for j in range(K):
984
991
if nobs[i, j] == 0:
985
- out[i, j] = nan
992
+ out[i, j] = NAN
986
993
else:
987
994
out[i, j] = sumx[i, j]
988
995
"""
@@ -1040,7 +1047,7 @@ def group_prod_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1040
1047
for i in range(len(counts)):
1041
1048
for j in range(K):
1042
1049
if nobs[i, j] == 0:
1043
- out[i, j] = nan
1050
+ out[i, j] = NAN
1044
1051
else:
1045
1052
out[i, j] = prodx[i, j]
1046
1053
"""
@@ -1098,7 +1105,7 @@ def group_prod_bin_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1098
1105
for i in range(ngroups):
1099
1106
for j in range(K):
1100
1107
if nobs[i, j] == 0:
1101
- out[i, j] = nan
1108
+ out[i, j] = NAN
1102
1109
else:
1103
1110
out[i, j] = prodx[i, j]
1104
1111
"""
@@ -1160,7 +1167,7 @@ def group_var_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1160
1167
for j in range(K):
1161
1168
ct = nobs[i, j]
1162
1169
if ct < 2:
1163
- out[i, j] = nan
1170
+ out[i, j] = NAN
1164
1171
else:
1165
1172
out[i, j] = ((ct * sumxx[i, j] - sumx[i, j] * sumx[i, j]) /
1166
1173
(ct * ct - ct))
@@ -1223,7 +1230,7 @@ def group_var_bin_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1223
1230
for j in range(K):
1224
1231
ct = nobs[i, j]
1225
1232
if ct < 2:
1226
- out[i, j] = nan
1233
+ out[i, j] = NAN
1227
1234
else:
1228
1235
out[i, j] = ((ct * sumxx[i, j] - sumx[i, j] * sumx[i, j]) /
1229
1236
(ct * ct - ct))
@@ -1608,7 +1615,7 @@ def group_mean_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1608
1615
for j in range(K):
1609
1616
count = nobs[i, j]
1610
1617
if nobs[i, j] == 0:
1611
- out[i, j] = nan
1618
+ out[i, j] = NAN
1612
1619
else:
1613
1620
out[i, j] = sumx[i, j] / count
1614
1621
"""
@@ -1663,7 +1670,7 @@ def group_mean_bin_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1663
1670
for j in range(K):
1664
1671
count = nobs[i, j]
1665
1672
if count == 0:
1666
- out[i, j] = nan
1673
+ out[i, j] = NAN
1667
1674
else:
1668
1675
out[i, j] = sumx[i, j] / count
1669
1676
"""
@@ -1680,7 +1687,7 @@ def group_ohlc_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1680
1687
cdef:
1681
1688
Py_ssize_t i, j, N, K, ngroups, b
1682
1689
%(dest_type2)s val, count
1683
- %(dest_type2)s vopen, vhigh, vlow, vclose, NA
1690
+ %(dest_type2)s vopen, vhigh, vlow, vclose
1684
1691
bint got_first = 0
1685
1692
1686
1693
if bins[len(bins) - 1] == len(values):
@@ -1693,8 +1700,6 @@ def group_ohlc_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1693
1700
if out.shape[1] != 4:
1694
1701
raise ValueError('Output array must have 4 columns')
1695
1702
1696
- NA = np.nan
1697
-
1698
1703
b = 0
1699
1704
if K > 1:
1700
1705
raise NotImplementedError("Argument 'values' must have only "
@@ -1703,10 +1708,10 @@ def group_ohlc_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1703
1708
for i in range(N):
1704
1709
while b < ngroups - 1 and i >= bins[b]:
1705
1710
if not got_first:
1706
- out[b, 0] = NA
1707
- out[b, 1] = NA
1708
- out[b, 2] = NA
1709
- out[b, 3] = NA
1711
+ out[b, 0] = NAN
1712
+ out[b, 1] = NAN
1713
+ out[b, 2] = NAN
1714
+ out[b, 3] = NAN
1710
1715
else:
1711
1716
out[b, 0] = vopen
1712
1717
out[b, 1] = vhigh
@@ -1733,10 +1738,10 @@ def group_ohlc_%(name)s(ndarray[%(dest_type2)s, ndim=2] out,
1733
1738
vclose = val
1734
1739
1735
1740
if not got_first:
1736
- out[b, 0] = NA
1737
- out[b, 1] = NA
1738
- out[b, 2] = NA
1739
- out[b, 3] = NA
1741
+ out[b, 0] = NAN
1742
+ out[b, 1] = NAN
1743
+ out[b, 2] = NAN
1744
+ out[b, 3] = NAN
1740
1745
else:
1741
1746
out[b, 0] = vopen
1742
1747
out[b, 1] = vhigh
@@ -2337,16 +2342,16 @@ def generate_put_template(template, use_ints=True, use_floats=True,
2337
2342
def generate_put_min_max_template (template , use_ints = True , use_floats = True ,
2338
2343
use_objects = False , use_datelikes = False ):
2339
2344
floats_list = [
2340
- ('float64' , 'float64_t' , 'nan ' , 'np.inf' ),
2341
- ('float32' , 'float32_t' , 'nan ' , 'np.inf' ),
2345
+ ('float64' , 'float64_t' , 'NAN ' , 'np.inf' ),
2346
+ ('float32' , 'float32_t' , 'NAN ' , 'np.inf' ),
2342
2347
]
2343
2348
ints_list = [
2344
2349
('int64' , 'int64_t' , 'iNaT' , _int64_max ),
2345
2350
]
2346
2351
date_like_list = [
2347
2352
('int64' , 'int64_t' , 'iNaT' , _int64_max ),
2348
2353
]
2349
- object_list = [('object' , 'object' , 'nan' , 'np.inf' )]
2354
+ object_list = [('object' , 'object' , 'np. nan' , 'np.inf' )]
2350
2355
function_list = []
2351
2356
if use_floats :
2352
2357
function_list .extend (floats_list )
@@ -2369,16 +2374,16 @@ def generate_put_min_max_template(template, use_ints=True, use_floats=True,
2369
2374
def generate_put_selection_template (template , use_ints = True , use_floats = True ,
2370
2375
use_objects = False , use_datelikes = False ):
2371
2376
floats_list = [
2372
- ('float64' , 'float64_t' , 'float64_t' , 'nan ' ),
2373
- ('float32' , 'float32_t' , 'float32_t' , 'nan ' ),
2377
+ ('float64' , 'float64_t' , 'float64_t' , 'NAN ' ),
2378
+ ('float32' , 'float32_t' , 'float32_t' , 'NAN ' ),
2374
2379
]
2375
2380
ints_list = [
2376
2381
('int64' , 'int64_t' , 'int64_t' , 'iNaT' ),
2377
2382
]
2378
2383
date_like_list = [
2379
2384
('int64' , 'int64_t' , 'int64_t' , 'iNaT' ),
2380
2385
]
2381
- object_list = [('object' , 'object' , 'object' , 'nan' )]
2386
+ object_list = [('object' , 'object' , 'object' , 'np. nan' )]
2382
2387
function_list = []
2383
2388
if use_floats :
2384
2389
function_list .extend (floats_list )
0 commit comments