Skip to content

Commit f385fc0

Browse files
Fixes #45506 Catch overflow error when converting to datetime (#45532)
1 parent 4f0e22c commit f385fc0

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ Conversion
247247
- Bug in :meth:`Float64Index.astype` to unsigned integer dtype incorrectly casting to ``np.int64`` dtype (:issue:`45309`)
248248
- Bug in :meth:`Series.astype` and :meth:`DataFrame.astype` from floating dtype to unsigned integer dtype failing to raise in the presence of negative values (:issue:`45151`)
249249
- Bug in :func:`array` with ``FloatingDtype`` and values containing float-castable strings incorrectly raising (:issue:`45424`)
250-
-
250+
- Bug when comparing string and datetime64ns objects causing ``OverflowError`` exception. (:issue:`45506`)
251251

252252
Strings
253253
^^^^^^^

pandas/core/arrays/datetimes.py

+7-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,9 @@ 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+
raise OutOfBoundsDatetime("Out of bounds nanosecond timestamp") from err
22182224

22192225
if tz_parsed is not None:
22202226
# 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)