Skip to content

Commit 9a5d635

Browse files
committed
use more memoryviews
1 parent de87047 commit 9a5d635

File tree

5 files changed

+55
-72
lines changed

5 files changed

+55
-72
lines changed

pandas/_libs/algos.pyx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class NegInfinity(object):
7777
__ge__ = lambda self, other: isinstance(other, NegInfinity)
7878

7979

80-
cpdef ndarray[int64_t, ndim=1] unique_deltas(ndarray[int64_t] arr):
80+
cpdef ndarray[int64_t, ndim=1] unique_deltas(int64_t[:] arr):
8181
"""
8282
Efficiently find the unique first-differences of the given array.
8383
@@ -236,7 +236,7 @@ def nancorr(ndarray[float64_t, ndim=2] mat, bint cov=0, minp=None):
236236
Py_ssize_t i, j, xi, yi, N, K
237237
bint minpv
238238
ndarray[float64_t, ndim=2] result
239-
ndarray[uint8_t, ndim=2] mask
239+
uint8_t[:, :] mask
240240
int64_t nobs = 0
241241
float64_t vx, vy, sumx, sumy, sumxx, sumyy, meanx, meany, divisor
242242

@@ -301,7 +301,7 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1):
301301
ndarray[float64_t, ndim=2] result
302302
ndarray[float64_t, ndim=1] maskedx
303303
ndarray[float64_t, ndim=1] maskedy
304-
ndarray[uint8_t, ndim=2] mask
304+
uint8_t[:, :] mask
305305
int64_t nobs = 0
306306
float64_t vx, vy, sumx, sumxx, sumyy, mean, divisor
307307

@@ -475,10 +475,7 @@ pad_bool = pad["uint8_t"]
475475

476476
@cython.boundscheck(False)
477477
@cython.wraparound(False)
478-
def pad_inplace(algos_t[:] values,
479-
ndarray[uint8_t, cast=True] mask,
480-
# TODO: What does the cast=True mean? If unneeded, use bint[:]?
481-
limit=None):
478+
def pad_inplace(algos_t[:] values, uint8_t[:] mask, limit=None):
482479
cdef:
483480
Py_ssize_t i, N
484481
algos_t val
@@ -522,9 +519,7 @@ pad_inplace_bool = pad_inplace["uint8_t"]
522519

523520
@cython.boundscheck(False)
524521
@cython.wraparound(False)
525-
def pad_2d_inplace(ndarray[algos_t, ndim=2] values,
526-
ndarray[uint8_t, ndim=2] mask,
527-
limit=None):
522+
def pad_2d_inplace(algos_t[:, :] values, uint8_t[:, :] mask, limit=None):
528523
cdef:
529524
Py_ssize_t i, j, N, K
530525
algos_t val
@@ -675,9 +670,7 @@ backfill_bool = backfill["uint8_t"]
675670

676671
@cython.boundscheck(False)
677672
@cython.wraparound(False)
678-
def backfill_inplace(algos_t[:] values,
679-
ndarray[uint8_t, cast=True] mask,
680-
limit=None):
673+
def backfill_inplace(algos_t[:] values, uint8_t[:] mask, limit=None):
681674
cdef:
682675
Py_ssize_t i, N
683676
algos_t val
@@ -721,9 +714,7 @@ backfill_inplace_bool = backfill_inplace["uint8_t"]
721714

722715
@cython.boundscheck(False)
723716
@cython.wraparound(False)
724-
def backfill_2d_inplace(ndarray[algos_t, ndim=2] values,
725-
ndarray[uint8_t, ndim=2] mask,
726-
limit=None):
717+
def backfill_2d_inplace(algos_t[:, :] values, uint8_t[:, :] mask, limit=None):
727718
cdef:
728719
Py_ssize_t i, j, N, K
729720
algos_t val

pandas/_libs/algos_common_helper.pxi.in

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
"""
22
Template for each `dtype` helper function using 1-d template
33

