Skip to content

Commit fd55cbe

Browse files
authored
REF: cython3 cleanups (#56028)
1 parent ef2c61a commit fd55cbe

File tree

8 files changed

+25
-58
lines changed

8 files changed

+25
-58
lines changed

pandas/_libs/algos.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,7 @@ def rank_1d(
998998

999999
N = len(values)
10001000
if labels is not None:
1001-
# TODO(cython3): cast won't be necessary (#2992)
1002-
assert <Py_ssize_t>len(labels) == N
1001+
assert len(labels) == N
10031002
out = np.empty(N)
10041003
grp_sizes = np.ones(N, dtype=np.int64)
10051004

@@ -1386,7 +1385,7 @@ def diff_2d(
13861385
cdef:
13871386
Py_ssize_t i, j, sx, sy, start, stop
13881387
bint f_contig = arr.flags.f_contiguous
1389-
# bint f_contig = arr.is_f_contig() # TODO(cython3)
1388+
# bint f_contig = arr.is_f_contig() # TODO(cython3) once arr is memoryview
13901389
diff_t left, right
13911390

13921391
# Disable for unsupported dtype combinations,

pandas/_libs/arrays.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ cdef class NDArrayBacked:
126126

127127
@property
128128
def size(self) -> int:
129-
# TODO(cython3): use self._ndarray.size
130-
return cnp.PyArray_SIZE(self._ndarray)
129+
return self._ndarray.size
131130

132131
@property
133132
def nbytes(self) -> int:

pandas/_libs/groupby.pyx

+4-12
Original file line numberDiff line numberDiff line change
@@ -1436,9 +1436,7 @@ def group_last(
14361436
bint uses_mask = mask is not None
14371437
bint isna_entry
14381438

1439-
# TODO(cython3):
1440-
# Instead of `labels.shape[0]` use `len(labels)`
1441-
if not len(values) == labels.shape[0]:
1439+
if not len(values) == len(labels):
14421440
raise AssertionError("len(index) != len(labels)")
14431441

14441442
min_count = max(min_count, 1)
@@ -1500,9 +1498,7 @@ def group_nth(
15001498
bint uses_mask = mask is not None
15011499
bint isna_entry
15021500

1503-
# TODO(cython3):
1504-
# Instead of `labels.shape[0]` use `len(labels)`
1505-
if not len(values) == labels.shape[0]:
1501+
if not len(values) == len(labels):
15061502
raise AssertionError("len(index) != len(labels)")
15071503

15081504
min_count = max(min_count, 1)
@@ -1676,9 +1672,7 @@ cdef group_min_max(
16761672
bint uses_mask = mask is not None
16771673
bint isna_entry
16781674

1679-
# TODO(cython3):
1680-
# Instead of `labels.shape[0]` use `len(labels)`
1681-
if not len(values) == labels.shape[0]:
1675+
if not len(values) == len(labels):
16821676
raise AssertionError("len(index) != len(labels)")
16831677

16841678
min_count = max(min_count, 1)
@@ -1779,9 +1773,7 @@ def group_idxmin_idxmax(
17791773

17801774
assert name == "idxmin" or name == "idxmax"
17811775

1782-
# TODO(cython3):
1783-
# Instead of `labels.shape[0]` use `len(labels)`
1784-
if not len(values) == labels.shape[0]:
1776+
if not len(values) == len(labels):
17851777
raise AssertionError("len(index) != len(labels)")
17861778

17871779
N, K = (<object>values).shape

pandas/_libs/internals.pyx

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ from collections import defaultdict
22
import weakref
33

44
cimport cython
5+
from cpython.pyport cimport PY_SSIZE_T_MAX
56
from cpython.slice cimport PySlice_GetIndicesEx
67
from cython cimport Py_ssize_t
78

8-
9-
cdef extern from "Python.h":
10-
# TODO(cython3): from cpython.pyport cimport PY_SSIZE_T_MAX
11-
Py_ssize_t PY_SSIZE_T_MAX
12-
139
import numpy as np
1410

1511
cimport numpy as cnp

pandas/_libs/lib.pyx

+1-2
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,7 @@ def get_reverse_indexer(const intp_t[:] indexer, Py_ssize_t length) -> ndarray:
502502

503503
@cython.wraparound(False)
504504
@cython.boundscheck(False)
505-
# TODO(cython3): Can add const once cython#1772 is resolved
506-
def has_infs(floating[:] arr) -> bool:
505+
def has_infs(const floating[:] arr) -> bool:
507506
cdef:
508507
Py_ssize_t i, n = len(arr)
509508
floating inf, neginf, val

pandas/_libs/parsers.pyx

+1-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ from cpython.unicode cimport (
3434
PyUnicode_AsUTF8String,
3535
PyUnicode_Decode,
3636
PyUnicode_DecodeUTF8,
37+
PyUnicode_FromString,
3738
)
3839
from cython cimport Py_ssize_t
3940
from libc.stdlib cimport free
@@ -44,11 +45,6 @@ from libc.string cimport (
4445
)
4546

4647

47-
cdef extern from "Python.h":
48-
# TODO(cython3): get this from cpython.unicode
49-
object PyUnicode_FromString(char *v)
50-
51-
5248
import numpy as np
5349

5450
cimport numpy as cnp

pandas/_libs/tslibs/timestamps.pyx

+10-22
Original file line numberDiff line numberDiff line change
@@ -504,17 +504,11 @@ cdef class _Timestamp(ABCTimestamp):
504504
return NotImplemented
505505

506506
# coerce if necessary if we are a Timestamp-like
507-
if (PyDateTime_Check(self)
508-
and (PyDateTime_Check(other) or cnp.is_datetime64_object(other))):
507+
if PyDateTime_Check(other) or cnp.is_datetime64_object(other):
509508
# both_timestamps is to determine whether Timedelta(self - other)
510509
# should raise the OOB error, or fall back returning a timedelta.
511-
# TODO(cython3): clean out the bits that moved to __rsub__
512-
both_timestamps = (isinstance(other, _Timestamp) and
513-
isinstance(self, _Timestamp))
514-
if isinstance(self, _Timestamp):
515-
other = type(self)(other)
516-
else:
517-
self = type(other)(self)
510+
both_timestamps = isinstance(other, _Timestamp)
511+
other = type(self)(other)
518512

519513
if (self.tzinfo is None) ^ (other.tzinfo is None):
520514
raise TypeError(
@@ -531,24 +525,18 @@ cdef class _Timestamp(ABCTimestamp):
531525
# scalar Timestamp/datetime - Timestamp/datetime -> yields a
532526
# Timedelta
533527
try:
534-
res_value = self._value- other._value
528+
res_value = self._value - other._value
535529
return Timedelta._from_value_and_reso(res_value, self._creso)
536530
except (OverflowError, OutOfBoundsDatetime, OutOfBoundsTimedelta) as err:
537-
if isinstance(other, _Timestamp):
538-
if both_timestamps:
539-
raise OutOfBoundsDatetime(
540-
"Result is too large for pandas.Timedelta. Convert inputs "
541-
"to datetime.datetime with 'Timestamp.to_pydatetime()' "
542-
"before subtracting."
543-
) from err
531+
if both_timestamps:
532+
raise OutOfBoundsDatetime(
533+
"Result is too large for pandas.Timedelta. Convert inputs "
534+
"to datetime.datetime with 'Timestamp.to_pydatetime()' "
535+
"before subtracting."
536+
) from err
544537
# We get here in stata tests, fall back to stdlib datetime
545538
# method and return stdlib timedelta object
546539
pass
547-
elif cnp.is_datetime64_object(self):
548-
# GH#28286 cython semantics for __rsub__, `other` is actually
549-
# the Timestamp
550-
# TODO(cython3): remove this, this moved to __rsub__
551-
return type(other)(self) - other
552540

553541
return NotImplemented
554542

pandas/_libs/tslibs/util.pxd

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
from cpython.object cimport PyTypeObject
3+
from cpython.unicode cimport PyUnicode_AsUTF8AndSize
34

45

56
cdef extern from "Python.h":
@@ -10,14 +11,8 @@ cdef extern from "Python.h":
1011
bint PyComplex_Check(object obj) nogil
1112
bint PyObject_TypeCheck(object obj, PyTypeObject* type) nogil
1213

13-
# TODO(cython3): cimport this, xref GH#49670
1414
# Note that following functions can potentially raise an exception,
15-
# thus they cannot be declared 'nogil'. Also PyUnicode_AsUTF8AndSize() can
16-
# potentially allocate memory inside in unlikely case of when underlying
17-
# unicode object was stored as non-utf8 and utf8 wasn't requested before.
18-
const char* PyUnicode_AsUTF8AndSize(object obj,
19-
Py_ssize_t* length) except NULL
20-
15+
# thus they cannot be declared 'nogil'.
2116
object PyUnicode_EncodeLocale(object obj, const char *errors) nogil
2217
object PyUnicode_DecodeLocale(const char *str, const char *errors) nogil
2318

@@ -180,6 +175,9 @@ cdef inline const char* get_c_string_buf_and_size(str py_string,
180175
-------
181176
buf : const char*
182177
"""
178+
# Note PyUnicode_AsUTF8AndSize() can
179+
# potentially allocate memory inside in unlikely case of when underlying
180+
# unicode object was stored as non-utf8 and utf8 wasn't requested before.
183181
return PyUnicode_AsUTF8AndSize(py_string, length)
184182

185183

0 commit comments

Comments
 (0)