Skip to content

Commit

Permalink
Backport PR #52555 on branch 2.0.x (BUG: DatetimeArray.unit when cons…
Browse files Browse the repository at this point in the history
…tructed from a non-nano ndarray) (#52608)

Backport PR #52555: BUG: DatetimeArray.unit when constructed from a non-nano ndarray

Co-authored-by: Luke Manley <lukemanley@gmail.com>
  • Loading branch information
meeseeksmachine and lukemanley committed Apr 11, 2023
1 parent 99bcfe3 commit 7e6261a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Bug fixes
- 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 :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`)
- Bug in :class:`arrays.DatetimeArray` constructor returning an incorrect unit when passed a non-nanosecond numpy datetime array (:issue:`52555`)

.. ---------------------------------------------------------------------------
.. _whatsnew_201.other:
Expand Down
5 changes: 4 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,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(
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/arrays/datetimes/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 7e6261a

Please sign in to comment.