Skip to content

Commit a8bbe75

Browse files
BUG: fix construction from read-only non-ns datetime64 numpy array (#34844)
1 parent 5d69786 commit a8bbe75

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+3
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,9 @@ Datetimelike
843843
- Bug in :meth:`DatetimeIndex.intersection` and :meth:`TimedeltaIndex.intersection` with results not having the correct ``name`` attribute (:issue:`33904`)
844844
- Bug in :meth:`DatetimeArray.__setitem__`, :meth:`TimedeltaArray.__setitem__`, :meth:`PeriodArray.__setitem__` incorrectly allowing values with ``int64`` dtype to be silently cast (:issue:`33717`)
845845
- Bug in subtracting :class:`TimedeltaIndex` from :class:`Period` incorrectly raising ``TypeError`` in some cases where it should succeed and ``IncompatibleFrequency`` in some cases where it should raise ``TypeError`` (:issue:`33883`)
846+
- Bug in constructing a Series or Index from a read-only NumPy array with non-ns
847+
resolution which converted to object dtype instead of coercing to ``datetime64[ns]``
848+
dtype when within the timestamp bounds (:issue:`34843`).
846849
- The ``freq`` keyword in :class:`Period`, :func:`date_range`, :func:`period_range`, :func:`pd.tseries.frequencies.to_offset` no longer allows tuples, pass as string instead (:issue:`34703`)
847850

848851
Timedelta

pandas/_libs/tslibs/conversion.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ def ensure_datetime64ns(arr: ndarray, copy: bool=True):
167167
"""
168168
cdef:
169169
Py_ssize_t i, n = arr.size
170-
int64_t[:] ivalues, iresult
170+
const int64_t[:] ivalues
171+
int64_t[:] iresult
171172
NPY_DATETIMEUNIT unit
172173
npy_datetimestruct dts
173174

pandas/tests/base/test_constructors.py

+24
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@
1313
from pandas.core.base import NoNewAttributesMixin, PandasObject
1414

1515

16+
@pytest.fixture(
17+
params=[
18+
Series,
19+
lambda x, **kwargs: DataFrame({"a": x}, **kwargs)["a"],
20+
lambda x, **kwargs: DataFrame(x, **kwargs)[0],
21+
Index,
22+
],
23+
ids=["Series", "DataFrame-dict", "DataFrame-array", "Index"],
24+
)
25+
def constructor(request):
26+
return request.param
27+
28+
1629
class TestPandasDelegate:
1730
class Delegator:
1831
_properties = ["foo"]
@@ -145,3 +158,14 @@ def test_constructor_datetime_outofbound(self, a, klass):
145158
msg = "Out of bounds"
146159
with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg):
147160
klass(a, dtype="datetime64[ns]")
161+
162+
def test_constructor_datetime_nonns(self, constructor):
163+
arr = np.array(["2020-01-01T00:00:00.000000"], dtype="datetime64[us]")
164+
expected = constructor(pd.to_datetime(["2020-01-01"]))
165+
result = constructor(arr)
166+
tm.assert_equal(result, expected)
167+
168+
# https://github.com/pandas-dev/pandas/issues/34843
169+
arr.flags.writeable = False
170+
result = constructor(arr)
171+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)