diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index e8b490edf8080..f1167a715afeb 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -302,6 +302,7 @@ Deprecations - Deprecated strings ``T``, ``t``, ``L`` and ``l`` denoting units in :func:`to_timedelta` (:issue:`52536`) - Deprecated the "method" and "limit" keywords on :meth:`Series.fillna`, :meth:`DataFrame.fillna`, :meth:`SeriesGroupBy.fillna`, :meth:`DataFrameGroupBy.fillna`, and :meth:`Resampler.fillna`, use ``obj.bfill()`` or ``obj.ffill()`` instead (:issue:`53394`) - Deprecated the ``method`` and ``limit`` keywords in :meth:`DataFrame.replace` and :meth:`Series.replace` (:issue:`33302`) +- Deprecated the use of non-supported datetime64 and timedelta64 resolutions with :func:`pandas.array`. Supported resolutions are: "s", "ms", "us", "ns" resolutions (:issue:`53439`) - Deprecated values "pad", "ffill", "bfill", "backfill" for :meth:`Series.interpolate` and :meth:`DataFrame.interpolate`, use ``obj.ffill()`` or ``obj.bfill()`` instead (:issue:`53581`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 9b4d67a20a7cd..76f18f55b4f1e 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -14,6 +14,7 @@ cast, overload, ) +import warnings import numpy as np from numpy import ma @@ -31,6 +32,7 @@ DtypeObj, T, ) +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.base import ExtensionDtype from pandas.core.dtypes.cast import ( @@ -185,9 +187,8 @@ def array( Length: 2, dtype: str32 Finally, Pandas has arrays that mostly overlap with NumPy - - * :class:`arrays.DatetimeArray` - * :class:`arrays.TimedeltaArray` + * :class:`arrays.DatetimeArray` + * :class:`arrays.TimedeltaArray` When data with a ``datetime64[ns]`` or ``timedelta64[ns]`` dtype is passed, pandas will always return a ``DatetimeArray`` or ``TimedeltaArray`` @@ -379,6 +380,16 @@ def array( ): return TimedeltaArray._from_sequence(data, dtype=dtype, copy=copy) + elif lib.is_np_dtype(dtype, "mM"): + warnings.warn( + r"dt/td64 dtypes with 'm' and 'h' resolutions are deprecated." + r"Supported resolutions are 's', 'ms','us', and 'ns'. " + r"In future releases, 'm' and 'h' resolutions will be cast to the closest " + r"supported unit.", + FutureWarning, + stacklevel=find_stack_level(), + ) + return PandasArray._from_sequence(data, dtype=dtype, copy=copy) diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index 337cdaa26a3d4..06f8e24b18825 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -1,5 +1,6 @@ import datetime import decimal +import re import numpy as np import pytest @@ -28,6 +29,22 @@ ) +def test_dt64_array(): + # PR 53439 + dtype_unit_lst = ["M8[h]", "M8[m]", "m8[h]", "M8[m]"] + + for unit in dtype_unit_lst: + dtype_var = np.dtype(unit) + msg = ( + r"dt/td64 dtypes with 'm' and 'h' resolutions are deprecated." + r"Supported resolutions are 's', 'ms','us', and 'ns'. " + r"In future releases, 'm' and 'h' resolutions will be cast to the " + r"closest supported unit." + ) + with tm.assert_produces_warning(FutureWarning, match=re.escape(msg)): + pd.array([], dtype=dtype_var) + + @pytest.mark.parametrize( "data, dtype, expected", [