Skip to content

REF/BUG: DTA/TDA/PA comparison ops inconsistencies #30637

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 4 commits into from
Jan 3, 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
21 changes: 9 additions & 12 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,19 @@ def _dt_array_cmp(cls, op):
@unpack_zerodim_and_defer(opname)
def wrapper(self, other):

if isinstance(other, (datetime, np.datetime64, str)):
if isinstance(other, (datetime, np.datetime64)):
# GH#18435 strings get a pass from tzawareness compat
self._assert_tzawareness_compat(other)

if isinstance(other, str):
try:
other = _to_M8(other, tz=self.tz)
# GH#18435 strings get a pass from tzawareness compat
other = self._scalar_from_string(other)
except ValueError:
# string that cannot be parsed to Timestamp
return invalid_comparison(self, other, op)

result = op(self.asi8, other.view("i8"))
if isinstance(other, (datetime, np.datetime64)):
other = Timestamp(other)
self._assert_tzawareness_compat(other)

result = op(self.asi8, other.value)
if isna(other):
result.fill(nat_result)
elif lib.is_scalar(other) or np.ndim(other) == 0:
Expand All @@ -164,9 +165,7 @@ def wrapper(self, other):
other = type(self)._from_sequence(other)
except ValueError:
other = np.array(other, dtype=np.object_)
elif not isinstance(
other, (np.ndarray, ABCIndexClass, ABCSeries, DatetimeArray)
):
elif not isinstance(other, (np.ndarray, DatetimeArray)):
# Following Timestamp convention, __eq__ is all-False
# and __ne__ is all True, others raise TypeError.
return invalid_comparison(self, other, op)
Expand All @@ -185,8 +184,6 @@ def wrapper(self, other):
return invalid_comparison(self, other, op)
else:
self._assert_tzawareness_compat(other)
if isinstance(other, (ABCIndexClass, ABCSeries)):
other = other.array

if (
is_datetime64_dtype(other)
Expand Down
16 changes: 14 additions & 2 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from pandas.core.arrays import datetimelike as dtl
import pandas.core.common as com
from pandas.core.ops.common import unpack_zerodim_and_defer
from pandas.core.ops.invalid import invalid_comparison

from pandas.tseries import frequencies
from pandas.tseries.offsets import DateOffset, Tick, _delta_to_tick
Expand Down Expand Up @@ -75,6 +76,18 @@ def wrapper(self, other):
if is_list_like(other) and len(other) != len(self):
raise ValueError("Lengths must match")

if isinstance(other, str):
try:
other = self._scalar_from_string(other)
except ValueError:
# string that can't be parsed as Period
return invalid_comparison(self, other, op)
elif isinstance(other, int):
# TODO: sure we want to allow this? we dont for DTA/TDA
# 2 tests rely on this
other = Period(other, freq=self.freq)
result = ordinal_op(other.ordinal)

if isinstance(other, Period):
self._check_compatible_with(other)

Expand All @@ -93,8 +106,7 @@ def wrapper(self, other):
result = np.empty(len(self.asi8), dtype=bool)
result.fill(nat_result)
else:
other = Period(other, freq=self.freq)
result = ordinal_op(other.ordinal)
return invalid_comparison(self, other, op)

if self._hasnans:
result[self._isnan] = nat_result
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,16 @@ def _td_array_cmp(cls, op):
@unpack_zerodim_and_defer(opname)
def wrapper(self, other):

if _is_convertible_to_td(other) or other is NaT:
if isinstance(other, str):
try:
other = Timedelta(other)
other = self._scalar_from_string(other)
except ValueError:
# failed to parse as timedelta
return invalid_comparison(self, other, op)

if _is_convertible_to_td(other) or other is NaT:
other = Timedelta(other)

result = op(self.view("i8"), other.value)
if isna(other):
result.fill(nat_result)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/arithmetic/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from pandas.tseries.frequencies import to_offset

from .common import assert_invalid_comparison

# ------------------------------------------------------------------
# Comparisons

Expand All @@ -39,6 +41,15 @@ def test_compare_zerodim(self, box_with_array):
expected = tm.box_expected(expected, xbox)
tm.assert_equal(result, expected)

@pytest.mark.parametrize(
"scalar", ["foo", pd.Timestamp.now(), pd.Timedelta(days=4)]
)
def test_compare_invalid_scalar(self, box_with_array, scalar):
# comparison with scalar that cannot be interpreted as a Period
pi = pd.period_range("2000", periods=4)
parr = tm.box_expected(pi, box_with_array)
assert_invalid_comparison(parr, scalar, box_with_array)


class TestPeriodIndexComparisons:
# TODO: parameterize over boxes
Expand Down