Skip to content

BUG: ensure_datetime64ns with bigendian array #30976

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 3 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ def ensure_datetime64ns(arr: ndarray, copy: bool=True):

shape = (<object>arr).shape

if (<object>arr).dtype.byteorder == ">":
# GH#29684 we incorrectly get OutOfBoundsDatetime if we dont swap
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know where this actually is causing the problem? Are we doing some low level byte swapping elsewhere in our code or just something getting thrown off in interfacing with numpy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we do the arr.view(np.int64) that view is little-endian

dtype = arr.dtype
arr = arr.astype(dtype.newbyteorder("<"))

ivalues = arr.view(np.int64).ravel()

result = np.empty(shape, dtype=NS_DTYPE)
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/tslibs/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ def test_length_zero_copy(dtype, copy):
assert result.base is (None if copy else arr)


def test_ensure_datetime64ns_bigendian():
# GH#29684
arr = np.array([np.datetime64(1, "ms")], dtype=">M8[ms]")
result = conversion.ensure_datetime64ns(arr)

expected = np.array([np.datetime64(1, "ms")], dtype="M8[ns]")
tm.assert_numpy_array_equal(result, expected)


class SubDatetime(datetime):
pass

Expand Down