Skip to content

REF: collect IntervalArray methods by topic #36438

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 3 commits into from
Sep 18, 2020
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
68 changes: 37 additions & 31 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@
DTScalarOrNaT = Union[DatetimeLikeScalar, NaTType]


class InvalidComparison(Exception):
"""
Raised by _validate_comparison_value to indicate to caller it should
return invalid_comparison.
"""

pass


def _datetimelike_array_cmp(cls, op):
"""
Wrap comparison operations to convert Timestamp/Timedelta/Period-like to
Expand All @@ -75,44 +84,14 @@ def _datetimelike_array_cmp(cls, op):
opname = f"__{op.__name__}__"
nat_result = opname == "__ne__"

class InvalidComparison(Exception):
pass

def _validate_comparison_value(self, other):
if isinstance(other, str):
try:
# GH#18435 strings get a pass from tzawareness compat
other = self._scalar_from_string(other)
except ValueError:
# failed to parse as Timestamp/Timedelta/Period
raise InvalidComparison(other)

if isinstance(other, self._recognized_scalars) or other is NaT:
other = self._scalar_type(other)
self._check_compatible_with(other)

elif not is_list_like(other):
raise InvalidComparison(other)

elif len(other) != len(self):
raise ValueError("Lengths must match")

else:
try:
other = self._validate_listlike(other, opname, allow_object=True)
except TypeError as err:
raise InvalidComparison(other) from err

return other

@unpack_zerodim_and_defer(opname)
def wrapper(self, other):
if self.ndim > 1 and getattr(other, "shape", None) == self.shape:
# TODO: handle 2D-like listlikes
return op(self.ravel(), other.ravel()).reshape(self.shape)

try:
other = _validate_comparison_value(self, other)
other = self._validate_comparison_value(other, opname)
except InvalidComparison:
return invalid_comparison(self, other, op)

Expand Down Expand Up @@ -696,6 +675,33 @@ def _from_factorized(cls, values, original):
# Validation Methods
# TODO: try to de-duplicate these, ensure identical behavior

def _validate_comparison_value(self, other, opname: str):
if isinstance(other, str):
try:
# GH#18435 strings get a pass from tzawareness compat
other = self._scalar_from_string(other)
except ValueError:
# failed to parse as Timestamp/Timedelta/Period
raise InvalidComparison(other)

if isinstance(other, self._recognized_scalars) or other is NaT:
other = self._scalar_type(other) # type: ignore[call-arg]
self._check_compatible_with(other)

elif not is_list_like(other):
raise InvalidComparison(other)

elif len(other) != len(self):
raise ValueError("Lengths must match")

else:
try:
other = self._validate_listlike(other, opname, allow_object=True)
except TypeError as err:
raise InvalidComparison(other) from err

return other

def _validate_fill_value(self, fill_value):
"""
If a fill_value is passed to `take` convert it to an i8 representation,
Expand Down
Loading