Skip to content

Commit 7c55534

Browse files
authored
BUG: DatetimeArray.unit when constructed from a non-nano ndarray (#52555)
* BUG: DatetimeArray.unit when constructed from a non-nano ndarray * whatsnew * move whatsnew to 2.0.1
1 parent 656e4fd commit 7c55534

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v2.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Bug fixes
3030
- 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`)
3131
- Bug in :meth:`ArrowDtype.__from_arrow__` not respecting if dtype is explicitly given (:issue:`52533`)
3232
- Bug in :func:`read_csv` casting PyArrow datetimes to NumPy when ``dtype_backend="pyarrow"`` and ``parse_dates`` is set causing a performance bottleneck in the process (:issue:`52546`)
33+
- Bug in :class:`arrays.DatetimeArray` constructor returning an incorrect unit when passed a non-nanosecond numpy datetime array (:issue:`52555`)
3334

3435
.. ---------------------------------------------------------------------------
3536
.. _whatsnew_201.other:

pandas/core/arrays/datetimelike.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,10 @@ def __init__(
18781878
values = values._ndarray
18791879

18801880
elif dtype is None:
1881-
dtype = self._default_dtype
1881+
if isinstance(values, np.ndarray) and values.dtype.kind in "Mm":
1882+
dtype = values.dtype
1883+
else:
1884+
dtype = self._default_dtype
18821885

18831886
if not isinstance(values, np.ndarray):
18841887
raise ValueError(

pandas/tests/arrays/datetimes/test_constructors.py

+7
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ def test_copy(self):
127127
arr = DatetimeArray(data, copy=True)
128128
assert arr._ndarray is not data
129129

130+
@pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"])
131+
def test_numpy_datetime_unit(self, unit):
132+
data = np.array([1, 2, 3], dtype=f"M8[{unit}]")
133+
arr = DatetimeArray(data)
134+
assert arr.unit == unit
135+
assert arr[0].unit == unit
136+
130137

131138
class TestSequenceToDT64NS:
132139
def test_tz_dtype_mismatch_raises(self):

0 commit comments

Comments
 (0)