Skip to content

Fix cython3 #35675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,8 @@ cdef class Slider:
Only handles contiguous data for now
"""
cdef:
ndarray values, buf
Py_ssize_t stride, orig_len, orig_stride
char *orig_data
ndarray values, buf, orig_data
Py_ssize_t stride

def __init__(self, ndarray values, ndarray buf):
assert values.ndim == 1
Expand All @@ -309,25 +308,17 @@ cdef class Slider:
self.buf = buf
self.stride = values.strides[0]

self.orig_data = self.buf.data
self.orig_len = self.buf.shape[0]
self.orig_stride = self.buf.strides[0]

self.buf.data = self.values.data
self.buf.strides[0] = self.stride
self.orig_data = self.buf[:]
self.buf = self.values[::self.stride]

cdef move(self, int start, int end):
"""
For slicing
"""
self.buf.data = self.values.data + self.stride * start
self.buf.shape[0] = end - start
self.buf = self.values[start:end:self.stride]

cdef reset(self):

self.buf.shape[0] = self.orig_len
self.buf.data = self.orig_data
self.buf.strides[0] = self.orig_stride
self.buf = self.orig_data[:]


class InvalidApply(Exception):
Expand Down Expand Up @@ -407,7 +398,7 @@ cdef class BlockSlider:
list blocks

cdef:
char **base_ptrs
list base_ptrs

def __init__(self, object frame):
cdef:
Expand All @@ -430,12 +421,7 @@ cdef class BlockSlider:
self.idx_slider = Slider(
self.frame.index._index_data, self.dummy.index._index_data)

self.base_ptrs = <char**>malloc(sizeof(char*) * len(self.blocks))
for i, block in enumerate(self.blocks):
self.base_ptrs[i] = (<ndarray>block).data

def __dealloc__(self):
free(self.base_ptrs)
self.base_ptrs = [block[:] for block in self.blocks]

cdef move(self, int start, int end):
cdef:
Expand All @@ -444,11 +430,7 @@ cdef class BlockSlider:

# move blocks
for i in range(self.nblocks):
arr = self.blocks[i]

# axis=1 is the frame's axis=0
arr.data = self.base_ptrs[i] + arr.strides[1] * start
arr.shape[1] = end - start
self.blocks[i] = self.base_ptrs[i][:, start:end]

# move and set the index
self.idx_slider.move(start, end)
Expand All @@ -463,8 +445,4 @@ cdef class BlockSlider:

# reset blocks
for i in range(self.nblocks):
arr = self.blocks[i]

# axis=1 is the frame's axis=0
arr.data = self.base_ptrs[i]
arr.shape[1] = 0
self.blocks[i] = self.base_ptrs[i][:]
4 changes: 2 additions & 2 deletions pandas/_libs/writers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cython
from cython import Py_ssize_t

from cpython.bytes cimport PyBytes_GET_SIZE
from cpython.unicode cimport PyUnicode_GET_SIZE
from cpython.unicode cimport PyUnicode_GET_LENGTH

import numpy as np

Expand Down Expand Up @@ -144,7 +144,7 @@ cpdef inline Py_ssize_t word_len(object val):
Py_ssize_t l = 0

if isinstance(val, str):
l = PyUnicode_GET_SIZE(val)
l = PyUnicode_GET_LENGTH(val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be done independently of the rest of the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, do you want to split it out?

elif isinstance(val, bytes):
l = PyBytes_GET_SIZE(val)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
requires = [
"setuptools",
"wheel",
"Cython>=0.29.16,<3", # Note: sync with setup.py
"Cython>=0.29.21", # Note: sync with setup.py
"numpy==1.16.5; python_version=='3.7' and platform_system!='AIX'",
"numpy==1.17.3; python_version>='3.8' and platform_system!='AIX'",
"numpy==1.16.5; python_version=='3.7' and platform_system=='AIX'",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def is_platform_mac():


min_numpy_ver = "1.16.5"
min_cython_ver = "0.29.16" # note: sync with pyproject.toml
min_cython_ver = "0.29.21" # note: sync with pyproject.toml

try:
import Cython
Expand Down