Skip to content

Commit b8732fe

Browse files
jbrockmendelproost
authored andcommitted
CLN: assorted, mostly typing (pandas-dev#29419)
1 parent 77a8200 commit b8732fe

17 files changed

+238
-206
lines changed

pandas/_libs/algos.pyx

+71
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,77 @@ def rank_2d(rank_t[:, :] in_arr, axis=0, ties_method='average',
11501150
return ranks
11511151

11521152

1153+
ctypedef fused diff_t:
1154+
float64_t
1155+
float32_t
1156+
int8_t
1157+
int16_t
1158+
int32_t
1159+
int64_t
1160+
1161+
ctypedef fused out_t:
1162+
float32_t
1163+
float64_t
1164+
1165+
1166+
@cython.boundscheck(False)
1167+
@cython.wraparound(False)
1168+
def diff_2d(ndarray[diff_t, ndim=2] arr,
1169+
ndarray[out_t, ndim=2] out,
1170+
Py_ssize_t periods, int axis):
1171+
cdef:
1172+
Py_ssize_t i, j, sx, sy, start, stop
1173+
bint f_contig = arr.flags.f_contiguous
1174+
1175+
# Disable for unsupported dtype combinations,
1176+
# see https://github.com/cython/cython/issues/2646
1177+
if (out_t is float32_t
1178+
and not (diff_t is float32_t or diff_t is int8_t or diff_t is int16_t)):
1179+
raise NotImplementedError
1180+
elif (out_t is float64_t
1181+
and (diff_t is float32_t or diff_t is int8_t or diff_t is int16_t)):
1182+
raise NotImplementedError
1183+
else:
1184+
# We put this inside an indented else block to avoid cython build
1185+
# warnings about unreachable code
1186+
sx, sy = (<object>arr).shape
1187+
with nogil:
1188+
if f_contig:
1189+
if axis == 0:
1190+
if periods >= 0:
1191+
start, stop = periods, sx
1192+
else:
1193+
start, stop = 0, sx + periods
1194+
for j in range(sy):
1195+
for i in range(start, stop):
1196+
out[i, j] = arr[i, j] - arr[i - periods, j]
1197+
else:
1198+
if periods >= 0:
1199+
start, stop = periods, sy
1200+
else:
1201+
start, stop = 0, sy + periods
1202+
for j in range(start, stop):
1203+
for i in range(sx):
1204+
out[i, j] = arr[i, j] - arr[i, j - periods]
1205+
else:
1206+
if axis == 0:
1207+
if periods >= 0:
1208+
start, stop = periods, sx
1209+
else:
1210+
start, stop = 0, sx + periods
1211+
for i in range(start, stop):
1212+
for j in range(sy):
1213+
out[i, j] = arr[i, j] - arr[i - periods, j]
1214+
else:
1215+
if periods >= 0:
1216+
start, stop = periods, sy
1217+
else:
1218+
start, stop = 0, sy + periods
1219+
for i in range(sx):
1220+
for j in range(start, stop):
1221+
out[i, j] = arr[i, j] - arr[i, j - periods]
1222+
1223+
11531224
# generated from template
11541225
include "algos_common_helper.pxi"
11551226
include "algos_take_helper.pxi"

pandas/_libs/algos_common_helper.pxi.in

-71
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,6 @@ Template for each `dtype` helper function using 1-d template
44
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
55
"""
66

7-
ctypedef fused diff_t:
8-
float64_t
9-
float32_t
10-
int8_t
11-
int16_t
12-
int32_t
13-
int64_t
14-
15-
ctypedef fused out_t:
16-
float32_t
17-
float64_t
18-
19-
20-
@cython.boundscheck(False)
21-
@cython.wraparound(False)
22-
def diff_2d(ndarray[diff_t, ndim=2] arr,
23-
ndarray[out_t, ndim=2] out,
24-
Py_ssize_t periods, int axis):
25-
cdef:
26-
Py_ssize_t i, j, sx, sy, start, stop
27-
bint f_contig = arr.flags.f_contiguous
28-
29-
# Disable for unsupported dtype combinations,
30-
# see https://github.com/cython/cython/issues/2646
31-
if (out_t is float32_t
32-
and not (diff_t is float32_t or diff_t is int8_t or diff_t is int16_t)):
33-
raise NotImplementedError
34-
elif (out_t is float64_t
35-
and (diff_t is float32_t or diff_t is int8_t or diff_t is int16_t)):
36-
raise NotImplementedError
37-
else:
38-
# We put this inside an indented else block to avoid cython build
39-
# warnings about unreachable code
40-
sx, sy = (<object>arr).shape
41-
with nogil:
42-
if f_contig:
43-
if axis == 0:
44-
if periods >= 0:
45-
start, stop = periods, sx
46-
else:
47-
start, stop = 0, sx + periods
48-
for j in range(sy):
49-
for i in range(start, stop):
50-
out[i, j] = arr[i, j] - arr[i - periods, j]
51-
else:
52-
if periods >= 0:
53-
start, stop = periods, sy
54-
else:
55-
start, stop = 0, sy + periods
56-
for j in range(start, stop):
57-
for i in range(sx):
58-
out[i, j] = arr[i, j] - arr[i, j - periods]
59-
else:
60-
if axis == 0:
61-
if periods >= 0:
62-
start, stop = periods, sx
63-
else:
64-
start, stop = 0, sx + periods
65-
for i in range(start, stop):
66-
for j in range(sy):
67-
out[i, j] = arr[i, j] - arr[i - periods, j]
68-
else:
69-
if periods >= 0:
70-
start, stop = periods, sy
71-
else:
72-
start, stop = 0, sy + periods
73-
for i in range(sx):
74-
for j in range(start, stop):
75-
out[i, j] = arr[i, j] - arr[i, j - periods]
76-
77-
787
# ----------------------------------------------------------------------
798
# ensure_dtype
809
# ----------------------------------------------------------------------

pandas/_libs/missing.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ cpdef ndarray[uint8_t] isnaobj(ndarray arr):
121121

122122
@cython.wraparound(False)
123123
@cython.boundscheck(False)
124-
def isnaobj_old(ndarray arr):
124+
def isnaobj_old(arr: ndarray) -> ndarray:
125125
"""
126126
Return boolean mask denoting which elements of a 1-D array are na-like,
127127
defined as being any of:
@@ -156,7 +156,7 @@ def isnaobj_old(ndarray arr):
156156

157157
@cython.wraparound(False)
158158
@cython.boundscheck(False)
159-
def isnaobj2d(ndarray arr):
159+
def isnaobj2d(arr: ndarray) -> ndarray:
160160
"""
161161
Return boolean mask denoting which elements of a 2-D array are na-like,
162162
according to the criteria defined in `checknull`:
@@ -198,7 +198,7 @@ def isnaobj2d(ndarray arr):
198198

199199
@cython.wraparound(False)
200200
@cython.boundscheck(False)
201-
def isnaobj2d_old(ndarray arr):
201+
def isnaobj2d_old(arr: ndarray) -> ndarray:
202202
"""
203203
Return boolean mask denoting which elements of a 2-D array are na-like,
204204
according to the criteria defined in `checknull_old`:

pandas/_libs/window.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ def _check_minp(win, minp, N, floor=None) -> int:
6969
if not util.is_integer_object(minp):
7070
raise ValueError("min_periods must be an integer")
7171
if minp > win:
72-
raise ValueError("min_periods (%d) must be <= "
73-
"window (%d)" % (minp, win))
72+
raise ValueError("min_periods (minp) must be <= "
73+
"window (win)".format(minp=minp, win=win))
7474
elif minp > N:
7575
minp = N + 1
7676
elif minp < 0:

pandas/core/base.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def _selected_obj(self):
207207
return self.obj[self._selection]
208208

209209
@cache_readonly
210-
def ndim(self):
210+
def ndim(self) -> int:
211211
return self._selected_obj.ndim
212212

213213
@cache_readonly
@@ -339,7 +339,7 @@ def _aggregate(self, arg, *args, **kwargs):
339339

340340
obj = self._selected_obj
341341

342-
def nested_renaming_depr(level=4):
342+
def nested_renaming_depr(level: int = 4):
343343
# deprecation of nested renaming
344344
# GH 15931
345345
msg = textwrap.dedent(
@@ -488,11 +488,11 @@ def _agg(arg, func):
488488

489489
# combine results
490490

491-
def is_any_series():
491+
def is_any_series() -> bool:
492492
# return a boolean if we have *any* nested series
493493
return any(isinstance(r, ABCSeries) for r in result.values())
494494

495-
def is_any_frame():
495+
def is_any_frame() -> bool:
496496
# return a boolean if we have *any* nested series
497497
return any(isinstance(r, ABCDataFrame) for r in result.values())
498498

pandas/core/groupby/categorical.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
)
99

1010

11-
def recode_for_groupby(c, sort, observed):
11+
def recode_for_groupby(c: Categorical, sort: bool, observed: bool):
1212
"""
1313
Code the categories to ensure we can groupby for categoricals.
1414
@@ -74,7 +74,7 @@ def recode_for_groupby(c, sort, observed):
7474
return c.reorder_categories(cat.categories), None
7575

7676

77-
def recode_from_groupby(c, sort, ci):
77+
def recode_from_groupby(c: Categorical, sort: bool, ci):
7878
"""
7979
Reverse the codes_to_groupby to account for sort / observed.
8080

0 commit comments

Comments
 (0)