From 158ebd3ec2d9fcaeb03e4cb5a7a5f570012de6d7 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Sun, 28 May 2023 19:47:14 -0400 Subject: [PATCH 01/16] Adding logic to deprecate the use of dt64/td64 dtypes for pandas.array --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/core/construction.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 2c5263f447951..6e545d6f21828 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -269,6 +269,7 @@ Deprecations - Deprecated behavior of :func:`assert_series_equal` and :func:`assert_frame_equal` considering NA-like values (e.g. ``NaN`` vs ``None`` as equivalent) (:issue:`52081`) - Deprecated constructing :class:`SparseArray` from scalar data, pass a sequence instead (:issue:`53039`) - Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`) +- Deprecated the use of dt64/td64 dtypes with :func:`pandas.array` (:issue:`53058`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 9b4d67a20a7cd..97d5d2c1a82ad 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 ( @@ -364,6 +366,14 @@ def array( # 1. datetime64[ns,us,ms,s] # 2. timedelta64[ns,us,ms,s] # so that a DatetimeArray is returned. + if "datetime64" in dtype.name or "timedelta64" in dtype.name: + warnings.warn( + "Using the dt64/td64 dtype is deprecated and " + "will be removed in a future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) + if ( lib.is_np_dtype(dtype, "M") # error: Argument 1 to "py_get_unit_from_dtype" has incompatible type From 4ff6dc5954ff9a8b5b49331cadf0064a99644b70 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Mon, 29 May 2023 12:29:03 -0400 Subject: [PATCH 02/16] Updating unit tests --- pandas/core/construction.py | 13 +++---------- pandas/tests/arrays/test_array.py | 26 ++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 97d5d2c1a82ad..b024cf54ecea3 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -14,7 +14,6 @@ cast, overload, ) -import warnings import numpy as np from numpy import ma @@ -32,7 +31,6 @@ DtypeObj, T, ) -from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.base import ExtensionDtype from pandas.core.dtypes.cast import ( @@ -297,6 +295,9 @@ def array( ) from pandas.core.arrays.string_ import StringDtype + if isinstance(dtype, np.dtype) and dtype.kind in "mM": + raise TypeError(f"Data type {dtype} is deprecated.") + if lib.is_scalar(data): msg = f"Cannot pass scalar '{data}' to 'pandas.array'." raise ValueError(msg) @@ -366,14 +367,6 @@ def array( # 1. datetime64[ns,us,ms,s] # 2. timedelta64[ns,us,ms,s] # so that a DatetimeArray is returned. - if "datetime64" in dtype.name or "timedelta64" in dtype.name: - warnings.warn( - "Using the dt64/td64 dtype is deprecated and " - "will be removed in a future version.", - FutureWarning, - stacklevel=find_stack_level(), - ) - if ( lib.is_np_dtype(dtype, "M") # error: Argument 1 to "py_get_unit_from_dtype" has incompatible type diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index 337cdaa26a3d4..a43ecb3d3e7b3 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 @@ -27,6 +28,22 @@ to_decimal, ) +dt_dep_msg = ( + "Using the dt64/td64 dtype is deprecated and " + "will be removed in a future version." +) + + +def test_dt64_array(): + dtype_dt64 = np.dtype("M8[h]") + dtype_td64 = np.dtype("m8[h]") + + with tm.assert_produces_warning(FutureWarning, match=dt_dep_msg): + pd.array([], dtype=dtype_dt64) + + with tm.assert_produces_warning(FutureWarning, match=dt_dep_msg): + pd.array([], dtype=dtype_td64) + @pytest.mark.parametrize( "data, dtype, expected", @@ -203,8 +220,13 @@ ], ) def test_array(data, dtype, expected): - result = pd.array(data, dtype=dtype) - tm.assert_equal(result, expected) + msg = f"Data type {dtype} is deprecated." + if isinstance(dtype, np.dtype) and dtype.kind in "mM": + with pytest.raises(TypeError, match=re.escape(msg)): + result = pd.array(data, dtype=dtype) + else: + result = pd.array(data, dtype=dtype) + tm.assert_equal(result, expected) def test_array_copy(): From c15123d96e6d637f918e54ec4e287c7d0e1e9a83 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Mon, 29 May 2023 13:24:03 -0400 Subject: [PATCH 03/16] Fixing unit tests --- pandas/tests/arrays/test_array.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index a43ecb3d3e7b3..206d48db7ace0 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -28,20 +28,17 @@ to_decimal, ) -dt_dep_msg = ( - "Using the dt64/td64 dtype is deprecated and " - "will be removed in a future version." -) - def test_dt64_array(): dtype_dt64 = np.dtype("M8[h]") dtype_td64 = np.dtype("m8[h]") - with tm.assert_produces_warning(FutureWarning, match=dt_dep_msg): + msg = f"Data type {dtype_dt64} is deprecated." + with pytest.raises(TypeError, match=re.escape(msg)): pd.array([], dtype=dtype_dt64) - with tm.assert_produces_warning(FutureWarning, match=dt_dep_msg): + msg = f"Data type {dtype_td64} is deprecated." + with pytest.raises(TypeError, match=re.escape(msg)): pd.array([], dtype=dtype_td64) From 43602543cf1efde14c3d85cba2abe8bf40e88b40 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Mon, 29 May 2023 15:35:28 -0400 Subject: [PATCH 04/16] Updating documentation to reflect the deprecation of dt64/d64 dtype. --- doc/source/reference/arrays.rst | 2 -- pandas/core/construction.py | 22 ---------------------- pandas/tests/arrays/test_array.py | 1 + 3 files changed, 1 insertion(+), 24 deletions(-) diff --git a/doc/source/reference/arrays.rst b/doc/source/reference/arrays.rst index 54e49448daca8..849815956c38d 100644 --- a/doc/source/reference/arrays.rst +++ b/doc/source/reference/arrays.rst @@ -22,8 +22,6 @@ can be found at :ref:`basics.dtypes`. =================== ========================== ============================= ============================= Kind of Data pandas Data Type Scalar Array =================== ========================== ============================= ============================= -TZ-aware datetime :class:`DatetimeTZDtype` :class:`Timestamp` :ref:`api.arrays.datetime` -Timedeltas (none) :class:`Timedelta` :ref:`api.arrays.timedelta` Period (time spans) :class:`PeriodDtype` :class:`Period` :ref:`api.arrays.period` Intervals :class:`IntervalDtype` :class:`Interval` :ref:`api.arrays.interval` Nullable Integer :class:`Int64Dtype`, ... (none) :ref:`api.arrays.integer_na` diff --git a/pandas/core/construction.py b/pandas/core/construction.py index b024cf54ecea3..035e213dda9cd 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -109,8 +109,6 @@ def array( ============================== ======================================= :class:`pandas.Interval` :class:`pandas.arrays.IntervalArray` :class:`pandas.Period` :class:`pandas.arrays.PeriodArray` - :class:`datetime.datetime` :class:`pandas.arrays.DatetimeArray` - :class:`datetime.timedelta` :class:`pandas.arrays.TimedeltaArray` :class:`int` :class:`pandas.arrays.IntegerArray` :class:`float` :class:`pandas.arrays.FloatingArray` :class:`str` :class:`pandas.arrays.StringArray` or @@ -184,26 +182,6 @@ def array( ['a', 'b'] Length: 2, dtype: str32 - Finally, Pandas has arrays that mostly overlap with NumPy - - * :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`` - rather than a ``PandasArray``. This is for symmetry with the case of - timezone-aware data, which NumPy does not natively support. - - >>> pd.array(['2015', '2016'], dtype='datetime64[ns]') - - ['2015-01-01 00:00:00', '2016-01-01 00:00:00'] - Length: 2, dtype: datetime64[ns] - - >>> pd.array(["1H", "2H"], dtype='timedelta64[ns]') - - ['0 days 01:00:00', '0 days 02:00:00'] - Length: 2, dtype: timedelta64[ns] - Examples -------- If a dtype is not specified, pandas will infer the best dtype from the values. diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index 206d48db7ace0..3790520135255 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -30,6 +30,7 @@ def test_dt64_array(): + # PR 53439 dtype_dt64 = np.dtype("M8[h]") dtype_td64 = np.dtype("m8[h]") From 48a59abeff9d80e06f6ee60e423c2262e62b2cc0 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Mon, 29 May 2023 15:44:31 -0400 Subject: [PATCH 05/16] Updating whatsnew/v2.1.0.rst --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 6e545d6f21828..35db4064426ee 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -269,7 +269,7 @@ Deprecations - Deprecated behavior of :func:`assert_series_equal` and :func:`assert_frame_equal` considering NA-like values (e.g. ``NaN`` vs ``None`` as equivalent) (:issue:`52081`) - Deprecated constructing :class:`SparseArray` from scalar data, pass a sequence instead (:issue:`53039`) - Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`) -- Deprecated the use of dt64/td64 dtypes with :func:`pandas.array` (:issue:`53058`) +- Deprecated the use of dt64/td64 dtypes with :func:`pandas.array` (:issue:`53439`) - .. --------------------------------------------------------------------------- From 5efe978436229e7ce12f8f8ff314e67b7d1f9d1e Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Fri, 2 Jun 2023 22:48:23 -0400 Subject: [PATCH 06/16] Updating unit tests based on reviewer recommendations. --- pandas/core/construction.py | 3 ++- pandas/tests/arrays/test_array.py | 26 ++++++++++---------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 035e213dda9cd..ed32e7281db33 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -41,6 +41,7 @@ maybe_convert_platform, maybe_infer_to_datetimelike, maybe_promote, + _ensure_nanosecond_dtype, ) from pandas.core.dtypes.common import ( is_list_like, @@ -274,7 +275,7 @@ def array( from pandas.core.arrays.string_ import StringDtype if isinstance(dtype, np.dtype) and dtype.kind in "mM": - raise TypeError(f"Data type {dtype} is deprecated.") + _ensure_nanosecond_dtype(dtype) if lib.is_scalar(data): msg = f"Cannot pass scalar '{data}' to 'pandas.array'." diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index 3790520135255..fcaaff8eeb119 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -31,16 +31,13 @@ def test_dt64_array(): # PR 53439 - dtype_dt64 = np.dtype("M8[h]") - dtype_td64 = np.dtype("m8[h]") + dtype_unit_lst = ["M8[h]", "M8[m]", "m8[h]", "M8[m]"] - msg = f"Data type {dtype_dt64} is deprecated." - with pytest.raises(TypeError, match=re.escape(msg)): - pd.array([], dtype=dtype_dt64) - - msg = f"Data type {dtype_td64} is deprecated." - with pytest.raises(TypeError, match=re.escape(msg)): - pd.array([], dtype=dtype_td64) + for unit in dtype_unit_lst: + dtype_var = np.dtype(unit) + msg = "dtype={} is not supported. Supported resolutions are 's', 'ms', 'us', and 'ns'".format(dtype_var) + with pytest.raises(TypeError, match=re.escape(msg)): + pd.array([], dtype=dtype_var) @pytest.mark.parametrize( @@ -218,13 +215,10 @@ def test_dt64_array(): ], ) def test_array(data, dtype, expected): - msg = f"Data type {dtype} is deprecated." - if isinstance(dtype, np.dtype) and dtype.kind in "mM": - with pytest.raises(TypeError, match=re.escape(msg)): - result = pd.array(data, dtype=dtype) - else: - result = pd.array(data, dtype=dtype) - tm.assert_equal(result, expected) + with open("/home/richard/Desktop/file.txt", 'a+') as fil: + fil.write(f'{dtype}\n') + result = pd.array(data, dtype=dtype) + tm.assert_equal(result, expected) def test_array_copy(): From e5a085d484de2308becaf00114235f300b4045e7 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Fri, 2 Jun 2023 22:52:11 -0400 Subject: [PATCH 07/16] Updating unit tests based on reviewer recommendations. --- pandas/tests/arrays/test_array.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index fcaaff8eeb119..c849140d6222c 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -35,7 +35,10 @@ def test_dt64_array(): for unit in dtype_unit_lst: dtype_var = np.dtype(unit) - msg = "dtype={} is not supported. Supported resolutions are 's', 'ms', 'us', and 'ns'".format(dtype_var) + msg = ( + "dtype={} is not supported. Supported resolutions are 's', 'ms', " + "'us', and 'ns'".format(dtype_var) + ) with pytest.raises(TypeError, match=re.escape(msg)): pd.array([], dtype=dtype_var) @@ -215,8 +218,8 @@ def test_dt64_array(): ], ) def test_array(data, dtype, expected): - with open("/home/richard/Desktop/file.txt", 'a+') as fil: - fil.write(f'{dtype}\n') + with open("/home/richard/Desktop/file.txt", "a+") as fil: + fil.write(f"{dtype}\n") result = pd.array(data, dtype=dtype) tm.assert_equal(result, expected) From 7addd43d4f52ef9bed09f96aa1e198f01d0324fe Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Fri, 2 Jun 2023 22:55:59 -0400 Subject: [PATCH 08/16] Adding back documentation that should'nt have been removed --- doc/source/reference/arrays.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/reference/arrays.rst b/doc/source/reference/arrays.rst index 849815956c38d..54e49448daca8 100644 --- a/doc/source/reference/arrays.rst +++ b/doc/source/reference/arrays.rst @@ -22,6 +22,8 @@ can be found at :ref:`basics.dtypes`. =================== ========================== ============================= ============================= Kind of Data pandas Data Type Scalar Array =================== ========================== ============================= ============================= +TZ-aware datetime :class:`DatetimeTZDtype` :class:`Timestamp` :ref:`api.arrays.datetime` +Timedeltas (none) :class:`Timedelta` :ref:`api.arrays.timedelta` Period (time spans) :class:`PeriodDtype` :class:`Period` :ref:`api.arrays.period` Intervals :class:`IntervalDtype` :class:`Interval` :ref:`api.arrays.interval` Nullable Integer :class:`Int64Dtype`, ... (none) :ref:`api.arrays.integer_na` From d3ba003842ba86c9d50931ab081e979580ededa4 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Fri, 2 Jun 2023 23:29:19 -0400 Subject: [PATCH 09/16] Fixing formatting error, adding documentation back that shouldn't have been removed. --- pandas/core/construction.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index ed32e7281db33..a21c6ce1ab2e7 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -41,7 +41,6 @@ maybe_convert_platform, maybe_infer_to_datetimelike, maybe_promote, - _ensure_nanosecond_dtype, ) from pandas.core.dtypes.common import ( is_list_like, @@ -110,6 +109,8 @@ def array( ============================== ======================================= :class:`pandas.Interval` :class:`pandas.arrays.IntervalArray` :class:`pandas.Period` :class:`pandas.arrays.PeriodArray` + :class:`datetime.datetime` :class:`pandas.arrays.DatetimeArray` + :class:`datetime.timedelta` :class:`pandas.arrays.TimedeltaArray` :class:`int` :class:`pandas.arrays.IntegerArray` :class:`float` :class:`pandas.arrays.FloatingArray` :class:`str` :class:`pandas.arrays.StringArray` or @@ -183,6 +184,25 @@ def array( ['a', 'b'] Length: 2, dtype: str32 + Finally, Pandas has arrays that mostly overlap with NumPy + * :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`` + rather than a ``PandasArray``. This is for symmetry with the case of + timezone-aware data, which NumPy does not natively support. + + >>> pd.array(['2015', '2016'], dtype='datetime64[ns]') + + ['2015-01-01 00:00:00', '2016-01-01 00:00:00'] + Length: 2, dtype: datetime64[ns] + + >>> pd.array(["1H", "2H"], dtype='timedelta64[ns]') + + ['0 days 01:00:00', '0 days 02:00:00'] + Length: 2, dtype: timedelta64[ns] + Examples -------- If a dtype is not specified, pandas will infer the best dtype from the values. @@ -275,7 +295,7 @@ def array( from pandas.core.arrays.string_ import StringDtype if isinstance(dtype, np.dtype) and dtype.kind in "mM": - _ensure_nanosecond_dtype(dtype) + maybe_cast_to_datetime(data, dtype) if lib.is_scalar(data): msg = f"Cannot pass scalar '{data}' to 'pandas.array'." From 73de691efb4accfee8d5a8b171e61d8f7a1c6d82 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Fri, 2 Jun 2023 23:42:41 -0400 Subject: [PATCH 10/16] Updating unit tests per reviewer recommendation. --- pandas/tests/arrays/test_array.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tests/arrays/test_array.py b/pandas/tests/arrays/test_array.py index c849140d6222c..b6e53ee3a2497 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -218,8 +218,6 @@ def test_dt64_array(): ], ) def test_array(data, dtype, expected): - with open("/home/richard/Desktop/file.txt", "a+") as fil: - fil.write(f"{dtype}\n") result = pd.array(data, dtype=dtype) tm.assert_equal(result, expected) From 19835a4da4e0a6a9b028f43a5b979f5491802547 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Sat, 3 Jun 2023 10:29:07 -0400 Subject: [PATCH 11/16] Fixing reviewer recomendations. --- pandas/core/construction.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index a21c6ce1ab2e7..064338c296fef 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -294,9 +294,6 @@ def array( ) from pandas.core.arrays.string_ import StringDtype - if isinstance(dtype, np.dtype) and dtype.kind in "mM": - maybe_cast_to_datetime(data, dtype) - if lib.is_scalar(data): msg = f"Cannot pass scalar '{data}' to 'pandas.array'." raise ValueError(msg) @@ -381,6 +378,9 @@ def array( ): return TimedeltaArray._from_sequence(data, dtype=dtype, copy=copy) + elif isinstance(dtype, np.dtype) and dtype.kind in "mM": + sanitize_array(data, None, dtype, copy) + return PandasArray._from_sequence(data, dtype=dtype, copy=copy) From 2a98a5770207ff210799259691de66bfcb003161 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Sat, 3 Jun 2023 11:15:12 -0400 Subject: [PATCH 12/16] Fixing pylint error --- 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 b6e53ee3a2497..b9f4979b464de 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 = ( - "dtype={} is not supported. Supported resolutions are 's', 'ms', " - "'us', and 'ns'".format(dtype_var) + f"dtype={dtype_var} is not supported. Supported resolutions are 's', 'ms', " + "'us', and 'ns'" ) with pytest.raises(TypeError, match=re.escape(msg)): pd.array([], dtype=dtype_var) From 427e6d61e2d6ec79fe4bbeacaca2cf4b2e3483df Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Sun, 11 Jun 2023 15:46:52 -0400 Subject: [PATCH 13/16] Fixing error in whatsnew documentation --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 1a858d6aa0fcb..90bb6694b6060 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -283,8 +283,8 @@ Deprecations - Deprecated constructing :class:`SparseArray` from scalar data, pass a sequence instead (:issue:`53039`) - Deprecated option "mode.use_inf_as_na", convert inf entries to ``NaN`` before instead (:issue:`51684`) - Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`) -- Deprecated the use of dt64/td64 dtypes with :func:`pandas.array` (:issue:`53439`) - 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 use of dt64/td64 dtypes with :func:`pandas.array` (:issue:`53439`) - .. --------------------------------------------------------------------------- From 33c7f72cc7ebf74ecbc1a5b62bb6f88c25cd18ef Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Wed, 14 Jun 2023 21:18:34 -0400 Subject: [PATCH 14/16] Fixing documentation error after fixing merge conflict --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 641e25f1c2567..934693dc9c19e 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -290,8 +290,8 @@ Deprecations - Deprecated option "mode.use_inf_as_na", convert inf entries to ``NaN`` before instead (:issue:`51684`) - Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`) - 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 use of dt64/td64 dtypes with :func:`pandas.array` (:issue:`53439`) - Deprecated the ``method`` and ``limit`` keywords in :meth:`DataFrame.replace` and :meth:`Series.replace` (:issue:`33302`) +- Deprecated the use of dt64/td64 dtypes with :func:`pandas.array` (: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`) - From 5ca499f7bdab9cc27fec1669f76aee6ae7623082 Mon Sep 17 00:00:00 2001 From: Richard Howe Date: Thu, 15 Jun 2023 20:55:54 -0400 Subject: [PATCH 15/16] Updating PR based on reviewer recommendations --- doc/source/whatsnew/v2.1.0.rst | 2 +- pandas/core/construction.py | 13 +++++++++++-- pandas/tests/arrays/test_array.py | 8 +++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 934693dc9c19e..9d666dd32ff56 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -291,7 +291,7 @@ Deprecations - Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`) - 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 dt64/td64 dtypes with :func:`pandas.array` (:issue:`53439`) +- Deprecated the use of hour and minute dt64/td64 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 064338c296fef..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 ( @@ -378,8 +380,15 @@ def array( ): return TimedeltaArray._from_sequence(data, dtype=dtype, copy=copy) - elif isinstance(dtype, np.dtype) and dtype.kind in "mM": - sanitize_array(data, None, dtype, 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 b9f4979b464de..06f8e24b18825 100644 --- a/pandas/tests/arrays/test_array.py +++ b/pandas/tests/arrays/test_array.py @@ -36,10 +36,12 @@ def test_dt64_array(): for unit in dtype_unit_lst: dtype_var = np.dtype(unit) msg = ( - f"dtype={dtype_var} is not supported. Supported resolutions are 's', 'ms', " - "'us', and 'ns'" + 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 pytest.raises(TypeError, match=re.escape(msg)): + with tm.assert_produces_warning(FutureWarning, match=re.escape(msg)): pd.array([], dtype=dtype_var) From c3bdbf8610af26b162f83a9e758da3ef72125f1e Mon Sep 17 00:00:00 2001 From: rmhowe425 <45905457+rmhowe425@users.noreply.github.com> Date: Sat, 17 Jun 2023 14:33:29 -0400 Subject: [PATCH 16/16] Updated PR based on reviewer recommendations --- doc/source/whatsnew/v2.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 9d666dd32ff56..7c96a5865967f 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -291,7 +291,7 @@ Deprecations - Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`) - 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 hour and minute dt64/td64 resolutions with :func:`pandas.array`. Supported resolutions are: "s", "ms", "us", "ns" resolutions (:issue:`53439`) +- 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`) -