Skip to content

Commit 8a12f16

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 Added whatsnew entry for v1.5.0
1 parent 5e40ff5 commit 8a12f16

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
@@ -244,7 +244,7 @@ Conversion
244244
- Bug in :meth:`Float64Index.astype` to unsigned integer dtype incorrectly casting to ``np.int64`` dtype (:issue:`45309`)
245245
- 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`)
246246
- Bug in :func:`array` with ``FloatingDtype`` and values containing float-castable strings incorrectly raising (:issue:`45424`)
247-
-
247+
- Bug in :meth:`objects_to_datetime64ns` throws OverflowError when converting string to datetime64ns. The issue was observed when comparing string and datetime64ns objects. (:issue:`45506`)
248248

249249
Strings
250250
^^^^^^^

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)