cimport cython
cimport numpy as np
import numpy as np
from cython import address
import pandas as pd
@cython.boundscheck(False)
@cython.wraparound(False)
cpdef unsigned char[:,:] get_bytes_arr_view(np.ndarray bytes_arr):
# 在每个bytes元素最后补零(可自行测试不补零的后果)
bytes_arr = bytes_arr.astype(str("S%s" % (bytes_arr.dtype.itemsize+1)))
# 按uint8获取视图
cdef unsigned char[:,:] bytes_arr_view = bytes_arr.view('uint8').reshape(bytes_arr.size, bytes_arr.dtype.itemsize)
return bytes_arr_view
def use_case():
str_arr = np.array(["19:30:11", "19:30:31"])
cdef np.ndarray time_arr = str_arr.astype(bytes)
cdef unsigned char[:,:] time_arr_view = get_bytes_arr_view(time_arr)
cdef unsigned char* time = b""
# 遍历打印字符串array的元素
for i in range(time_arr.shape[0]):
time = address(time_arr_view[i, 0])
print(time)
版权声明:本文为junior_programmer原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。