Skip to content

Commit cdb324b

Browse files
committed
BUG: Catch overflow error when converting to datetime (#45506)
Exception is raised when a part of the date like day, month or year is greater than 32 bit signed integer. Added tests for this issue in pandas/tests/series/methods/test_compare.py TODO: Remove this when its fixed in Python
1 parent e950e00 commit cdb324b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pandas/core/arrays/datetimes.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
tzconversion,
3838
)
3939
from pandas._typing import npt
40-
from pandas.errors import PerformanceWarning
40+
from pandas.errors import (
41+
OutOfBoundsDatetime,
42+
PerformanceWarning,
43+
)
4144
from pandas.util._exceptions import find_stack_level
4245
from pandas.util._validators import validate_inclusive
4346

@@ -2215,6 +2218,10 @@ def objects_to_datetime64ns(
22152218
return values.view("i8"), tz_parsed
22162219
except (ValueError, TypeError):
22172220
raise err
2221+
except OverflowError as err:
2222+
# Exception is raised when a part of date is greater than 32 bit signed int
2223+
# TODO: Remove this when its fixed in Python
2224+
raise OutOfBoundsDatetime("Out of bounds nanosecond timestamp") from err
22182225

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

pandas/tests/series/methods/test_compare.py

+25
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,28 @@ def test_compare_unaligned_objects():
114114
ser1 = pd.Series([1, 2, 3])
115115
ser2 = pd.Series([1, 2, 3, 4])
116116
ser1.compare(ser2)
117+
118+
119+
def test_compare_datetime64_and_string():
120+
# Issue https://github.com/pandas-dev/pandas/issues/45506
121+
# Catch OverflowError when comparing datetime64 and string
122+
data = [
123+
{"a": "2015-07-01", "b": "08335394550"},
124+
{"a": "2015-07-02", "b": "+49 (0) 0345 300033"},
125+
{"a": "2015-07-03", "b": "+49(0)2598 04457"},
126+
{"a": "2015-07-04", "b": "0741470003"},
127+
{"a": "2015-07-05", "b": "04181 83668"},
128+
]
129+
dtypes = {"a": "datetime64[ns]", "b": "string"}
130+
df = pd.DataFrame(data=data).astype(dtypes)
131+
132+
result_eq1 = df["a"].eq(df["b"])
133+
result_eq2 = df["a"] == df["b"]
134+
result_neq = df["a"] != df["b"]
135+
136+
expected_eq = pd.Series([False] * 5) # For .eq and ==
137+
expected_neq = pd.Series([True] * 5) # For !=
138+
139+
tm.assert_series_equal(result_eq1, expected_eq)
140+
tm.assert_series_equal(result_eq2, expected_eq)
141+
tm.assert_series_equal(result_neq, expected_neq)

0 commit comments

Comments
 (0)