Skip to content

Commit 510951b

Browse files
committed
ENH: Add test for to_datetime with scalar out of bounds value
1 parent a4e8149 commit 510951b

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

pandas/tests/tools/test_to_datetime.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""test to_datetime"""
22

33
import calendar
4+
import locale
45
from collections import deque
56
from datetime import (
67
date,
@@ -9,12 +10,12 @@
910
timezone,
1011
)
1112
from decimal import Decimal
12-
import locale
1313
import zoneinfo
1414

15-
from dateutil.parser import parse
1615
import numpy as np
16+
import pandas as pd
1717
import pytest
18+
from dateutil.parser import parse
1819

1920
from pandas._libs import tslib
2021
from pandas._libs.tslibs import (
@@ -29,8 +30,6 @@
2930
import pandas.util._test_decorators as td
3031

3132
from pandas.core.dtypes.common import is_datetime64_ns_dtype
32-
33-
import pandas as pd
3433
from pandas import (
3534
DataFrame,
3635
DatetimeIndex,
@@ -3689,3 +3688,22 @@ def test_to_datetime_wrapped_datetime64_ps():
36893688
["1970-01-01 00:00:01.901901901"], dtype="datetime64[ns]", freq=None
36903689
)
36913690
tm.assert_index_equal(result, expected)
3691+
3692+
3693+
def test_to_datetime_scalar_out_of_bounds():
3694+
"""Ensure pd.to_datetime raises an error for out-of-bounds scalar values."""
3695+
uint64_max = np.iinfo("uint64").max
3696+
3697+
# Expect an OverflowError when passing uint64_max as a scalar
3698+
with pytest.raises(OutOfBoundsDatetime):
3699+
pd.to_datetime(uint64_max, unit="ns")
3700+
3701+
# Expect the same behavior when passing it as a list
3702+
with pytest.raises(OutOfBoundsDatetime):
3703+
pd.to_datetime([uint64_max], unit="ns")
3704+
3705+
# Test a valid value (should not raise an error)
3706+
valid_timestamp = 1_700_000_000_000_000_000 # A reasonable nanosecond timestamp
3707+
result = pd.to_datetime(valid_timestamp, unit="ns")
3708+
assert isinstance(result, pd.Timestamp)
3709+

0 commit comments

Comments
 (0)