Skip to content

Commit 3b5af6a

Browse files
committed
User memory views in .pyx files
1 parent 97be7d6 commit 3b5af6a

9 files changed

+55
-55
lines changed

pandas/_libs/algos.pyx

+13-13
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class NegInfinity(object):
7676

7777
@cython.wraparound(False)
7878
@cython.boundscheck(False)
79-
cpdef ndarray[int64_t, ndim=1] unique_deltas(ndarray[int64_t] arr):
79+
cpdef ndarray[int64_t, ndim=1] unique_deltas(const int64_t[:] arr):
8080
"""
8181
Efficiently find the unique first-differences of the given array.
8282
@@ -150,7 +150,7 @@ def is_lexsorted(list_of_arrays: list) -> bint:
150150

151151
@cython.boundscheck(False)
152152
@cython.wraparound(False)
153-
def groupsort_indexer(ndarray[int64_t] index, Py_ssize_t ngroups):
153+
def groupsort_indexer(const int64_t[:] index, Py_ssize_t ngroups):
154154
"""
155155
compute a 1-d indexer that is an ordering of the passed index,
156156
ordered by the groups. This is a reverse of the label
@@ -230,7 +230,7 @@ def kth_smallest(numeric[:] a, Py_ssize_t k) -> numeric:
230230

231231
@cython.boundscheck(False)
232232
@cython.wraparound(False)
233-
def nancorr(ndarray[float64_t, ndim=2] mat, bint cov=0, minp=None):
233+
def nancorr(const float64_t[:, :] mat, bint cov=0, minp=None):
234234
cdef:
235235
Py_ssize_t i, j, xi, yi, N, K
236236
bint minpv
@@ -294,7 +294,7 @@ def nancorr(ndarray[float64_t, ndim=2] mat, bint cov=0, minp=None):
294294

295295
@cython.boundscheck(False)
296296
@cython.wraparound(False)
297-
def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1):
297+
def nancorr_spearman(const float64_t[:, :] mat, Py_ssize_t minp=1):
298298
cdef:
299299
Py_ssize_t i, j, xi, yi, N, K
300300
ndarray[float64_t, ndim=2] result
@@ -435,8 +435,8 @@ def pad(ndarray[algos_t] old, ndarray[algos_t] new, limit=None):
435435

