forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgos_common_helper.pxi.in
165 lines (135 loc) · 4.66 KB
/
algos_common_helper.pxi.in
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
"""
Template for each `dtype` helper function using 1-d template
# 1-d template
- map_indices
- pad
- pad_1d
- pad_2d
- backfill
- backfill_1d
- backfill_2d
- is_monotonic
- arrmap
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
"""
{{py:
# name, c_type, dest_type, dest_dtype
dtypes = [('float64', 'float64_t', 'float64_t', 'np.float64'),
('float32', 'float32_t', 'float32_t', 'np.float32'),
('int8', 'int8_t', 'float32_t', 'np.float32'),
('int16', 'int16_t', 'float32_t', 'np.float32'),
('int32', 'int32_t', 'float64_t', 'np.float64'),
('int64', 'int64_t', 'float64_t', 'np.float64')]
def get_dispatch(dtypes):
for name, c_type, dest_type, dest_dtype, in dtypes:
dest_type2 = dest_type
dest_type = dest_type.replace('_t', '')
yield name, c_type, dest_type, dest_type2, dest_dtype
}}
{{for name, c_type, dest_type, dest_type2, dest_dtype
in get_dispatch(dtypes)}}
@cython.boundscheck(False)
@cython.wraparound(False)
def diff_2d_{{name}}(ndarray[{{c_type}}, ndim=2] arr,
ndarray[{{dest_type2}}, ndim=2] out,
Py_ssize_t periods, int axis):
cdef:
Py_ssize_t i, j, sx, sy
sx, sy = (<object> arr).shape
if arr.flags.f_contiguous:
if axis == 0:
if periods >= 0:
start, stop = periods, sx
else:
start, stop = 0, sx + periods
for j in range(sy):
for i in range(start, stop):
out[i, j] = arr[i, j] - arr[i - periods, j]
else:
if periods >= 0:
start, stop = periods, sy
else:
start, stop = 0, sy + periods
for j in range(start, stop):
for i in range(sx):
out[i, j] = arr[i, j] - arr[i, j - periods]
else:
if axis == 0:
if periods >= 0:
start, stop = periods, sx
else:
start, stop = 0, sx + periods
for i in range(start, stop):
for j in range(sy):
out[i, j] = arr[i, j] - arr[i - periods, j]
else:
if periods >= 0:
start, stop = periods, sy
else:
start, stop = 0, sy + periods
for i in range(sx):
for j in range(start, stop):
out[i, j] = arr[i, j] - arr[i, j - periods]
def put2d_{{name}}_{{dest_type}}(ndarray[{{c_type}}, ndim=2, cast=True] values,
ndarray[int64_t] indexer, Py_ssize_t loc,
ndarray[{{dest_type2}}] out):
cdef:
Py_ssize_t i, j, k
k = len(values)
for j in range(k):
i = indexer[j]
out[i] = values[j, loc]
{{endfor}}
#----------------------------------------------------------------------
# ensure_dtype
#----------------------------------------------------------------------
cdef int PLATFORM_INT = (<ndarray> np.arange(0, dtype=np.intp)).descr.type_num
def ensure_platform_int(object arr):
# GH3033, GH1392
# platform int is the size of the int pointer, e.g. np.intp
if util.is_array(arr):
if (<ndarray> arr).descr.type_num == PLATFORM_INT:
return arr
else:
return arr.astype(np.intp)
else:
return np.array(arr, dtype=np.intp)
def ensure_object(object arr):
if util.is_array(arr):
if (<ndarray> arr).descr.type_num == NPY_OBJECT:
return arr
else:
return arr.astype(np.object_)
elif hasattr(arr, '_box_values_as_index'):
return arr._box_values_as_index()
else:
return np.array(arr, dtype=np.object_)
{{py:
# name, c_type, dtype
dtypes = [('float64', 'FLOAT64', 'float64'),
('float32', 'FLOAT32', 'float32'),
('int64', 'INT64', 'int64'),
('int32', 'INT32', 'int32'),
('int16', 'INT16', 'int16'),
('int8', 'INT8', 'int8'),
('uint64', 'UINT64', 'uint64'),
('uint32', 'UINT32', 'uint32'),
('uint16', 'UINT16', 'uint16'),
('uint8', 'UINT8', 'uint8'),
# ('platform_int', 'INT', 'int_'),
# ('object', 'OBJECT', 'object_'),
]
def get_dispatch(dtypes):
for name, c_type, dtype in dtypes:
yield name, c_type, dtype
}}
{{for name, c_type, dtype in get_dispatch(dtypes)}}
def ensure_{{name}}(object arr, copy=True):
if util.is_array(arr):
if (<ndarray> arr).descr.type_num == NPY_{{c_type}}:
return arr
else:
return arr.astype(np.{{dtype}}, copy=copy)
else:
return np.array(arr, dtype=np.{{dtype}})
{{endfor}}