From f338a519f9e3118a8a03b037ce7dd188ec83807f Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Sun, 9 Apr 2023 17:24:27 -0400 Subject: [PATCH 1/3] BUG: DatetimeArray.unit when constructed from a non-nano ndarray --- pandas/core/arrays/datetimelike.py | 5 ++++- pandas/tests/arrays/datetimes/test_constructors.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index b01b8e91a2cc7..e2ea75d654b27 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1885,7 +1885,10 @@ def __init__( values = values._ndarray elif dtype is None: - dtype = self._default_dtype + if isinstance(values, np.ndarray) and values.dtype.kind in "Mm": + dtype = values.dtype + else: + dtype = self._default_dtype if not isinstance(values, np.ndarray): raise ValueError( diff --git a/pandas/tests/arrays/datetimes/test_constructors.py b/pandas/tests/arrays/datetimes/test_constructors.py index 6670d07a4c075..bbc66dcd328c3 100644 --- a/pandas/tests/arrays/datetimes/test_constructors.py +++ b/pandas/tests/arrays/datetimes/test_constructors.py @@ -127,6 +127,13 @@ def test_copy(self): arr = DatetimeArray(data, copy=True) assert arr._ndarray is not data + @pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"]) + def test_numpy_datetime_unit(self, unit): + data = np.array([1, 2, 3], dtype=f"M8[{unit}]") + arr = DatetimeArray(data) + assert arr.unit == unit + assert arr[0].unit == unit + class TestSequenceToDT64NS: def test_tz_dtype_mismatch_raises(self): From b812f37bd7c852eb71090096e0dea3aa933b1571 Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Sun, 9 Apr 2023 17:28:35 -0400 Subject: [PATCH 2/3] whatsnew --- doc/source/whatsnew/v2.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 9f5d6011a7780..07a6fcac5bdfc 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -219,6 +219,7 @@ Datetimelike - Bug in :meth:`Timestamp.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsDatetime`` (:issue:`51494`) - :meth:`DatetimeIndex.map` with ``na_action="ignore"`` now works as expected. (:issue:`51644`) - Bug in :meth:`arrays.DatetimeArray.map` and :meth:`DatetimeIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`) +- Bug in :class:`arrays.DatetimeArray` constructor returning an incorrect unit when passed a non-nanosecond numpy datetime array (:issue:`52555`) - Timedelta From e5db40f7d1035d9b63992adcd93e0862f92cc6fe Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Mon, 10 Apr 2023 20:27:59 -0400 Subject: [PATCH 3/3] move whatsnew to 2.0.1 --- doc/source/whatsnew/v2.0.1.rst | 1 + doc/source/whatsnew/v2.1.0.rst | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index a4a58811e382f..841d6147a43c8 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -28,6 +28,7 @@ Bug fixes - Fixed segfault in :meth:`Series.to_numpy` with ``null[pyarrow]`` dtype (:issue:`52443`) - Bug in :func:`pandas.testing.assert_series_equal` where ``check_dtype=False`` would still raise for datetime or timedelta types with different resolutions (:issue:`52449`) - Bug in :meth:`ArrowDtype.__from_arrow__` not respecting if dtype is explicitly given (:issue:`52533`) +- Bug in :class:`arrays.DatetimeArray` constructor returning an incorrect unit when passed a non-nanosecond numpy datetime array (:issue:`52555`) .. --------------------------------------------------------------------------- .. _whatsnew_201.other: diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 462dea81007cd..afe361da1114d 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -272,7 +272,6 @@ Datetimelike - Bug in :meth:`Timestamp.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsDatetime`` (:issue:`51494`) - :meth:`DatetimeIndex.map` with ``na_action="ignore"`` now works as expected. (:issue:`51644`) - Bug in :meth:`arrays.DatetimeArray.map` and :meth:`DatetimeIndex.map`, where the supplied callable operated array-wise instead of element-wise (:issue:`51977`) -- Bug in :class:`arrays.DatetimeArray` constructor returning an incorrect unit when passed a non-nanosecond numpy datetime array (:issue:`52555`) - Timedelta