Skip to content

Commit dc1bf59

Browse files
authored
BUG: DTI/TDI.equals with i8 (#36744)
1 parent 7322222 commit dc1bf59

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ Datetimelike
305305
- Bug in :meth:`DatetimeIndex.searchsorted`, :meth:`TimedeltaIndex.searchsorted`, :meth:`PeriodIndex.searchsorted`, and :meth:`Series.searchsorted` with ``datetime64``, ``timedelta64`` or ``Period`` dtype placement of ``NaT`` values being inconsistent with ``NumPy`` (:issue:`36176`, :issue:`36254`)
306306
- Inconsistency in :class:`DatetimeArray`, :class:`TimedeltaArray`, and :class:`PeriodArray` setitem casting arrays of strings to datetimelike scalars but not scalar strings (:issue:`36261`)
307307
- Bug in :class:`DatetimeIndex.shift` incorrectly raising when shifting empty indexes (:issue:`14811`)
308-
308+
- Bug in :meth:`DatetimeIndex.equals` and :meth:`TimedeltaIndex.equals` incorrectly considering ``int64`` indexes as equal (:issue:`36744`)
309309

310310
Timedelta
311311
^^^^^^^^^

pandas/core/indexes/datetimelike.py

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ def equals(self, other: object) -> bool:
134134

135135
if not isinstance(other, Index):
136136
return False
137+
elif other.dtype.kind in ["f", "i", "u", "c"]:
138+
return False
137139
elif not isinstance(other, type(self)):
138140
try:
139141
other = type(self)(other)

pandas/core/ops/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,9 @@ def _should_reindex_frame_op(
539539
if fill_value is None and level is None and axis is default_axis:
540540
# TODO: any other cases we should handle here?
541541
cols = left.columns.intersection(right.columns)
542-
if not (cols.equals(left.columns) and cols.equals(right.columns)):
542+
543+
if len(cols) and not (cols.equals(left.columns) and cols.equals(right.columns)):
544+
# TODO: is there a shortcut available when len(cols) == 0?
543545
return True
544546

545547
return False

pandas/tests/computation/test_eval.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1054,15 +1054,15 @@ def test_complex_series_frame_alignment(self, engine, parser):
10541054
m2, n, data_gen_f=f, r_idx_type=r2, c_idx_type=c2
10551055
)
10561056
index = getattr(locals().get(obj_name), index_name)
1057-
s = Series(np.random.randn(n), index[:n])
1057+
ser = Series(np.random.randn(n), index[:n])
10581058

10591059
if r2 == "dt" or c2 == "dt":
10601060
if engine == "numexpr":
1061-
expected2 = df2.add(s)
1061+
expected2 = df2.add(ser)
10621062
else:
1063-
expected2 = df2 + s
1063+
expected2 = df2 + ser
10641064
else:
1065-
expected2 = df2 + s
1065+
expected2 = df2 + ser
10661066

10671067
if r1 == "dt" or c1 == "dt":
10681068
if engine == "numexpr":
@@ -1072,11 +1072,11 @@ def test_complex_series_frame_alignment(self, engine, parser):
10721072
else:
10731073
expected = expected2 + df
10741074

1075-
if should_warn(df2.index, s.index, df.index):
1075+
if should_warn(df2.index, ser.index, df.index):
10761076
with tm.assert_produces_warning(RuntimeWarning):
1077-
res = pd.eval("df2 + s + df", engine=engine, parser=parser)
1077+
res = pd.eval("df2 + ser + df", engine=engine, parser=parser)
10781078
else:
1079-
res = pd.eval("df2 + s + df", engine=engine, parser=parser)
1079+
res = pd.eval("df2 + ser + df", engine=engine, parser=parser)
10801080
assert res.shape == expected.shape
10811081
tm.assert_frame_equal(res, expected)
10821082

pandas/tests/indexes/datetimelike.py

+7
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,10 @@ def test_getitem_preserves_freq(self):
108108

109109
result = index[:]
110110
assert result.freq == index.freq
111+
112+
def test_not_equals_numeric(self):
113+
index = self.create_index()
114+
115+
assert not index.equals(pd.Index(index.asi8))
116+
assert not index.equals(pd.Index(index.asi8.astype("u8")))
117+
assert not index.equals(pd.Index(index.asi8).astype("f8"))

0 commit comments

Comments
 (0)