From f8b8d489d302b7b96713331b313d73cfeb8e4325 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 28 Mar 2024 19:28:19 -0700 Subject: [PATCH] DEPR: unsupported dt64/td64 dtypes in pd.array --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/core/construction.py | 11 +++-------- pandas/tests/arrays/test_array.py | 9 +++------ 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 2ebd6fb62b424..7ee31fdf88082 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -207,6 +207,7 @@ Removal of prior version deprecations/changes - All arguments except ``name`` in :meth:`Index.rename` are now keyword only (:issue:`56493`) - All arguments except the first ``path``-like argument in IO writers are now keyword only (:issue:`54229`) - Disallow passing a pandas type to :meth:`Index.view` (:issue:`55709`) +- Disallow units other than "s", "ms", "us", "ns" for datetime64 and timedelta64 dtypes in :func:`array` (:issue:`53817`) - Removed "freq" keyword from :class:`PeriodArray` constructor, use "dtype" instead (:issue:`52462`) - Removed deprecated "method" and "limit" keywords from :meth:`Series.replace` and :meth:`DataFrame.replace` (:issue:`53492`) - Removed the "closed" and "normalize" keywords in :meth:`DatetimeIndex.__new__` (:issue:`52628`) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index e6d99ab773db9..ec49340e9a516 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -15,7 +15,6 @@ cast, overload, ) -import warnings import numpy as np from numpy import ma @@ -35,7 +34,6 @@ DtypeObj, T, ) -from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.base import ExtensionDtype from pandas.core.dtypes.cast import ( @@ -373,13 +371,10 @@ def array( return TimedeltaArray._from_sequence(data, dtype=dtype, copy=copy) elif lib.is_np_dtype(dtype, "mM"): - warnings.warn( + raise ValueError( + # GH#53817 r"datetime64 and timedelta64 dtype resolutions other than " - r"'s', 'ms', 'us', and 'ns' are deprecated. " - r"In future releases passing unsupported resolutions will " - r"raise an exception.", - FutureWarning, - stacklevel=find_stack_level(), + r"'s', 'ms', 'us', and 'ns' are no longer supported." ) return NumpyExtensionArray._from_sequence(data, dtype=dtype, copy=copy) diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index a84fefebf044c..50dafb5dbbb06 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -1,6 +1,5 @@ import datetime import decimal -import re import numpy as np import pytest @@ -31,15 +30,13 @@ @pytest.mark.parametrize("dtype_unit", ["M8[h]", "M8[m]", "m8[h]"]) def test_dt64_array(dtype_unit): - # PR 53817 + # GH#53817 dtype_var = np.dtype(dtype_unit) msg = ( r"datetime64 and timedelta64 dtype resolutions other than " - r"'s', 'ms', 'us', and 'ns' are deprecated. " - r"In future releases passing unsupported resolutions will " - r"raise an exception." + r"'s', 'ms', 'us', and 'ns' are no longer supported." ) - with tm.assert_produces_warning(FutureWarning, match=re.escape(msg)): + with pytest.raises(ValueError, match=msg): pd.array([], dtype=dtype_var)