Skip to content

Commit 7c7286c

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/frame/methods/test_astype.py TODO: Remove this when its fixed in Python
1 parent 2db3b0a commit 7c7286c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-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("datetime out of range") from err
22182225

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

pandas/tests/frame/methods/test_astype.py

+15
Original file line numberDiff line numberDiff line change
@@ -787,3 +787,18 @@ def test_frame_astype_no_copy():
787787

788788
assert result.a.dtype == pd.Int16Dtype()
789789
assert np.shares_memory(df.b.values, result.b.values)
790+
791+
792+
def test_datetime64_eq_string():
793+
# Issue https://github.com/pandas-dev/pandas/issues/45506
794+
# Catch OverflowError when comparing datetime64 and string
795+
data = [
796+
{"a": "2015-07-01", "b": "08335394550"},
797+
{"a": "2015-07-02", "b": "+49 (0) 0345 300033"},
798+
{"a": "2015-07-03", "b": "+49(0)2598 04457"},
799+
{"a": "2015-07-04", "b": "0741470003"},
800+
{"a": "2015-07-05", "b": "04181 83668"},
801+
]
802+
dtypes = {"a": "datetime64[ns]", "b": "string"}
803+
df = DataFrame(data=data).astype(dtypes)
804+
tm.assert_series_equal(df["a"].eq(df["b"]), Series([False] * 5))

0 commit comments

Comments
 (0)