Skip to content

CLN: assorted, mostly typing #29419

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
Nov 6, 2019
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
71 changes: 71 additions & 0 deletions pandas/_libs/algos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,77 @@ def rank_2d(rank_t[:, :] in_arr, axis=0, ties_method='average',
return ranks


ctypedef fused diff_t:
float64_t
float32_t
int8_t
int16_t
int32_t
int64_t

ctypedef fused out_t:
float32_t
float64_t


@cython.boundscheck(False)
@cython.wraparound(False)
def diff_2d(ndarray[diff_t, ndim=2] arr,
ndarray[out_t, ndim=2] out,
Py_ssize_t periods, int axis):
cdef:
Py_ssize_t i, j, sx, sy, start, stop
bint f_contig = arr.flags.f_contiguous

# Disable for unsupported dtype combinations,
# see https://github.com/cython/cython/issues/2646
if (out_t is float32_t
and not (diff_t is float32_t or diff_t is int8_t or diff_t is int16_t)):
raise NotImplementedError
elif (out_t is float64_t
and (diff_t is float32_t or diff_t is int8_t or diff_t is int16_t)):
raise NotImplementedError
else:
# We put this inside an indented else block to avoid cython build
# warnings about unreachable code
sx, sy = (<object>arr).shape
with nogil:
if f_contig:
if axis == 0:
if periods >= 0:
start, stop = periods, sx
else:
start, stop = 0, sx + periods
for j in range(sy):
for i in range(start, stop):
out[i, j] = arr[i, j] - arr[i - periods, j]
else:
if periods >= 0:
start, stop = periods, sy
else:
start, stop = 0, sy + periods
for j in range(start, stop):
for i in range(sx):
out[i, j] = arr[i, j] - arr[i, j - periods]
else:
if axis == 0:
if periods >= 0:
start, stop = periods, sx
else:
start, stop = 0, sx + periods
for i in range(start, stop):
for j in range(sy):
out[i, j] = arr[i, j] - arr[i - periods, j]
else:
if periods >= 0:
start, stop = periods, sy
else:
start, stop = 0, sy + periods
for i in range(sx):
for j in range(start, stop):
out[i, j] = arr[i, j] - arr[i, j - periods]


# generated from template
include "algos_common_helper.pxi"
include "algos_take_helper.pxi"
71 changes: 0 additions & 71 deletions pandas/_libs/algos_common_helper.pxi.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,6 @@ Template for each `dtype` helper function using 1-d template
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
"""

ctypedef fused diff_t:
float64_t
float32_t
int8_t
int16_t
int32_t
int64_t

ctypedef fused out_t:
float32_t
float64_t


@cython.boundscheck(False)
@cython.wraparound(False)
def diff_2d(ndarray[diff_t, ndim=2] arr,
ndarray[out_t, ndim=2] out,
Py_ssize_t periods, int axis):
cdef:
Py_ssize_t i, j, sx, sy, start, stop
bint f_contig = arr.flags.f_contiguous

# Disable for unsupported dtype combinations,
# see https://github.com/cython/cython/issues/2646
if (out_t is float32_t
and not (diff_t is float32_t or diff_t is int8_t or diff_t is int16_t)):
raise NotImplementedError
elif (out_t is float64_t
and (diff_t is float32_t or diff_t is int8_t or diff_t is int16_t)):
raise NotImplementedError
else:
# We put this inside an indented else block to avoid cython build
# warnings about unreachable code
sx, sy = (<object>arr).shape
with nogil:
if f_contig:
if axis == 0:
if periods >= 0:
start, stop = periods, sx
else:
start, stop = 0, sx + periods
for j in range(sy):
for i in range(start, stop):
out[i, j] = arr[i, j] - arr[i - periods, j]
else:
if periods >= 0:
start, stop = periods, sy
else:
start, stop = 0, sy + periods
for j in range(start, stop):
for i in range(sx):
out[i, j] = arr[i, j] - arr[i, j - periods]
else:
if axis == 0:
if periods >= 0:
start, stop = periods, sx
else:
start, stop = 0, sx + periods
for i in range(start, stop):
for j in range(sy):
out[i, j] = arr[i, j] - arr[i - periods, j]
else:
if periods >= 0:
start, stop = periods, sy
else:
start, stop = 0, sy + periods
for i in range(sx):
for j in range(start, stop):
out[i, j] = arr[i, j] - arr[i, j - periods]


# ----------------------------------------------------------------------
# ensure_dtype
# ----------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ cpdef ndarray[uint8_t] isnaobj(ndarray arr):

@cython.wraparound(False)
@cython.boundscheck(False)
def isnaobj_old(ndarray arr):
def isnaobj_old(arr: ndarray) -> ndarray:
"""
Return boolean mask denoting which elements of a 1-D array are na-like,
defined as being any of:
Expand Down Expand Up @@ -156,7 +156,7 @@ def isnaobj_old(ndarray arr):

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

@cython.wraparound(False)
@cython.boundscheck(False)
def isnaobj2d_old(ndarray arr):
def isnaobj2d_old(arr: ndarray) -> ndarray:
"""
Return boolean mask denoting which elements of a 2-D array are na-like,
according to the criteria defined in `checknull_old`:
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/window.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def _check_minp(win, minp, N, floor=None) -> int:
if not util.is_integer_object(minp):
raise ValueError("min_periods must be an integer")
if minp > win:
raise ValueError("min_periods (%d) must be <= "
"window (%d)" % (minp, win))
raise ValueError("min_periods (minp) must be <= "
"window (win)".format(minp=minp, win=win))
elif minp > N:
minp = N + 1
elif minp < 0:
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def _selected_obj(self):
return self.obj[self._selection]

@cache_readonly
def ndim(self):
def ndim(self) -> int:
return self._selected_obj.ndim

@cache_readonly
Expand Down Expand Up @@ -339,7 +339,7 @@ def _aggregate(self, arg, *args, **kwargs):

obj = self._selected_obj

def nested_renaming_depr(level=4):
def nested_renaming_depr(level: int = 4):
# deprecation of nested renaming
# GH 15931
msg = textwrap.dedent(
Expand Down Expand Up @@ -488,11 +488,11 @@ def _agg(arg, func):

# combine results

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

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

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)


def recode_for_groupby(c, sort, observed):
def recode_for_groupby(c: Categorical, sort: bool, observed: bool):
"""
Code the categories to ensure we can groupby for categoricals.

Expand Down Expand Up @@ -74,7 +74,7 @@ def recode_for_groupby(c, sort, observed):
return c.reorder_categories(cat.categories), None


def recode_from_groupby(c, sort, ci):
def recode_from_groupby(c: Categorical, sort: bool, ci):
"""
Reverse the codes_to_groupby to account for sort / observed.

Expand Down
Loading