Skip to content

DOC/STY: Cleaned pandas/_libs/{internals,interval}.pyx #30157

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 2 commits into from
Dec 10, 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
7 changes: 1 addition & 6 deletions pandas/_libs/internals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down
57 changes: 24 additions & 33 deletions pandas/_libs/interval.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,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 self.closed in ('left', 'both')

Expand All @@ -56,8 +55,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 self.closed in ('right', 'both')

Expand All @@ -71,8 +69,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_left

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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):
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))

Expand All @@ -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)
Expand Down Expand Up @@ -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
--------
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down