Skip to content

Fixes #45506 Catch overflow error when converting to datetime #45532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ Conversion
- Bug in :meth:`Float64Index.astype` to unsigned integer dtype incorrectly casting to ``np.int64`` dtype (:issue:`45309`)
- 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`)
- Bug in :func:`array` with ``FloatingDtype`` and values containing float-castable strings incorrectly raising (:issue:`45424`)
-
- Bug when comparing string and datetime64ns objects causing ``OverflowError`` exception. (:issue:`45506`)

Strings
^^^^^^^
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
tzconversion,
)
from pandas._typing import npt
from pandas.errors import PerformanceWarning
from pandas.errors import (
OutOfBoundsDatetime,
PerformanceWarning,
)
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import validate_inclusive

Expand Down Expand Up @@ -2215,6 +2218,9 @@ def objects_to_datetime64ns(
return values.view("i8"), tz_parsed
except (ValueError, TypeError):
raise err
except OverflowError as err:
# Exception is raised when a part of date is greater than 32 bit signed int
raise OutOfBoundsDatetime("Out of bounds nanosecond timestamp") from err

if tz_parsed is not None:
# We can take a shortcut since the datetime64 numpy array
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/series/methods/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,28 @@ def test_compare_unaligned_objects():
ser1 = pd.Series([1, 2, 3])
ser2 = pd.Series([1, 2, 3, 4])
ser1.compare(ser2)


def test_compare_datetime64_and_string():
# Issue https://github.com/pandas-dev/pandas/issues/45506
# Catch OverflowError when comparing datetime64 and string
data = [
{"a": "2015-07-01", "b": "08335394550"},
{"a": "2015-07-02", "b": "+49 (0) 0345 300033"},
{"a": "2015-07-03", "b": "+49(0)2598 04457"},
{"a": "2015-07-04", "b": "0741470003"},
{"a": "2015-07-05", "b": "04181 83668"},
]
dtypes = {"a": "datetime64[ns]", "b": "string"}
df = pd.DataFrame(data=data).astype(dtypes)

result_eq1 = df["a"].eq(df["b"])
result_eq2 = df["a"] == df["b"]
result_neq = df["a"] != df["b"]

expected_eq = pd.Series([False] * 5) # For .eq and ==
expected_neq = pd.Series([True] * 5) # For !=

tm.assert_series_equal(result_eq1, expected_eq)
tm.assert_series_equal(result_eq2, expected_eq)
tm.assert_series_equal(result_neq, expected_neq)