From 6fbe6196d399001e293915e3dcb1a491931dae6c Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Fri, 23 Jun 2023 12:10:30 -0400 Subject: [PATCH 1/8] Adding deprecation logic, unit test, and documentation --- doc/source/whatsnew/v2.1.0.rst | 2 ++ pandas/core/construction.py | 12 ++++++++++++ pandas/tests/arrays/test_array.py | 17 +++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 2436d91690ed3..f2e389d95dd1c 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -304,7 +304,9 @@ 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:`53058`) - Deprecated values "pad", "ffill", "bfill", "backfill" for :meth:`Series.interpolate` and :meth:`DataFrame.interpolate`, use ``obj.ffill()`` or ``obj.bfill()`` instead (:issue:`53581`) +- .. --------------------------------------------------------------------------- .. _whatsnew_210.performance: diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 9b4d67a20a7cd..97e2ce04c0fbb 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 ( @@ -379,6 +381,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", [ From 8f6505ac46323b6b69252b1f021a2b5462bde04f Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Tue, 27 Jun 2023 18:47:21 -0400 Subject: [PATCH 2/8] Updated documentation in unit test. --- pandas/tests/arrays/test_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index 06f8e24b18825..37c3f1abd2cff 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -30,7 +30,7 @@ def test_dt64_array(): - # PR 53439 + # PR 53817 dtype_unit_lst = ["M8[h]", "M8[m]", "m8[h]", "M8[m]"] for unit in dtype_unit_lst: From c5872b4454d8e8d8bb79c472e966a9ff0fb0e4e4 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Fri, 30 Jun 2023 14:42:09 -0400 Subject: [PATCH 3/8] Updating warning message based on reviewer recommendations. --- pandas/core/construction.py | 6 +++--- pandas/tests/arrays/test_array.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 97e2ce04c0fbb..0e84d26d7090c 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -383,9 +383,9 @@ def array( 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"datetime64 and timedelta64 dtypes with 'm' and 'h' resolutions " + r"are deprecated.Supported resolutions are 's', 'ms','us', and 'ns'. " + r"In future releases resolutions will be cast to the closest " r"supported unit.", FutureWarning, stacklevel=find_stack_level(), diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index 37c3f1abd2cff..8fc37364277bb 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -36,10 +36,10 @@ def test_dt64_array(): 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." + r"datetime64 and timedelta64 dtypes with 'm' and 'h' resolutions " + r"are deprecated.Supported resolutions are 's', 'ms','us', and 'ns'. " + r"In future releases resolutions will be cast to the closest " + r"supported unit." ) with tm.assert_produces_warning(FutureWarning, match=re.escape(msg)): pd.array([], dtype=dtype_var) From 2553d323fe23212c05e1063c726c7765c25a14b8 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Fri, 30 Jun 2023 14:49:26 -0400 Subject: [PATCH 4/8] Updating warning message based on reviewer recommendations. --- pandas/core/construction.py | 4 ++-- pandas/tests/arrays/test_array.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 0e84d26d7090c..daa08a7e2294c 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -383,8 +383,8 @@ def array( elif lib.is_np_dtype(dtype, "mM"): warnings.warn( - r"datetime64 and timedelta64 dtypes with 'm' and 'h' resolutions " - r"are deprecated.Supported resolutions are 's', 'ms','us', and 'ns'. " + r"Deprecating unsupported datetime64 and timedelta64 dtype resolutions. " + r"Supported resolutions are 's', 'ms','us', and 'ns'. " r"In future releases resolutions will be cast to the closest " r"supported unit.", FutureWarning, diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index 8fc37364277bb..72bb1baed40cd 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -36,8 +36,8 @@ def test_dt64_array(): for unit in dtype_unit_lst: dtype_var = np.dtype(unit) msg = ( - r"datetime64 and timedelta64 dtypes with 'm' and 'h' resolutions " - r"are deprecated.Supported resolutions are 's', 'ms','us', and 'ns'. " + r"Deprecating unsupported datetime64 and timedelta64 dtype resolutions. " + r"Supported resolutions are 's', 'ms','us', and 'ns'. " r"In future releases resolutions will be cast to the closest " r"supported unit." ) From 27dc4e139dc6e11c7b7a99b0ba137ddfec818721 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Sat, 8 Jul 2023 14:29:51 -0400 Subject: [PATCH 5/8] Updating implementation based on reviewer recommendations. --- pandas/core/construction.py | 8 ++++---- pandas/tests/arrays/test_array.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index daa08a7e2294c..c42842f09784a 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -383,10 +383,10 @@ def array( elif lib.is_np_dtype(dtype, "mM"): warnings.warn( - r"Deprecating unsupported datetime64 and timedelta64 dtype resolutions. " - r"Supported resolutions are 's', 'ms','us', and 'ns'. " - r"In future releases resolutions will be cast to the closest " - r"supported unit.", + 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(), ) diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index 72bb1baed40cd..639181b785b66 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -36,10 +36,10 @@ def test_dt64_array(): for unit in dtype_unit_lst: dtype_var = np.dtype(unit) msg = ( - r"Deprecating unsupported datetime64 and timedelta64 dtype resolutions. " - r"Supported resolutions are 's', 'ms','us', and 'ns'. " - r"In future releases resolutions will be cast to the closest " - r"supported unit." + 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. " ) with tm.assert_produces_warning(FutureWarning, match=re.escape(msg)): pd.array([], dtype=dtype_var) From c0ca5eb2da86e27d793505e04160bc885b0cc59f Mon Sep 17 00:00:00 2001 From: rmhowe425 <45905457+rmhowe425@users.noreply.github.com> Date: Sun, 9 Jul 2023 16:17:06 -0400 Subject: [PATCH 6/8] Updating implementation based on reviewer recommendations --- pandas/core/construction.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index c42842f09784a..aca4dab788e53 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -384,9 +384,9 @@ def array( elif lib.is_np_dtype(dtype, "mM"): warnings.warn( r"datetime64 and timedelta64 dtype resolutions other than " - r"'s', 'ms','us', and 'ns' are deprecated. " + r"'s', 'ms', 'us', and 'ns' are deprecated. " r"In future releases passing unsupported resolutions will " - r"raise an exception. ", + r"raise an exception.", FutureWarning, stacklevel=find_stack_level(), ) From ea4c7ec8d6981d8ac519018eba6b7bbd6c8bdbb2 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Sun, 9 Jul 2023 16:47:30 -0400 Subject: [PATCH 7/8] Updating implementation based on reviewer recommendations. --- pandas/tests/arrays/test_array.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index 639181b785b66..ed92f54cf5c94 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -37,9 +37,9 @@ def test_dt64_array(): dtype_var = np.dtype(unit) msg = ( r"datetime64 and timedelta64 dtype resolutions other than " - r"'s', 'ms','us', and 'ns' are deprecated. " + r"'s', 'ms', 'us', and 'ns' are deprecated. " r"In future releases passing unsupported resolutions will " - r"raise an exception. " + r"raise an exception." ) with tm.assert_produces_warning(FutureWarning, match=re.escape(msg)): pd.array([], dtype=dtype_var) From 899f2fbdb67908b58022ea3b3f109ad94b8df6ee Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Mon, 10 Jul 2023 18:30:20 -0400 Subject: [PATCH 8/8] Updating unit tests based on reviewer recommendations. --- pandas/tests/arrays/test_array.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index ed92f54cf5c94..d5c1d5bbd03b0 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -29,20 +29,18 @@ ) -def test_dt64_array(): +@pytest.mark.parametrize("dtype_unit", ["M8[h]", "M8[m]", "m8[h]", "M8[m]"]) +def test_dt64_array(dtype_unit): # PR 53817 - dtype_unit_lst = ["M8[h]", "M8[m]", "m8[h]", "M8[m]"] - - for unit in dtype_unit_lst: - dtype_var = np.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." - ) - with tm.assert_produces_warning(FutureWarning, match=re.escape(msg)): - pd.array([], dtype=dtype_var) + 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." + ) + with tm.assert_produces_warning(FutureWarning, match=re.escape(msg)): + pd.array([], dtype=dtype_var) @pytest.mark.parametrize(