Skip to content

DEPR: Deprecate use of un-supported numpy dt64/td64 dtype for pandas.array #53817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 11, 2023
Merged
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
cast,
overload,
)
import warnings

import numpy as np
from numpy import ma
Expand All @@ -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 (
Expand Down Expand Up @@ -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"Deprecating unsupported datetime64 and timedelta64 dtype resolutions. "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the first sentence here is awkward. Maybe "datetime64 and timedelta64 dtypes with resolutions other than ... are deprecated."

If they pass an unsupported dtype, we should raise, not silently cast

r"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(),
)

return PandasArray._from_sequence(data, dtype=dtype, copy=copy)


Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/arrays/test_array.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import decimal
import re

import numpy as np
import pytest
Expand Down Expand Up @@ -28,6 +29,22 @@
)


def test_dt64_array():
# PR 53817
dtype_unit_lst = ["M8[h]", "M8[m]", "m8[h]", "M8[m]"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could you use @pytest.mark.parameterize here?


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."
)
with tm.assert_produces_warning(FutureWarning, match=re.escape(msg)):
pd.array([], dtype=dtype_var)


@pytest.mark.parametrize(
"data, dtype, expected",
[
Expand Down