Skip to content

Commit 6151ba6

Browse files
jbrockmendeljreback
authored andcommitted
use fused types for reshape (#22454)
1 parent 9dd454e commit 6151ba6

File tree

3 files changed

+89
-91
lines changed

3 files changed

+89
-91
lines changed

pandas/_libs/reshape.pyx

+88-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,95 @@
11
# -*- coding: utf-8 -*-
22

3-
cimport cython
4-
from cython cimport Py_ssize_t
3+
import cython
4+
from cython import Py_ssize_t
55

6-
import numpy as np
7-
from numpy cimport (ndarray,
8-
int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t,
6+
from numpy cimport (int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t,
97
uint32_t, uint64_t, float32_t, float64_t)
108

119

12-
cdef double NaN = <double> np.NaN
13-
cdef double nan = NaN
10+
ctypedef fused reshape_t:
11+
uint8_t
12+
uint16_t
13+
uint32_t
14+
uint64_t
15+
int8_t
16+
int16_t
17+
int32_t
18+
int64_t
19+
float32_t
20+
float64_t
21+
object
1422

15-
include "reshape_helper.pxi"
23+
24+
@cython.wraparound(False)
25+
@cython.boundscheck(False)
26+
def unstack(reshape_t[:, :] values, uint8_t[:] mask,
27+
Py_ssize_t stride, Py_ssize_t length, Py_ssize_t width,
28+
reshape_t[:, :] new_values, uint8_t[:, :] new_mask):
29+
"""
30+
transform long sorted_values to wide new_values
31+
32+
Parameters
33+
----------
34+
values : typed ndarray
35+
mask : boolean ndarray
36+
stride : int
37+
length : int
38+
width : int
39+
new_values : typed ndarray
40+
result array
41+
new_mask : boolean ndarray
42+
result mask
43+
"""
44+
cdef:
45+
Py_ssize_t i, j, w, nulls, s, offset
46+
47+
if reshape_t is not object:
48+
# evaluated at compile-time
49+
with nogil:
50+
for i in range(stride):
51+
52+
nulls = 0
53+
for j in range(length):
54+
55+
for w in range(width):
56+
57+
offset = j * width + w
58+
59+
if mask[offset]:
60+
s = i * width + w
61+
new_values[j, s] = values[offset - nulls, i]
62+
new_mask[j, s] = 1
63+
else:
64+
nulls += 1
65+
66+
else:
67+
# object-dtype, identical to above but we cannot use nogil
68+
for i in range(stride):
69+
70+
nulls = 0
71+
for j in range(length):
72+
73+
for w in range(width):
74+
75+
offset = j * width + w
76+
77+
if mask[offset]:
78+
s = i * width + w
79+
new_values[j, s] = values[offset - nulls, i]
80+
new_mask[j, s] = 1
81+
else:
82+
nulls += 1
83+
84+
85+
unstack_uint8 = unstack["uint8_t"]
86+
unstack_uint16 = unstack["uint16_t"]
87+
unstack_uint32 = unstack["uint32_t"]
88+
unstack_uint64 = unstack["uint64_t"]
89+
unstack_int8 = unstack["int8_t"]
90+
unstack_int16 = unstack["int16_t"]
91+
unstack_int32 = unstack["int32_t"]
92+
unstack_int64 = unstack["int64_t"]
93+
unstack_float32 = unstack["float32_t"]
94+
unstack_float64 = unstack["float64_t"]
95+
unstack_object = unstack["object"]

pandas/_libs/reshape_helper.pxi.in

-81
This file was deleted.

setup.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ def is_platform_windows():
7777
'_libs/algos_rank_helper.pxi.in'],
7878
'groupby': ['_libs/groupby_helper.pxi.in'],
7979
'join': ['_libs/join_helper.pxi.in', '_libs/join_func_helper.pxi.in'],
80-
'reshape': ['_libs/reshape_helper.pxi.in'],
8180
'hashtable': ['_libs/hashtable_class_helper.pxi.in',
8281
'_libs/hashtable_func_helper.pxi.in'],
8382
'index': ['_libs/index_class_helper.pxi.in'],
@@ -558,7 +557,7 @@ def srcpath(name=None, suffix='.pyx', subdir='src'):
558557
'include': []},
559558
'_libs.reshape': {
560559
'pyxfile': '_libs/reshape',
561-
'depends': _pxi_dep['reshape']},
560+
'depends': []},
562561
'_libs.skiplist': {
563562
'pyxfile': '_libs/skiplist',
564563
'depends': ['pandas/_libs/src/skiplist.h']},

0 commit comments

Comments
 (0)