From 9a107ade422d0b8337094b3fad5512a3d8f42ff7 Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Mon, 9 Dec 2019 15:39:11 +0200 Subject: [PATCH 1/2] STY/TYP --- pandas/_libs/internals.pyx | 7 +--- pandas/_libs/interval.pyx | 69 +++++++++++++++++--------------------- 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/pandas/_libs/internals.pyx b/pandas/_libs/internals.pyx index 603caed805e58..7be8b1b152fae 100644 --- a/pandas/_libs/internals.pyx +++ b/pandas/_libs/internals.pyx @@ -243,7 +243,6 @@ cpdef Py_ssize_t slice_len( - if ``s.step < 0``, ``s.start`` is not ``None`` Otherwise, the result is unreliable. - """ cdef: Py_ssize_t start, stop, step, length @@ -263,7 +262,6 @@ cdef slice_get_indices_ex(slice slc, Py_ssize_t objlen=PY_SSIZE_T_MAX): If `objlen` is not specified, slice must be bounded, otherwise the result will be wrong. - """ cdef: Py_ssize_t start, stop, step, length @@ -365,7 +363,6 @@ def get_blkno_indexers(int64_t[:] blknos, bint group=True): Returns ------- iter : iterator of (int, slice or array) - """ # There's blkno in this function's name because it's used in block & # blockno handling. @@ -436,18 +433,16 @@ def get_blkno_indexers(int64_t[:] blknos, bint group=True): def get_blkno_placements(blknos, group: bool = True): """ - Parameters ---------- blknos : array of int64 - group : bool + group : bool, default True Returns ------- iterator yield (BlockPlacement, blkno) """ - blknos = ensure_int64(blknos) for blkno, indexer in get_blkno_indexers(blknos, group): diff --git a/pandas/_libs/interval.pyx b/pandas/_libs/interval.pyx index a99ddc16ac3af..23b13eb3584ed 100644 --- a/pandas/_libs/interval.pyx +++ b/pandas/_libs/interval.pyx @@ -32,7 +32,7 @@ _VALID_CLOSED = frozenset(['left', 'right', 'both', 'neither']) cdef class IntervalMixin: @property - def closed_left(self): + def closed_left(self) -> bool: """ Check if the interval is closed on the left side. @@ -41,13 +41,12 @@ cdef class IntervalMixin: Returns ------- bool - ``True`` if the Interval is closed on the left-side, else - ``False``. + True if the Interval is closed on the left-side. """ return self.closed in ('left', 'both') @property - def closed_right(self): + def closed_right(self) -> bool: """ Check if the interval is closed on the right side. @@ -56,13 +55,12 @@ cdef class IntervalMixin: Returns ------- bool - ``True`` if the Interval is closed on the left-side, else - ``False``. + True if the Interval is closed on the left-side. """ return self.closed in ('right', 'both') @property - def open_left(self): + def open_left(self) -> bool: """ Check if the interval is open on the left side. @@ -71,13 +69,12 @@ cdef class IntervalMixin: Returns ------- bool - ``True`` if the Interval is closed on the left-side, else - ``False``. + True if the Interval is closed on the left-side. """ return not self.closed_left @property - def open_right(self): + def open_right(self) -> bool: """ Check if the interval is open on the right side. @@ -86,8 +83,7 @@ cdef class IntervalMixin: Returns ------- bool - ``True`` if the Interval is closed on the left-side, else - ``False``. + True if the Interval is closed on the left-side. """ return not self.closed_right @@ -308,16 +304,14 @@ cdef class Interval(IntervalMixin): self._validate_endpoint(right) if closed not in _VALID_CLOSED: - msg = f"invalid option for 'closed': {closed}" - raise ValueError(msg) + raise ValueError(f"invalid option for 'closed': {closed}") if not left <= right: - raise ValueError('left side of interval must be <= right side') + raise ValueError("left side of interval must be <= right side") if (isinstance(left, Timestamp) and not tz_compare(left.tzinfo, right.tzinfo)): # GH 18538 - msg = (f"left and right must have the same time zone, got " - f"{repr(left.tzinfo)}' and {repr(right.tzinfo)}") - raise ValueError(msg) + raise ValueError("left and right must have the same time zone, got " + f"{repr(left.tzinfo)}' and {repr(right.tzinfo)}") self.left = left self.right = right self.closed = closed @@ -326,16 +320,15 @@ cdef class Interval(IntervalMixin): # GH 23013 if not (is_integer_object(endpoint) or is_float_object(endpoint) or isinstance(endpoint, (Timestamp, Timedelta))): - msg = ("Only numeric, Timestamp and Timedelta endpoints " - "are allowed when constructing an Interval.") - raise ValueError(msg) + raise ValueError("Only numeric, Timestamp and Timedelta endpoints " + "are allowed when constructing an Interval.") def __hash__(self): return hash((self.left, self.right, self.closed)) - def __contains__(self, key): + def __contains__(self, key) -> bool: if _interval_like(key): - raise TypeError('__contains__ not defined for two intervals') + raise TypeError("__contains__ not defined for two intervals") return ((self.left < key if self.open_left else self.left <= key) and (key < self.right if self.open_right else key <= self.right)) @@ -358,7 +351,7 @@ cdef class Interval(IntervalMixin): name = type(self).__name__ other = type(other).__name__ op_str = {Py_LT: '<', Py_LE: '<=', Py_GT: '>', Py_GE: '>='}[op] - raise TypeError(f'unorderable types: {name}() {op_str} {other}()') + raise TypeError(f"unorderable types: {name}() {op_str} {other}()") def __reduce__(self): args = (self.left, self.right, self.closed) @@ -424,7 +417,7 @@ cdef class Interval(IntervalMixin): self.left // y, self.right // y, closed=self.closed) return NotImplemented - def overlaps(self, other): + def overlaps(self, other) -> bool: """ Check whether two Interval objects overlap. @@ -437,12 +430,12 @@ cdef class Interval(IntervalMixin): Parameters ---------- other : Interval - The interval to check against for an overlap. + Interval to check against for an overlap. Returns ------- bool - ``True`` if the two intervals overlap, else ``False``. + True if the two intervals overlap. See Also -------- @@ -473,8 +466,8 @@ cdef class Interval(IntervalMixin): False """ if not isinstance(other, Interval): - msg = f'`other` must be an Interval, got {type(other).__name__}' - raise TypeError(msg) + raise TypeError("`other` must be an Interval, " + f"got {type(other).__name__}") # equality is okay if both endpoints are closed (overlap at a point) op1 = le if (self.closed_left and other.closed_right) else lt @@ -494,20 +487,19 @@ def intervals_to_interval_bounds(ndarray intervals, Parameters ---------- intervals : ndarray - object array of Intervals / nulls + Object array of Intervals / nulls. - validate_closed: boolean, default True - boolean indicating if all intervals must be closed on the same side. + validate_closed: bool, default True + Boolean indicating if all intervals must be closed on the same side. Mismatching closed will raise if True, else return None for closed. Returns ------- - tuples (left: ndarray object array, - right: ndarray object array, - closed: str) - + tuple of tuples + left : (ndarray, object, array) + right : (ndarray, object, array) + closed: str """ - cdef: object closed = None, interval int64_t n = len(intervals) @@ -536,8 +528,7 @@ def intervals_to_interval_bounds(ndarray intervals, elif closed != interval.closed: closed = None if validate_closed: - msg = 'intervals must all be closed on the same side' - raise ValueError(msg) + raise ValueError("intervals must all be closed on the same side") return left, right, closed From 4ebb8c3f4a3dc84091e5d85fb7fb33e10586862f Mon Sep 17 00:00:00 2001 From: MomIsBestFriend <> Date: Tue, 10 Dec 2019 13:59:32 +0200 Subject: [PATCH 2/2] Removed Cython annotations --- pandas/_libs/interval.pyx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/_libs/interval.pyx b/pandas/_libs/interval.pyx index 23b13eb3584ed..4293108ea7ec2 100644 --- a/pandas/_libs/interval.pyx +++ b/pandas/_libs/interval.pyx @@ -32,7 +32,7 @@ _VALID_CLOSED = frozenset(['left', 'right', 'both', 'neither']) cdef class IntervalMixin: @property - def closed_left(self) -> bool: + def closed_left(self): """ Check if the interval is closed on the left side. @@ -46,7 +46,7 @@ cdef class IntervalMixin: return self.closed in ('left', 'both') @property - def closed_right(self) -> bool: + def closed_right(self): """ Check if the interval is closed on the right side. @@ -60,7 +60,7 @@ cdef class IntervalMixin: return self.closed in ('right', 'both') @property - def open_left(self) -> bool: + def open_left(self): """ Check if the interval is open on the left side. @@ -74,7 +74,7 @@ cdef class IntervalMixin: return not self.closed_left @property - def open_right(self) -> bool: + def open_right(self): """ Check if the interval is open on the right side. @@ -326,7 +326,7 @@ cdef class Interval(IntervalMixin): def __hash__(self): return hash((self.left, self.right, self.closed)) - def __contains__(self, key) -> bool: + def __contains__(self, key): if _interval_like(key): raise TypeError("__contains__ not defined for two intervals") return ((self.left < key if self.open_left else self.left <= key) and @@ -417,7 +417,7 @@ cdef class Interval(IntervalMixin): self.left // y, self.right // y, closed=self.closed) return NotImplemented - def overlaps(self, other) -> bool: + def overlaps(self, other): """ Check whether two Interval objects overlap.