-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Description
Describe the issue:
When inserting a 0-dimensional array containing a numpy.datetime64 value into a scalar string (e.g., '5') using np.insert
, NumPy raises a RuntimeError. However, inserting a direct numpy.datetime64
scalar value (not wrapped in an array) does not trigger the error and instead proceeds by converting the datetime to its string representation. When inserting the same values into a 0-dimensional array of dtype StringDType()
, no error occurs.
This appears to be a bug (I guess) in how NumPy handles type coercion and formatting during insertion, especially when dealing with datetime64
and str
. According to the numpy documentation,, values
should be cast to the type of arr
. I don't know why it didn't work, maybe datetime64
is special?
Reproduce the code example:
import numpy as np
from numpy.dtypes import StringDType
timev1 = np.datetime64('2025-10-10')
timev2 = np.array(np.datetime64('2025-10-10'))
timev3 = datetime.date(2025, 10, 10)
timev4 = np.array(datetime.date(2025, 10, 10))
arrv1 = '5'
arrv2 = np.array('5', dtype=StringDType())
t = np.insert(arrv2, 0, timev1)
t = np.insert(arrv2, 0, timev2)
t = np.insert(arrv2, 0, timev3)
t = np.insert(arrv2, 0, timev4)
# All outputs are as expected
# array(['2025-10-10', '5'], dtype=StringDType())
# array(['2025-10-10', '5'], dtype=StringDType())
# array(['2025-10-10', '5'], dtype=StringDType())
# array(['2025-10-10', '5'], dtype=StringDType())
t = np.insert(arrv1, 0, timev1)
print(repr(t)) #array(['2', '5'], dtype='<U1')
t = np.insert(arrv1, 0, timev2)
print(repr(t)) # fail
# RuntimeError: The string provided for NumPy ISO datetime formatting was too short, with length 1
t = np.insert(arrv1, 0, timev3)
print(repr(t)) #array(['2', '5'], dtype='<U1')
t = np.insert(arrv1, 0, timev4)
print(repr(t)) #array(['2', '5'], dtype='<U1')
Error message:
Traceback (most recent call last):
File "test.py", line 90, in <module>
t = np.insert(arrv1, 0, timev2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "python3.12/site-packages/numpy/lib/_function_base_impl.py", line 5613, in insert
values = array(values, copy=None, ndmin=arr.ndim, dtype=arr.dtype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: The string provided for NumPy ISO datetime formatting was too short, with length 1
Python and NumPy Versions:
2.3.1
3.12.0 | packaged by Anaconda, Inc. | (main, Oct 2 2023, 17:29:18) [GCC 11.2.0]
Runtime Environment:
[{'numpy_version': '2.3.1',
'python': '3.12.0 | packaged by Anaconda, Inc. | (main, Oct 2 2023, '
'17:29:18) [GCC 11.2.0]',
'uname': uname_result(system='Linux', node='gpu-node5', release='5.4.0-100-generic', version='#113-Ubuntu SMP Thu Feb 3 18:43:29 UTC 2022', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2',
'AVX512F',
'AVX512CD',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL'],
'not_found': ['AVX512_KNL', 'AVX512_KNM', 'AVX512_SPR']}},
{'architecture': 'SkylakeX',
'filepath': '/home/miniconda3/envs/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so',
'internal_api': 'openblas',
'num_threads': 64,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.29'}]
Context for the issue:
None