Skip to content

CLN: tighten cython declarations #40834

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

Merged
merged 8 commits into from
Apr 13, 2021
Merged
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
3 changes: 3 additions & 0 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ cdef class IndexEngine:
return val in self.mapping

cpdef get_loc(self, object val):
# -> Py_ssize_t | slice | ndarray[bool]
cdef:
Py_ssize_t loc

Expand Down Expand Up @@ -109,6 +110,7 @@ cdef class IndexEngine:
raise KeyError(val)

cdef inline _get_loc_duplicates(self, object val):
# -> Py_ssize_t | slice | ndarray[bool]
cdef:
Py_ssize_t diff

Expand Down Expand Up @@ -142,6 +144,7 @@ cdef class IndexEngine:
cdef _unpack_bool_indexer(self,
ndarray[uint8_t, ndim=1, cast=True] indexer,
object val):
# Returns ndarray[bool] or int
cdef:
ndarray[intp_t, ndim=1] found
int count
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/index_class_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ cdef class {{name}}Engine(IndexEngine):
raise KeyError(val)
{{endif}}

cdef void _call_map_locations(self, ndarray values):
self.mapping.map_locations(algos.ensure_{{name.lower()}}(values))
cdef void _call_map_locations(self, ndarray[{{dtype}}_t] values):
self.mapping.map_locations(values)

cdef _maybe_get_bool_indexer(self, object val):
# Returns ndarray[bool] or int
Expand Down
12 changes: 6 additions & 6 deletions pandas/_libs/intervaltree.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ cdef class IntervalTree(IntervalMixin):
self.root = node_cls(self.left, self.right, indices, leaf_size)

@property
def left_sorter(self):
def left_sorter(self) -> np.ndarray:
"""How to sort the left labels; this is used for binary search
"""
if self._left_sorter is None:
self._left_sorter = np.argsort(self.left)
return self._left_sorter

@property
def right_sorter(self):
def right_sorter(self) -> np.ndarray:
"""How to sort the right labels
"""
if self._right_sorter is None:
self._right_sorter = np.argsort(self.right)
return self._right_sorter

@property
def is_overlapping(self):
def is_overlapping(self) -> bool:
"""
Determine if the IntervalTree contains overlapping intervals.
Cached as self._is_overlapping.
Expand All @@ -109,7 +109,7 @@ cdef class IntervalTree(IntervalMixin):
return self._is_overlapping

@property
def is_monotonic_increasing(self):
def is_monotonic_increasing(self) -> bool:
"""
Return True if the IntervalTree is monotonic increasing (only equal or
increasing values), else False
Expand All @@ -119,7 +119,7 @@ cdef class IntervalTree(IntervalMixin):
sort_order = np.lexsort(values)
return is_monotonic(sort_order, False)[0]

def get_indexer(self, scalar_t[:] target):
def get_indexer(self, scalar_t[:] target) -> np.ndarray:
"""Return the positions corresponding to unique intervals that overlap
with the given array of scalar targets.
"""
Expand Down Expand Up @@ -180,7 +180,7 @@ cdef class IntervalTree(IntervalMixin):
n_elements=self.root.n_elements))

# compat with IndexEngine interface
def clear_mapping(self):
def clear_mapping(self) -> None:
pass


Expand Down
2 changes: 2 additions & 0 deletions pandas/_libs/sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ cdef class BlockIndex(SparseIndex):
pass


@cython.internal
cdef class BlockMerge:
"""
Object-oriented approach makes sharing state between recursive functions a
Expand Down Expand Up @@ -661,6 +662,7 @@ cdef class BlockMerge:
self.yi = xi


@cython.internal
cdef class BlockUnion(BlockMerge):
"""
Object-oriented approach makes sharing state between recursive functions a
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/conversion.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cdef _TSObject convert_datetime_to_tsobject(datetime ts, tzinfo tz,

cdef int64_t get_datetime64_nanos(object val) except? -1

cpdef datetime localize_pydatetime(datetime dt, object tz)
cpdef datetime localize_pydatetime(datetime dt, tzinfo tz)
cdef int64_t cast_from_unit(object ts, str unit) except? -1
cpdef (int64_t, int) precision_from_unit(str unit)

Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -810,14 +810,14 @@ cdef inline datetime _localize_pydatetime(datetime dt, tzinfo tz):
return dt.replace(tzinfo=tz)


cpdef inline datetime localize_pydatetime(datetime dt, object tz):
cpdef inline datetime localize_pydatetime(datetime dt, tzinfo tz):
"""
Take a datetime/Timestamp in UTC and localizes to timezone tz.

Parameters
----------
dt : datetime or Timestamp
tz : tzinfo, "UTC", or None
tz : tzinfo or None

Returns
-------
Expand Down