4-
# 1-d template
5-
- pad
6-
- pad_1d
7-
- pad_2d
8-
- backfill
9-
- backfill_1d
10-
- backfill_2d
11-
- is_monotonic
12-
- arrmap
13-
144
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
155
"""
166

@@ -39,7 +29,7 @@ def get_dispatch(dtypes):
3929
@cython.boundscheck(False)
4030
@cython.wraparound(False)
4131
def diff_2d_{{name}}(ndarray[{{c_type}}, ndim=2] arr,
42-
ndarray[{{dest_type}}, ndim=2] out,
32+
{{dest_type}}[:, :] out,
4333
Py_ssize_t periods, int axis):
4434
cdef:
4535
Py_ssize_t i, j, sx, sy
@@ -83,7 +73,7 @@ def diff_2d_{{name}}(ndarray[{{c_type}}, ndim=2] arr,
8373

8474
def put2d_{{name}}_{{dest_name}}(ndarray[{{c_type}}, ndim=2, cast=True] values,
8575
ndarray[int64_t] indexer, Py_ssize_t loc,
86-
ndarray[{{dest_type}}] out):
76+
{{dest_type}}[:, :] out):
8777
cdef:
8878
Py_ssize_t i, j, k
8979

pandas/_libs/groupby.pyx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ cdef inline float64_t kth_smallest_c(float64_t* a,
9999
@cython.boundscheck(False)
100100
@cython.wraparound(False)
101101
def group_median_float64(ndarray[float64_t, ndim=2] out,
102-
ndarray[int64_t] counts,
102+
int64_t[:] counts,
103103
ndarray[float64_t, ndim=2] values,
104-
ndarray[int64_t] labels,
104+
int64_t[:] labels,
105105
Py_ssize_t min_count=-1):
106106
"""
107107
Only aggregates on axis=0
108108
"""
109109
cdef:
110110
Py_ssize_t i, j, N, K, ngroups, size
111-
ndarray[int64_t] _counts
111+
int64_t[:] _counts
112112
ndarray data
113113
float64_t* ptr
114114

@@ -217,7 +217,7 @@ def group_cumsum(numeric[:, :] out,
217217

218218
@cython.boundscheck(False)
219219
@cython.wraparound(False)
220-
def group_shift_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
220+
def group_shift_indexer(int64_t[:] out, int64_t[:] labels,
221221
int ngroups, int periods):
222222
cdef:
223223
Py_ssize_t N, i, j, ii
@@ -269,8 +269,8 @@ def group_shift_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
269269

270270
@cython.wraparound(False)
271271
@cython.boundscheck(False)
272-
def group_fillna_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
273-
ndarray[uint8_t] mask, object direction,
272+
def group_fillna_indexer(int64_t[:] out, ndarray[int64_t] labels,
273+
uint8_t[:] mask, object direction,
274274
int64_t limit):
275275
"""Indexes how to fill values forwards or backwards within a group
276276
@@ -328,9 +328,9 @@ def group_fillna_indexer(ndarray[int64_t] out, ndarray[int64_t] labels,
328328
@cython.boundscheck(False)
329329
@cython.wraparound(False)
330330
def group_any_all(ndarray[uint8_t] out,
331-
ndarray[int64_t] labels,
332-
ndarray[uint8_t] values,
333-
ndarray[uint8_t] mask,
331+
int64_t[:] labels,
332+
uint8_t[:] values,
333+
uint8_t[:] mask,
334334
object val_test,
335335
bint skipna):
336336
"""Aggregated boolean values to show truthfulness of group elements
@@ -353,7 +353,7 @@ def group_any_all(ndarray[uint8_t] out,
353353
The returned values will either be 0 or 1 (False or True, respectively).
354354
"""
355355
cdef:
356-
Py_ssize_t i, N=len(labels)
356+
Py_ssize_t i, N = len(labels)
357357
int64_t lab
358358
uint8_t flag_val
359359

pandas/_libs/groupby_helper.pxi.in

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ def get_dispatch(dtypes):
3030
@cython.wraparound(False)
3131
@cython.boundscheck(False)
3232
def group_add_{{name}}(ndarray[{{c_type}}, ndim=2] out,
33-
ndarray[int64_t] counts,
33+
int64_t[:] counts,
3434
ndarray[{{c_type}}, ndim=2] values,
35-
ndarray[int64_t] labels,
35+
int64_t[:] labels,
3636
Py_ssize_t min_count=0):
3737
"""
3838
Only aggregates on axis=0
3939
"""
4040
cdef:
4141
Py_ssize_t i, j, N, K, lab, ncounts = len(counts)
4242
{{c_type}} val, count
43-
ndarray[{{c_type}}, ndim=2] sumx, nobs
43+
{{c_type}}[:, :] sumx, nobs
4444

4545
if not len(values) == len(labels):
4646
raise AssertionError("len(index) != len(labels)")
@@ -77,17 +77,17 @@ def group_add_{{name}}(ndarray[{{c_type}}, ndim=2] out,
7777
@cython.wraparound(False)
7878
@cython.boundscheck(False)
7979
def group_prod_{{name}}(ndarray[{{c_type}}, ndim=2] out,
80-
ndarray[int64_t] counts,
80+
int64_t[:] counts,
8181
ndarray[{{c_type}}, ndim=2] values,
82-
ndarray[int64_t] labels,
82+
int64_t[:] labels,
8383
Py_ssize_t min_count=0):
8484
"""
8585
Only aggregates on axis=0
8686
"""
8787
cdef:
8888
Py_ssize_t i, j, N, K, lab, ncounts = len(counts)
8989
{{c_type}} val, count
90-
ndarray[{{c_type}}, ndim=2] prodx, nobs
90+
{{c_type}}[:, :] prodx, nobs
9191

9292
if not len(values) == len(labels):
9393
raise AssertionError("len(index) != len(labels)")
@@ -124,14 +124,14 @@ def group_prod_{{name}}(ndarray[{{c_type}}, ndim=2] out,
124124
@cython.boundscheck(False)
125125
@cython.cdivision(True)
126126
def group_var_{{name}}(ndarray[{{c_type}}, ndim=2] out,
127-
ndarray[int64_t] counts,
127+
int64_t[:] counts,
128128
ndarray[{{c_type}}, ndim=2] values,
129-
ndarray[int64_t] labels,
129+
int64_t[:] labels,
130130
Py_ssize_t min_count=-1):
131131
cdef:
132132
Py_ssize_t i, j, N, K, lab, ncounts = len(counts)
133133
{{c_type}} val, ct, oldmean
134-
ndarray[{{c_type}}, ndim=2] nobs, mean
134+
{{c_type}}[:, :] nobs, mean
135135

136136
assert min_count == -1, "'min_count' only used in add and prod"
137137

@@ -176,14 +176,14 @@ def group_var_{{name}}(ndarray[{{c_type}}, ndim=2] out,
176176
@cython.wraparound(False)
177177
@cython.boundscheck(False)
178178
def group_mean_{{name}}(ndarray[{{c_type}}, ndim=2] out,
179-
ndarray[int64_t] counts,
179+
int64_t[:] counts,
180180
ndarray[{{c_type}}, ndim=2] values,
181181
ndarray[int64_t] labels,
182182
Py_ssize_t min_count=-1):
183183
cdef:
184184
Py_ssize_t i, j, N, K, lab, ncounts = len(counts)
185185
{{c_type}} val, count
186-
ndarray[{{c_type}}, ndim=2] sumx, nobs
186+
{{c_type}}[:, :] sumx, nobs
187187

188188
assert min_count == -1, "'min_count' only used in add and prod"
189189

@@ -221,9 +221,9 @@ def group_mean_{{name}}(ndarray[{{c_type}}, ndim=2] out,
221221
@cython.wraparound(False)
222222
@cython.boundscheck(False)
223223
def group_ohlc_{{name}}(ndarray[{{c_type}}, ndim=2] out,
224-
ndarray[int64_t] counts,
224+
int64_t[:] counts,
225225
ndarray[{{c_type}}, ndim=2] values,
226-
ndarray[int64_t] labels,
226+
int64_t[:] labels,
227227
Py_ssize_t min_count=-1):
228228
"""
229229
Only aggregates on axis=0

0 commit comments

Comments
 (0)