Skip to content

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

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 5 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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:
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 @@ -1878,7 +1878,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