Skip to content

Commit 5b6c6cf

Browse files
committed
BUG: Catch overflow error when converting to datetime (#45506)
Exception is raised when year is greater than 32 bit signed integer Added tests for this issue TODO: Remove this when its fixed in Python
1 parent 8456833 commit 5b6c6cf

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pandas/core/arrays/datetimes.py

+4
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,10 @@ def objects_to_datetime64ns(
22152215
return values.view("i8"), tz_parsed
22162216
except (ValueError, TypeError):
22172217
raise err
2218+
except OverflowError as err:
2219+
# Exception is raised when a part of date is greater than 32 bit signed int
2220+
# TODO: Remove this when its fixed in Python
2221+
raise ValueError("datetime out of range") from err
22182222

22192223
if tz_parsed is not None:
22202224
# We can take a shortcut since the datetime64 numpy array

pandas/tests/arrays/test_array.py

+15
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,21 @@ def test_array_not_registered(registry_without_decimal):
393393
tm.assert_equal(result, expected)
394394

395395

396+
def test_datetime64_eq_string():
397+
# Issue https://github.com/pandas-dev/pandas/issues/45506
398+
# Catch OverflowError when comparing datetime64 and string
399+
data = [
400+
{"f_0": "2015-07-01", "f_2": "08335394550"},
401+
{"f_0": "2015-07-02", "f_2": "+49 (0) 0345 300033"},
402+
{"f_0": "2015-07-03", "f_2": "+49(0)2598 04457"},
403+
{"f_0": "2015-07-04", "f_2": "0741470003"},
404+
{"f_0": "2015-07-05", "f_2": "04181 83668"},
405+
]
406+
dtypes = {"f_0": "datetime64[ns]", "f_2": "string"}
407+
df = pd.DataFrame(data=data).astype(dtypes)
408+
assert not df["f_0"].eq(df["f_2"]).any()
409+
410+
396411
class TestArrayAnalytics:
397412
def test_searchsorted(self, string_dtype):
398413
arr = pd.array(["a", "b", "c"], dtype=string_dtype)

0 commit comments

Comments
 (0)