Skip to content

Commit 2e5c399

Browse files
jbrockmendelNico Cernek
authored and
Nico Cernek
committed
CLN+TST: Catch specific exception in equals (pandas-dev#28532)
1 parent 048a495 commit 2e5c399

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

pandas/core/arrays/datetimes.py

+3
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,9 @@ def sequence_to_dt64ns(
19181918
tz = validate_tz_from_dtype(dtype, tz)
19191919

19201920
if isinstance(data, ABCIndexClass):
1921+
if data.nlevels > 1:
1922+
# Without this check, data._data below is None
1923+
raise TypeError("Cannot create a DatetimeArray from a MultiIndex.")
19211924
data = data._data
19221925

19231926
# By this point we are assured to have either a numpy array or Index

pandas/core/indexes/datetimelike.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@ def equals(self, other):
192192
elif not isinstance(other, type(self)):
193193
try:
194194
other = type(self)(other)
195-
except Exception:
195+
except (ValueError, TypeError, OverflowError):
196+
# e.g.
197+
# ValueError -> cannot parse str entry, or OutOfBoundsDatetime
198+
# TypeError -> trying to convert IntervalIndex to DatetimeIndex
199+
# OverflowError -> Index([very_large_timedeltas])
196200
return False
197201

198202
if not is_dtype_equal(self.dtype, other.dtype):

pandas/tests/arrays/test_datetimes.py

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616

1717
class TestDatetimeArrayConstructor:
18+
def test_from_sequence_invalid_type(self):
19+
mi = pd.MultiIndex.from_product([np.arange(5), np.arange(5)])
20+
with pytest.raises(TypeError, match="Cannot create a DatetimeArray"):
21+
DatetimeArray._from_sequence(mi)
22+
1823
def test_only_1dim_accepted(self):
1924
arr = np.array([0, 1, 2, 3], dtype="M8[h]").astype("M8[ns]")
2025

pandas/tests/indexes/datetimes/test_ops.py

+12
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,18 @@ def test_equals(self):
393393
assert not idx.equals(list(idx3))
394394
assert not idx.equals(pd.Series(idx3))
395395

396+
# check that we do not raise when comparing with OutOfBounds objects
397+
oob = pd.Index([datetime(2500, 1, 1)] * 3, dtype=object)
398+
assert not idx.equals(oob)
399+
assert not idx2.equals(oob)
400+
assert not idx3.equals(oob)
401+
402+
# check that we do not raise when comparing with OutOfBounds dt64
403+
oob2 = oob.map(np.datetime64)
404+
assert not idx.equals(oob2)
405+
assert not idx2.equals(oob2)
406+
assert not idx3.equals(oob2)
407+
396408
@pytest.mark.parametrize("values", [["20180101", "20180103", "20180105"], []])
397409
@pytest.mark.parametrize("freq", ["2D", Day(2), "2B", BDay(2), "48H", Hour(48)])
398410
@pytest.mark.parametrize("tz", [None, "US/Eastern"])

pandas/tests/indexes/timedeltas/test_ops.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from datetime import timedelta
2+
13
import numpy as np
24
import pytest
35

@@ -266,6 +268,17 @@ def test_equals(self):
266268
assert not idx.equals(list(idx2))
267269
assert not idx.equals(pd.Series(idx2))
268270

271+
# Check that we dont raise OverflowError on comparisons outside the
272+
# implementation range
273+
oob = pd.Index([timedelta(days=10 ** 6)] * 3, dtype=object)
274+
assert not idx.equals(oob)
275+
assert not idx2.equals(oob)
276+
277+
# FIXME: oob.apply(np.timedelta64) incorrectly overflows
278+
oob2 = pd.Index([np.timedelta64(x) for x in oob], dtype=object)
279+
assert not idx.equals(oob2)
280+
assert not idx2.equals(oob2)
281+
269282
@pytest.mark.parametrize("values", [["0 days", "2 days", "4 days"], []])
270283
@pytest.mark.parametrize("freq", ["2D", Day(2), "48H", Hour(48)])
271284
def test_freq_setter(self, values, freq):

0 commit comments

Comments
 (0)