Skip to content

Commit 81d9636

Browse files
jbrockmendeljreback
authored andcommitted
BUG: ensure_datetime64ns with bigendian array (#30976)
1 parent 3471270 commit 81d9636

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Numeric
8282

8383
Conversion
8484
^^^^^^^^^^
85-
85+
- Bug in :class:`Series` construction from NumPy array with big-endian ``datetime64`` dtype (:issue:`29684`)
8686
-
8787
-
8888

pandas/_libs/tslibs/conversion.pyx

+5
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ def ensure_datetime64ns(arr: ndarray, copy: bool=True):
9999

100100
shape = (<object>arr).shape
101101

102+
if (<object>arr).dtype.byteorder == ">":
103+
# GH#29684 we incorrectly get OutOfBoundsDatetime if we dont swap
104+
dtype = arr.dtype
105+
arr = arr.astype(dtype.newbyteorder("<"))
106+
102107
ivalues = arr.view(np.int64).ravel()
103108

104109
result = np.empty(shape, dtype=NS_DTYPE)

pandas/tests/series/test_constructors.py

+9
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,15 @@ def test_constructor_with_naive_string_and_datetimetz_dtype(self, arg):
967967
expected = Series(pd.Timestamp(arg)).dt.tz_localize("CET")
968968
tm.assert_series_equal(result, expected)
969969

970+
def test_constructor_datetime64_bigendian(self):
971+
# GH#30976
972+
ms = np.datetime64(1, "ms")
973+
arr = np.array([np.datetime64(1, "ms")], dtype=">M8[ms]")
974+
975+
result = Series(arr)
976+
expected = Series([Timestamp(ms)])
977+
tm.assert_series_equal(result, expected)
978+
970979
@pytest.mark.parametrize("interval_constructor", [IntervalIndex, IntervalArray])
971980
def test_construction_interval(self, interval_constructor):
972981
# construction from interval & array of intervals

pandas/tests/tslibs/test_conversion.py

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def test_length_zero_copy(dtype, copy):
7272
assert result.base is (None if copy else arr)
7373

7474

75+
def test_ensure_datetime64ns_bigendian():
76+
# GH#29684
77+
arr = np.array([np.datetime64(1, "ms")], dtype=">M8[ms]")
78+
result = conversion.ensure_datetime64ns(arr)
79+
80+
expected = np.array([np.datetime64(1, "ms")], dtype="M8[ns]")
81+
tm.assert_numpy_array_equal(result, expected)
82+
83+
7584
class SubDatetime(datetime):
7685
pass
7786

0 commit comments

Comments
 (0)