Skip to content

FIX: np.insert fails with datetime64 and string input combination #29339 #29408

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

Closed
Closed
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
Next Next commit
FIX: handle insertion of 0-dim datetime64 arrays into string scalars …
…in np.insert (#29339)

- Fix np.insert to correctly convert 0-dim np.datetime64 arrays to strings when inserting into string arrays.
 This resolves RuntimeError when inserting datetime64 wrapped in 0-d arrays.
  • Loading branch information
AnkitAhlawat7742 committed Jul 22, 2025
commit 73ad033aa1a33e7a87a6a2ac1549be899118f073
9 changes: 8 additions & 1 deletion numpy/lib/_function_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5612,7 +5612,14 @@ def insert(arr, obj, values, axis=None):
f"with size {N}")
if (index < 0):
index += N

# np.insert fails with datetime64 and string input combination #29339
if isinstance(values, np.ndarray):
if np.issubdtype(values.dtype, np.datetime64) and \
np.issubdtype(arr.dtype, np.str_):
if values.ndim == 0:
values = values.item()
else:
values = values.astype(str)
# There are some object array corner cases here, but we cannot avoid
# that:
values = array(values, copy=None, ndmin=arr.ndim, dtype=arr.dtype)
Expand Down
7 changes: 7 additions & 0 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,13 @@ def test_return_type(self):
res = np.gradient(([1, 2], [2, 3]))
assert type(res) is tuple

def test_insert_0d_datetime64_into_string_scalar(self):
# np.insert fails with datetime64 and string input combination #29339
arr = '5'
val_0d = np.array(np.datetime64('2025-10-10')) # 0-d array
result_0d = np.insert(arr, 0, val_0d)
expected_0d = np.array(['2025-10-10', '5'], dtype='<U10')
assert_array_equal(result_0d, expected_0d)

class TestAngle:

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy