diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index f66098633b45e..d2269b8ef78e1 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -190,7 +190,8 @@ Datetimelike ^^^^^^^^^^^^ - Bug in :class:`DataFrame` and :class:`Series` constructors sometimes dropping nanoseconds from :class:`Timestamp` (resp. :class:`Timedelta`) ``data``, with ``dtype=datetime64[ns]`` (resp. ``timedelta64[ns]``) (:issue:`38032`) - Bug in :meth:`DataFrame.first` and :meth:`Series.first` returning two months for offset one month when first day is last calendar day (:issue:`29623`) -- Bug in constructing a :class:`DataFrame` or :class:`Series` with mismatched ``datetime64`` data and ``timedelta64`` dtype, or vice-versa, failing to raise ``TypeError`` (:issue:`38575`, :issue:`38764`) +- Bug in constructing a :class:`DataFrame` or :class:`Series` with mismatched ``datetime64`` data and ``timedelta64`` dtype, or vice-versa, failing to raise ``TypeError`` (:issue:`38575`, :issue:`38764`, :issue:`38792`) +- Bug in constructing a :class:`Series` or :class:`DataFrame` with a ``datetime`` object out of bounds for ``datetime64[ns]`` dtype (:issue:`38792`) - Bug in :meth:`DatetimeIndex.intersection`, :meth:`DatetimeIndex.symmetric_difference`, :meth:`PeriodIndex.intersection`, :meth:`PeriodIndex.symmetric_difference` always returning object-dtype when operating with :class:`CategoricalIndex` (:issue:`38741`) - Bug in :meth:`Series.where` incorrectly casting ``datetime64`` values to ``int64`` (:issue:`37682`) - diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 261b13e52777b..54a6f47ae1b38 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -615,9 +615,12 @@ def _try_cast(arr, dtype: Optional[DtypeObj], copy: bool, raise_cast_failure: bo except OutOfBoundsDatetime: # in case of out of bound datetime64 -> always raise raise - except (ValueError, TypeError): + except (ValueError, TypeError) as err: if dtype is not None and raise_cast_failure: raise + elif "Cannot cast" in str(err): + # via _disallow_mismatched_datetimelike + raise else: subarr = np.array(arr, dtype=object, copy=copy) return subarr diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 0915043f8fd46..1cfa9957874ac 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -170,7 +170,7 @@ def maybe_unbox_datetimelike(value: Scalar, dtype: DtypeObj) -> Scalar: return value -def _disallow_mismatched_datetimelike(value: DtypeObj, dtype: DtypeObj): +def _disallow_mismatched_datetimelike(value, dtype: DtypeObj): """ numpy allows np.array(dt64values, dtype="timedelta64[ns]") and vice-versa, but we do not want to allow this, so we need to @@ -725,7 +725,11 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj, dtype = np.dtype(object) elif isinstance(val, (np.datetime64, datetime)): - val = Timestamp(val) + try: + val = Timestamp(val) + except OutOfBoundsDatetime: + return np.dtype(object), val + if val is NaT or val.tz is None: dtype = np.dtype("M8[ns]") else: @@ -1472,6 +1476,8 @@ def maybe_cast_to_datetime(value, dtype: Optional[DtypeObj]): # we have an array of datetime or timedeltas & nulls elif np.prod(value.shape) or not is_dtype_equal(value.dtype, dtype): + _disallow_mismatched_datetimelike(value, dtype) + try: if is_datetime64: value = to_datetime(value, errors="raise") diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 94b2431650359..dcade94e0186c 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -2987,12 +2987,11 @@ def test_from_timedelta64_scalar_object(self, constructor, request): def test_from_scalar_datetimelike_mismatched(self, constructor, cls, request): node = request.node params = node.callspec.params - if params["frame_or_series"] is DataFrame and params["constructor"] is not None: + if params["frame_or_series"] is DataFrame and params["constructor"] is dict: mark = pytest.mark.xfail( reason="DataFrame incorrectly allows mismatched datetimelike" ) node.add_marker(mark) - scalar = cls("NaT", "ns") dtype = {np.datetime64: "m8[ns]", np.timedelta64: "M8[ns]"}[cls] @@ -3002,3 +3001,9 @@ def test_from_scalar_datetimelike_mismatched(self, constructor, cls, request): scalar = cls(4, "ns") with pytest.raises(TypeError, match="Cannot cast"): constructor(scalar, dtype=dtype) + + def test_from_out_of_bounds_datetime(self, constructor): + scalar = datetime(9999, 1, 1) + result = constructor(scalar) + + assert type(get1(result)) is datetime