Skip to content

BUG: fix array_equivalent with mismatched tzawareness #28508

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 20, 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
18 changes: 13 additions & 5 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ cimport pandas._libs.util as util
from pandas._libs.util cimport is_nan, UINT64_MAX, INT64_MAX, INT64_MIN

from pandas._libs.tslib import array_to_datetime
from pandas._libs.tslibs.nattype cimport NPY_NAT
from pandas._libs.tslibs.nattype import NaT
from pandas._libs.tslibs.nattype cimport NPY_NAT, c_NaT as NaT
from pandas._libs.tslibs.conversion cimport convert_to_tsobject
from pandas._libs.tslibs.timedeltas cimport convert_to_timedelta64
from pandas._libs.tslibs.timezones cimport get_timezone, tz_compare
Expand Down Expand Up @@ -525,9 +524,18 @@ def array_equivalent_object(left: object[:], right: object[:]) -> bool:

# we are either not equal or both nan
# I think None == None will be true here
if not (PyObject_RichCompareBool(x, y, Py_EQ) or
(x is None or is_nan(x)) and (y is None or is_nan(y))):
return False
try:
if not (PyObject_RichCompareBool(x, y, Py_EQ) or
(x is None or is_nan(x)) and (y is None or is_nan(y))):
return False
except TypeError as err:
# Avoid raising TypeError on tzawareness mismatch
# TODO: This try/except can be removed if/when Timestamp
# comparisons are change dto match datetime, see GH#28507
if "tz-naive and tz-aware" in str(err):
return False
raise

return True


Expand Down
10 changes: 8 additions & 2 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,14 @@ def array_equivalent(left, right, strict_nan=False):
if not isinstance(right_value, float) or not np.isnan(right_value):
return False
else:
if np.any(left_value != right_value):
return False
try:
if np.any(left_value != right_value):
return False
except TypeError as err:
if "Cannot compare tz-naive" in str(err):
# tzawareness compat failure, see GH#28507
return False
raise
return True

# NaNs can occur in float and complex arrays.
Expand Down
9 changes: 3 additions & 6 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4324,12 +4324,9 @@ def equals(self, other):
# if other is not object, use other's logic for coercion
return other.equals(self)

try:
return array_equivalent(
com.values_from_object(self), com.values_from_object(other)
)
except Exception:
return False
return array_equivalent(
com.values_from_object(self), com.values_from_object(other)
)

def identical(self, other):
"""
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/dtypes/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from pandas import DatetimeIndex, Float64Index, NaT, Series, TimedeltaIndex, date_range
from pandas.util import testing as tm

now = pd.Timestamp.now()
utcnow = pd.Timestamp.now("UTC")


@pytest.mark.parametrize("notna_f", [notna, notnull])
def test_notna_notnull(notna_f):
Expand Down Expand Up @@ -332,6 +335,29 @@ def test_array_equivalent():
assert not array_equivalent(DatetimeIndex([0, np.nan]), TimedeltaIndex([0, np.nan]))


@pytest.mark.parametrize(
"lvalue, rvalue",
[
# There are 3 variants for each of lvalue and rvalue. We include all
# three for the tz-naive `now` and exclude the datetim64 variant
# for utcnow because it drops tzinfo.
(now, utcnow),
(now.to_datetime64(), utcnow),
(now.to_pydatetime(), utcnow),
(now, utcnow),
(now.to_datetime64(), utcnow.to_pydatetime()),
(now.to_pydatetime(), utcnow.to_pydatetime()),
],
)
def test_array_equivalent_tzawareness(lvalue, rvalue):
# we shouldn't raise if comparing tzaware and tznaive datetimes
left = np.array([lvalue], dtype=object)
right = np.array([rvalue], dtype=object)

assert not array_equivalent(left, right, strict_nan=True)
assert not array_equivalent(left, right, strict_nan=False)


def test_array_equivalent_compat():
# see gh-13388
m = np.array([(1, 2), (3, 4)], dtype=[("a", int), ("b", float)])
Expand Down