Skip to content
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

BUG: catch out-of-bounds datetime64 in Series/DataFrame constructor #26848

Merged
2 changes: 1 addition & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ def maybe_cast_to_datetime(value, dtype, errors='raise'):
dtype = value.dtype

if dtype.kind == 'M' and dtype != _NS_DTYPE:
value = value.astype(_NS_DTYPE)
value = tslibs.conversion.ensure_datetime64ns(value)

elif dtype.kind == 'm' and dtype != _TD_DTYPE:
value = to_timedelta(value)
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy.ma as ma

from pandas._libs import lib
from pandas._libs.tslibs import IncompatibleFrequency
from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime
from pandas.compat import raise_with_traceback

from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -701,6 +701,9 @@ def _try_cast(arr, take_fast_path, dtype, copy, raise_cast_failure):
elif not is_extension_type(subarr):
subarr = construct_1d_ndarray_preserving_na(subarr, dtype,
copy=copy)
except OutOfBoundsDatetime:
jreback marked this conversation as resolved.
Show resolved Hide resolved
if raise_cast_failure:
raise
except (ValueError, TypeError):
if is_categorical_dtype(dtype):
# We *do* allow casting to categorical, since we know
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,11 @@ def test_constructor_with_naive_string_and_datetimetz_dtype(self, arg):
expected = Series(pd.Timestamp(arg)).dt.tz_localize('CET')
assert_series_equal(result, expected)

def test_constructor_datetime64_outofbound(self):
# GH-26206 out of bound non-ns unit
with pytest.raises(pd.errors.OutOfBoundsDatetime):
pd.Series(np.array(['2262-04-12'], dtype='datetime64[D]'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also test the minimum date as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's relying on a general routine that does this bound checking and which is already tested thoroughly through other means, here I mainly want to test to ensure this routine is used. Therefore, I decided to keep the tests here a bit simpler (adding is not that hard, but it just complicates the test further, and there are already several cases). Is that OK?


def test_construction_interval(self):
# construction from interval & array of intervals
index = IntervalIndex.from_breaks(np.arange(3), closed='right')
Expand Down