436436
@cython.boundscheck(False)
437437
@cython.wraparound(False)
438-
def pad_inplace(ndarray[algos_t] values,
439-
ndarray[uint8_t, cast=True] mask,
438+
def pad_inplace(algos_t[:] values,
439+
const uint8_t[:] mask,
440440
limit=None):
441441
cdef:
442442
Py_ssize_t i, N
@@ -472,8 +472,8 @@ def pad_inplace(ndarray[algos_t] values,
472472

473473
@cython.boundscheck(False)
474474
@cython.wraparound(False)
475-
def pad_2d_inplace(ndarray[algos_t, ndim=2] values,
476-
ndarray[uint8_t, ndim=2] mask,
475+
def pad_2d_inplace(algos_t[:, :] values,
476+
const uint8_t[:, :] mask,
477477
limit=None):
478478
cdef:
479479
Py_ssize_t i, j, N, K
@@ -602,8 +602,8 @@ def backfill(ndarray[algos_t] old, ndarray[algos_t] new, limit=None):
602602

603603
@cython.boundscheck(False)
604604
@cython.wraparound(False)
605-
def backfill_inplace(ndarray[algos_t] values,
606-
ndarray[uint8_t, cast=True] mask,
605+
def backfill_inplace(algos_t[:] values,
606+
const uint8_t[:] mask,
607607
limit=None):
608608
cdef:
609609
Py_ssize_t i, N
@@ -639,8 +639,8 @@ def backfill_inplace(ndarray[algos_t] values,
639639

640640
@cython.boundscheck(False)
641641
@cython.wraparound(False)
642-
def backfill_2d_inplace(ndarray[algos_t, ndim=2] values,
643-
ndarray[uint8_t, ndim=2] mask,
642+
def backfill_2d_inplace(algos_t[:, :] values,
643+
const uint8_t[:, :] mask,
644644
limit=None):
645645
cdef:
646646
Py_ssize_t i, j, N, K
@@ -678,7 +678,7 @@ def backfill_2d_inplace(ndarray[algos_t, ndim=2] values,
678678

679679
@cython.wraparound(False)
680680
@cython.boundscheck(False)
681-
def arrmap(ndarray[algos_t] index, object func):
681+
def arrmap(algos_t[:] index, object func):
682682
cdef:
683683
Py_ssize_t length = index.shape[0]
684684
Py_ssize_t i = 0

pandas/_libs/hashtable.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ cdef class Int64Factorizer:
142142

143143
@cython.wraparound(False)
144144
@cython.boundscheck(False)
145-
def unique_label_indices(ndarray[int64_t, ndim=1] labels):
145+
def unique_label_indices(const int64_t[:] labels):
146146
"""
147147
indices of the first occurrences of the unique labels
148148
*excluding* -1. equivalent to:
@@ -170,6 +170,6 @@ def unique_label_indices(ndarray[int64_t, ndim=1] labels):
170170
kh_destroy_int64(table)
171171

172172
arr = idx.to_array()
173-
arr = arr[labels[arr].argsort()]
173+
arr = arr[np.asarray(labels)[arr].argsort()]
174174

175175
return arr[1:] if arr.size != 0 and labels[arr[0]] == -1 else arr

pandas/_libs/index.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ cpdef get_value_at(ndarray arr, object loc, object tz=None):
4848
return util.get_value_at(arr, loc)
4949

5050

51-
def get_value_box(arr: ndarray, loc: object) -> object:
51+
def get_value_box(ndarray arr, object loc) -> object:
5252
return get_value_at(arr, loc, tz=None)
5353

5454

pandas/_libs/join.pyx

+23-23
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ from pandas._libs.algos import groupsort_indexer, ensure_platform_int
1414
from pandas.core.algorithms import take_nd
1515

1616

17-
def inner_join(ndarray[int64_t] left, ndarray[int64_t] right,
17+
def inner_join(const int64_t[:] left, const int64_t[:] right,
1818
Py_ssize_t max_groups):
1919
cdef:
2020
Py_ssize_t i, j, k, count = 0
@@ -65,7 +65,7 @@ def inner_join(ndarray[int64_t] left, ndarray[int64_t] right,
6565
_get_result_indexer(right_sorter, right_indexer))
6666

6767

68-
def left_outer_join(ndarray[int64_t] left, ndarray[int64_t] right,
68+
def left_outer_join(const int64_t[:] left, const int64_t[:] right,
6969
Py_ssize_t max_groups, sort=True):
7070
cdef:
7171
Py_ssize_t i, j, k, count = 0
@@ -139,7 +139,7 @@ def left_outer_join(ndarray[int64_t] left, ndarray[int64_t] right,
139139
return left_indexer, right_indexer
140140

141141

142-
def full_outer_join(ndarray[int64_t] left, ndarray[int64_t] right,
142+
def full_outer_join(const int64_t[:] left, const int64_t[:] right,
143143
Py_ssize_t max_groups):
144144
cdef:
145145
Py_ssize_t i, j, k, count = 0
@@ -213,7 +213,7 @@ def _get_result_indexer(sorter, indexer):
213213
return res
214214

215215

216-
def ffill_indexer(ndarray[int64_t] indexer):
216+
def ffill_indexer(const int64_t[:] indexer):
217217
cdef:
218218
Py_ssize_t i, n = len(indexer)
219219
ndarray[int64_t] result
@@ -252,7 +252,7 @@ ctypedef fused join_t:
252252

253253
@cython.wraparound(False)
254254
@cython.boundscheck(False)
255-
def left_join_indexer_unique(ndarray[join_t] left, ndarray[join_t] right):
255+
def left_join_indexer_unique(join_t[:] left, join_t[:] right):
256256
cdef:
257257
Py_ssize_t i, j, nleft, nright
258258
ndarray[int64_t] indexer
@@ -677,10 +677,10 @@ ctypedef fused by_t:
677677
uint64_t
678678

679679

680-
def asof_join_backward_on_X_by_Y(ndarray[asof_t] left_values,
681-
ndarray[asof_t] right_values,
682-
ndarray[by_t] left_by_values,
683-
ndarray[by_t] right_by_values,
680+
def asof_join_backward_on_X_by_Y(asof_t[:] left_values,
681+
asof_t[:] right_values,
682+
by_t[:] left_by_values,
683+
by_t[:] right_by_values,
684684
bint allow_exact_matches=1,
685685
tolerance=None):
686686

@@ -746,10 +746,10 @@ def asof_join_backward_on_X_by_Y(ndarray[asof_t] left_values,
746746
return left_indexer, right_indexer
747747

748748

749-
def asof_join_forward_on_X_by_Y(ndarray[asof_t] left_values,
750-
ndarray[asof_t] right_values,
751-
ndarray[by_t] left_by_values,
752-
ndarray[by_t] right_by_values,
749+
def asof_join_forward_on_X_by_Y(asof_t[:] left_values,
750+
asof_t[:] right_values,
751+
by_t[:] left_by_values,
752+
by_t[:] right_by_values,
753753
bint allow_exact_matches=1,
754754
tolerance=None):
755755

@@ -815,10 +815,10 @@ def asof_join_forward_on_X_by_Y(ndarray[asof_t] left_values,
815815
return left_indexer, right_indexer
816816

817817

818-
def asof_join_nearest_on_X_by_Y(ndarray[asof_t] left_values,
819-
ndarray[asof_t] right_values,
820-
ndarray[by_t] left_by_values,
821-
ndarray[by_t] right_by_values,
818+
def asof_join_nearest_on_X_by_Y(asof_t[:] left_values,
819+
asof_t[:] right_values,
820+
by_t[:] left_by_values,
821+
by_t[:] right_by_values,
822822
bint allow_exact_matches=1,
823823
tolerance=None):
824824

@@ -864,8 +864,8 @@ def asof_join_nearest_on_X_by_Y(ndarray[asof_t] left_values,
864864
# asof_join
865865
# ----------------------------------------------------------------------
866866

867-
def asof_join_backward(ndarray[asof_t] left_values,
868-
ndarray[asof_t] right_values,
867+
def asof_join_backward(asof_t[:] left_values,
868+
asof_t[:] right_values,
869869
bint allow_exact_matches=1,
870870
tolerance=None):
871871

@@ -917,8 +917,8 @@ def asof_join_backward(ndarray[asof_t] left_values,
917917
return left_indexer, right_indexer
918918

919919

920-
def asof_join_forward(ndarray[asof_t] left_values,
921-
ndarray[asof_t] right_values,
920+
def asof_join_forward(asof_t[:] left_values,
921+
asof_t[:] right_values,
922922
bint allow_exact_matches=1,
923923
tolerance=None):
924924

@@ -971,8 +971,8 @@ def asof_join_forward(ndarray[asof_t] left_values,
971971
return left_indexer, right_indexer
972972

973973

974-
def asof_join_nearest(ndarray[asof_t] left_values,
975-
ndarray[asof_t] right_values,
974+
def asof_join_nearest(asof_t[:] left_values,
975+
asof_t[:] right_values,
976976
bint allow_exact_matches=1,
977977
tolerance=None):
978978

pandas/_libs/lib.pyx

+12-12
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def fast_zip(list ndarrays):
378378
return result
379379

380380

381-
def get_reverse_indexer(ndarray[int64_t] indexer, Py_ssize_t length):
381+
def get_reverse_indexer(const int64_t[:] indexer, Py_ssize_t length):
382382
"""
383383
Reverse indexing operation.
384384
@@ -407,7 +407,7 @@ def get_reverse_indexer(ndarray[int64_t] indexer, Py_ssize_t length):
407407

408408
@cython.wraparound(False)
409409
@cython.boundscheck(False)
410-
def has_infs_f4(ndarray[float32_t] arr) -> bool:
410+
def has_infs_f4(const float32_t[:] arr) -> bool:
411411
cdef:
412412
Py_ssize_t i, n = len(arr)
413413
float32_t inf, neginf, val
@@ -424,7 +424,7 @@ def has_infs_f4(ndarray[float32_t] arr) -> bool:
424424

425425
@cython.wraparound(False)
426426
@cython.boundscheck(False)
427-
def has_infs_f8(ndarray[float64_t] arr) -> bool:
427+
def has_infs_f8(const float64_t[:] arr) -> bool:
428428
cdef:
429429
Py_ssize_t i, n = len(arr)
430430
float64_t inf, neginf, val
@@ -662,7 +662,7 @@ def clean_index_list(obj: list):
662662
# is a general, O(max(len(values), len(binner))) method.
663663
@cython.boundscheck(False)
664664
@cython.wraparound(False)
665-
def generate_bins_dt64(ndarray[int64_t] values, ndarray[int64_t] binner,
665+
def generate_bins_dt64(ndarray[int64_t] values, const int64_t[:] binner,
666666
object closed='left', bint hasnans=0):
667667
"""
668668
Int64 (datetime64) version of generic python version in groupby.py
@@ -725,7 +725,7 @@ def generate_bins_dt64(ndarray[int64_t] values, ndarray[int64_t] binner,
725725

726726
@cython.boundscheck(False)
727727
@cython.wraparound(False)
728-
def row_bool_subset(ndarray[float64_t, ndim=2] values,
728+
def row_bool_subset(const float64_t[:, :] values,
729729
ndarray[uint8_t, cast=True] mask):
730730
cdef:
731731
Py_ssize_t i, j, n, k, pos = 0
@@ -769,8 +769,8 @@ def row_bool_subset_object(ndarray[object, ndim=2] values,
769769

770770
@cython.boundscheck(False)
771771
@cython.wraparound(False)
772-
def get_level_sorter(ndarray[int64_t, ndim=1] label,
773-
ndarray[int64_t, ndim=1] starts):
772+
def get_level_sorter(const int64_t[:] label,
773+
const int64_t[:] starts):
774774
"""
775775
argsort for a single level of a multi-index, keeping the order of higher
776776
levels unchanged. `starts` points to starts of same-key indices w.r.t
@@ -785,15 +785,15 @@ def get_level_sorter(ndarray[int64_t, ndim=1] label,
785785

786786
for i in range(len(starts) - 1):
787787
l, r = starts[i], starts[i + 1]
788-
out[l:r] = l + label[l:r].argsort(kind='mergesort')
788+
out[l:r] = l + np.asarray(label)[l:r].argsort(kind='mergesort')
789789

790790
return out
791791

792792

793793
@cython.boundscheck(False)
794794
@cython.wraparound(False)
795795
def count_level_2d(ndarray[uint8_t, ndim=2, cast=True] mask,
796-
ndarray[int64_t, ndim=1] labels,
796+
const int64_t[:] labels,
797797
Py_ssize_t max_bin,
798798
int axis):
799799
cdef:
@@ -820,7 +820,7 @@ def count_level_2d(ndarray[uint8_t, ndim=2, cast=True] mask,
820820
return counts
821821

822822

823-
def generate_slices(ndarray[int64_t] labels, Py_ssize_t ngroups):
823+
def generate_slices(const int64_t[:] labels, Py_ssize_t ngroups):
824824
cdef:
825825
Py_ssize_t i, group_size, n, start
826826
int64_t lab
@@ -849,7 +849,7 @@ def generate_slices(ndarray[int64_t] labels, Py_ssize_t ngroups):
849849
return starts, ends
850850

851851

852-
def indices_fast(object index, ndarray[int64_t] labels, list keys,
852+
def indices_fast(object index, const int64_t[:] labels, list keys,
853853
list sorted_labels):
854854
cdef:
855855
Py_ssize_t i, j, k, lab, cur, start, n = len(labels)
@@ -2148,7 +2148,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
21482148

21492149
@cython.boundscheck(False)
21502150
@cython.wraparound(False)
2151-
def map_infer_mask(ndarray arr, object f, ndarray[uint8_t] mask,
2151+
def map_infer_mask(ndarray arr, object f, const uint8_t[:] mask,
21522152
bint convert=1):
21532153
"""
21542154
Substitute for np.vectorize with pandas-friendly dtype inference

pandas/_libs/reduction.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ class InvalidApply(Exception):
494494

495495

496496
def apply_frame_axis0(object frame, object f, object names,
497-
ndarray[int64_t] starts, ndarray[int64_t] ends):
497+
const int64_t[:] starts, const int64_t[:] ends):
498498
cdef:
499499
BlockSlider slider
500500
Py_ssize_t i, n = len(starts)

pandas/_libs/tslibs/conversion.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def ensure_timedelta64ns(arr: ndarray, copy: bool=True):
147147

148148
@cython.boundscheck(False)
149149
@cython.wraparound(False)
150-
def datetime_to_datetime64(values: object[:]):
150+
def datetime_to_datetime64(object[:] values):
151151
"""
152152
Convert ndarray of datetime-like objects to int64 array representing
153153
nanosecond timestamps.

pandas/_libs/tslibs/fields.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ def get_start_end_field(int64_t[:] dtindex, object field,
381381

382382
@cython.wraparound(False)
383383
@cython.boundscheck(False)
384-
def get_date_field(ndarray[int64_t] dtindex, object field):
384+
def get_date_field(int64_t[:] dtindex, object field):
385385
"""
386386
Given a int64-based datetime index, extract the year, month, etc.,
387387
field and return an array of these values.

pandas/_libs/window.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ cdef class VariableWindowIndexer(WindowIndexer):
243243
# max window size
244244
self.win = (self.end - self.start).max()
245245

246-
def build(self, ndarray[int64_t] index, int64_t win, bint left_closed,
246+
def build(self, const int64_t[:] index, int64_t win, bint left_closed,
247247
bint right_closed):
248248

249249
cdef:

0 commit comments

Comments
 (0)