Skip to content

Commit 7df43a5

Browse files
mroeschkepmhatre1
authored andcommitted
REF: Use more cython memoryviews (pandas-dev#58271)
* fix some cython 3 todos * Another memoryview * Add another memoryview * Use more memoryview * another memoryview * more memoryview syntax in groupby * Add a few more memoryviews * Use Py_ssize_t
1 parent 650fe98 commit 7df43a5

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

pandas/_libs/algos.pyx

+10-12
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None):
351351
Py_ssize_t i, xi, yi, N, K
352352
int64_t minpv
353353
float64_t[:, ::1] result
354-
ndarray[uint8_t, ndim=2] mask
354+
uint8_t[:, :] mask
355355
int64_t nobs = 0
356356
float64_t vx, vy, dx, dy, meanx, meany, divisor, ssqdmx, ssqdmy, covxy
357357

@@ -407,7 +407,7 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1) -> ndarr
407407
Py_ssize_t i, xi, yi, N, K
408408
ndarray[float64_t, ndim=2] result
409409
ndarray[float64_t, ndim=2] ranked_mat
410-
ndarray[float64_t, ndim=1] rankedx, rankedy
410+
float64_t[::1] rankedx, rankedy
411411
float64_t[::1] maskedx, maskedy
412412
ndarray[uint8_t, ndim=2] mask
413413
int64_t nobs = 0
@@ -566,8 +566,8 @@ def get_fill_indexer(const uint8_t[:] mask, limit=None):
566566
@cython.boundscheck(False)
567567
@cython.wraparound(False)
568568
def pad(
569-
ndarray[numeric_object_t] old,
570-
ndarray[numeric_object_t] new,
569+
const numeric_object_t[:] old,
570+
const numeric_object_t[:] new,
571571
limit=None
572572
) -> ndarray:
573573
# -> ndarray[intp_t, ndim=1]
@@ -691,8 +691,8 @@ def pad_2d_inplace(numeric_object_t[:, :] values, uint8_t[:, :] mask, limit=None
691691
@cython.boundscheck(False)
692692
@cython.wraparound(False)
693693
def backfill(
694-
ndarray[numeric_object_t] old,
695-
ndarray[numeric_object_t] new,
694+
const numeric_object_t[:] old,
695+
const numeric_object_t[:] new,
696696
limit=None
697697
) -> ndarray: # -> ndarray[intp_t, ndim=1]
698698
"""
@@ -786,7 +786,7 @@ def backfill_2d_inplace(numeric_object_t[:, :] values,
786786

787787
@cython.boundscheck(False)
788788
@cython.wraparound(False)
789-
def is_monotonic(ndarray[numeric_object_t, ndim=1] arr, bint timelike):
789+
def is_monotonic(const numeric_object_t[:] arr, bint timelike):
790790
"""
791791
Returns
792792
-------
@@ -1089,8 +1089,7 @@ cdef void rank_sorted_1d(
10891089
float64_t[::1] out,
10901090
int64_t[::1] grp_sizes,
10911091
const intp_t[:] sort_indexer,
1092-
# TODO(cython3): make const (https://github.com/cython/cython/issues/3222)
1093-
numeric_object_t[:] masked_vals,
1092+
const numeric_object_t[:] masked_vals,
10941093
const uint8_t[:] mask,
10951094
bint check_mask,
10961095
Py_ssize_t N,
@@ -1378,16 +1377,15 @@ ctypedef fused out_t:
13781377
@cython.boundscheck(False)
13791378
@cython.wraparound(False)
13801379
def diff_2d(
1381-
ndarray[diff_t, ndim=2] arr, # TODO(cython3) update to "const diff_t[:, :] arr"
1380+
const diff_t[:, :] arr,
13821381
ndarray[out_t, ndim=2] out,
13831382
Py_ssize_t periods,
13841383
int axis,
13851384
bint datetimelike=False,
13861385
):
13871386
cdef:
13881387
Py_ssize_t i, j, sx, sy, start, stop
1389-
bint f_contig = arr.flags.f_contiguous
1390-
# bint f_contig = arr.is_f_contig() # TODO(cython3) once arr is memoryview
1388+
bint f_contig = arr.is_f_contig()
13911389
diff_t left, right
13921390

13931391
# Disable for unsupported dtype combinations,

pandas/_libs/groupby.pyx

+10-14
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,9 @@ def group_shift_indexer(
511511
@cython.wraparound(False)
512512
@cython.boundscheck(False)
513513
def group_fillna_indexer(
514-
ndarray[intp_t] out,
515-
ndarray[intp_t] labels,
516-
ndarray[uint8_t] mask,
514+
Py_ssize_t[::1] out,
515+
const intp_t[::1] labels,
516+
const uint8_t[:] mask,
517517
int64_t limit,
518518
bint compute_ffill,
519519
int ngroups,
@@ -1179,13 +1179,13 @@ def group_ohlc(
11791179
@cython.boundscheck(False)
11801180
@cython.wraparound(False)
11811181
def group_quantile(
1182-
ndarray[float64_t, ndim=2] out,
1182+
float64_t[:, ::1] out,
11831183
ndarray[numeric_t, ndim=1] values,
1184-
ndarray[intp_t] labels,
1184+
const intp_t[::1] labels,
11851185
const uint8_t[:] mask,
11861186
const float64_t[:] qs,
1187-
ndarray[int64_t] starts,
1188-
ndarray[int64_t] ends,
1187+
const int64_t[::1] starts,
1188+
const int64_t[::1] ends,
11891189
str interpolation,
11901190
uint8_t[:, ::1] result_mask,
11911191
bint is_datetimelike,
@@ -1388,7 +1388,7 @@ cdef inline void _check_below_mincount(
13881388
uint8_t[:, ::1] result_mask,
13891389
Py_ssize_t ncounts,
13901390
Py_ssize_t K,
1391-
int64_t[:, ::1] nobs,
1391+
const int64_t[:, ::1] nobs,
13921392
int64_t min_count,
13931393
mincount_t[:, ::1] resx,
13941394
) noexcept:
@@ -1435,14 +1435,12 @@ cdef inline void _check_below_mincount(
14351435
out[i, j] = 0
14361436

14371437

1438-
# TODO(cython3): GH#31710 use memorviews once cython 0.30 is released so we can
1439-
# use `const numeric_object_t[:, :] values`
14401438
@cython.wraparound(False)
14411439
@cython.boundscheck(False)
14421440
def group_last(
14431441
numeric_object_t[:, ::1] out,
14441442
int64_t[::1] counts,
1445-
ndarray[numeric_object_t, ndim=2] values,
1443+
const numeric_object_t[:, :] values,
14461444
const intp_t[::1] labels,
14471445
const uint8_t[:, :] mask,
14481446
uint8_t[:, ::1] result_mask=None,
@@ -1502,14 +1500,12 @@ def group_last(
15021500
)
15031501

15041502

1505-
# TODO(cython3): GH#31710 use memorviews once cython 0.30 is released so we can
1506-
# use `const numeric_object_t[:, :] values`
15071503
@cython.wraparound(False)
15081504
@cython.boundscheck(False)
15091505
def group_nth(
15101506
numeric_object_t[:, ::1] out,
15111507
int64_t[::1] counts,
1512-
ndarray[numeric_object_t, ndim=2] values,
1508+
const numeric_object_t[:, :] values,
15131509
const intp_t[::1] labels,
15141510
const uint8_t[:, :] mask,
15151511
uint8_t[:, ::1] result_mask=None,

pandas/_libs/hashing.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import numpy as np
1111

1212
from numpy cimport (
1313
import_array,
14-
ndarray,
1514
uint8_t,
1615
uint64_t,
1716
)
@@ -23,7 +22,7 @@ from pandas._libs.util cimport is_nan
2322

2423
@cython.boundscheck(False)
2524
def hash_object_array(
26-
ndarray[object] arr, str key, str encoding="utf8"
25+
object[:] arr, str key, str encoding="utf8"
2726
) -> np.ndarray[np.uint64]:
2827
"""
2928
Parameters

0 commit comments

Comments
 (0)