Skip to content

Commit b4929f8

Browse files
authored
DEPR: Deprecate use of un-supported numpy dt64/td64 dtype for pandas.array (#53817)
* Adding deprecation logic, unit test, and documentation * Updated documentation in unit test. * Updating warning message based on reviewer recommendations. * Updating warning message based on reviewer recommendations. * Updating implementation based on reviewer recommendations. * Updating implementation based on reviewer recommendations * Updating implementation based on reviewer recommendations. * Updating unit tests based on reviewer recommendations.
1 parent 997699e commit b4929f8

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Diff for: doc/source/whatsnew/v2.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,9 @@ Deprecations
320320
- Deprecated strings ``T``, ``t``, ``L`` and ``l`` denoting units in :func:`to_timedelta` (:issue:`52536`)
321321
- 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`)
322322
- Deprecated the ``method`` and ``limit`` keywords in :meth:`DataFrame.replace` and :meth:`Series.replace` (:issue:`33302`)
323+
- Deprecated the use of non-supported datetime64 and timedelta64 resolutions with :func:`pandas.array`. Supported resolutions are: "s", "ms", "us", "ns" resolutions (:issue:`53058`)
323324
- Deprecated values "pad", "ffill", "bfill", "backfill" for :meth:`Series.interpolate` and :meth:`DataFrame.interpolate`, use ``obj.ffill()`` or ``obj.bfill()`` instead (:issue:`53581`)
325+
-
324326

325327
.. ---------------------------------------------------------------------------
326328
.. _whatsnew_210.performance:

Diff for: pandas/core/construction.py

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
cast,
1515
overload,
1616
)
17+
import warnings
1718

1819
import numpy as np
1920
from numpy import ma
@@ -31,6 +32,7 @@
3132
DtypeObj,
3233
T,
3334
)
35+
from pandas.util._exceptions import find_stack_level
3436

3537
from pandas.core.dtypes.base import ExtensionDtype
3638
from pandas.core.dtypes.cast import (
@@ -369,6 +371,16 @@ def array(
369371
if lib.is_np_dtype(dtype, "m") and is_supported_unit(get_unit_from_dtype(dtype)):
370372
return TimedeltaArray._from_sequence(data, dtype=dtype, copy=copy)
371373

374+
elif lib.is_np_dtype(dtype, "mM"):
375+
warnings.warn(
376+
r"datetime64 and timedelta64 dtype resolutions other than "
377+
r"'s', 'ms', 'us', and 'ns' are deprecated. "
378+
r"In future releases passing unsupported resolutions will "
379+
r"raise an exception.",
380+
FutureWarning,
381+
stacklevel=find_stack_level(),
382+
)
383+
372384
return PandasArray._from_sequence(data, dtype=dtype, copy=copy)
373385

374386

Diff for: pandas/tests/arrays/test_array.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import decimal
3+
import re
34

45
import numpy as np
56
import pytest
@@ -28,6 +29,20 @@
2829
)
2930

3031

32+
@pytest.mark.parametrize("dtype_unit", ["M8[h]", "M8[m]", "m8[h]", "M8[m]"])
33+
def test_dt64_array(dtype_unit):
34+
# PR 53817
35+
dtype_var = np.dtype(dtype_unit)
36+
msg = (
37+
r"datetime64 and timedelta64 dtype resolutions other than "
38+
r"'s', 'ms', 'us', and 'ns' are deprecated. "
39+
r"In future releases passing unsupported resolutions will "
40+
r"raise an exception."
41+
)
42+
with tm.assert_produces_warning(FutureWarning, match=re.escape(msg)):
43+
pd.array([], dtype=dtype_var)
44+
45+
3146
@pytest.mark.parametrize(
3247
"data, dtype, expected",
3348
[

0 commit comments

Comments
 (0)