-
-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Description
Describe the issue:
Overview
np.datetime64 does not allow generic unit except for NaT.
However, np.ones(1, "M8") does not raise any error until the array is explicitly printed.
I think the following code example should raise an error immediately when np.ones is called.
Suggestion
According to the implementation of np.ones, the int value 1 is forced to be set to the empty array.
How about using np.array(1, dtype=a.dtype) instead of 1?
Lines 232 to 234 in e87a3c5
| a = empty(shape, dtype, order, device=device) | |
| multiarray.copyto(a, 1, casting='unsafe') | |
| return a |
This way is also helpful when timedelta with generic unit is deprecated because this avoids to set 1 to the empty array with specific unit.
Reproduce the code example:
import numpy as np
# This does NOT raise any error.
a = np.ones(1, "M8")
# At this point, the error is raised
print(a)Error message:
Python and NumPy Versions:
2.5.0.dev0+git20260227.e87a3c5
3.12.3 (main, Jul 12 2025, 11:14:50) [GCC 11.4.0]
Runtime Environment:
[{'numpy_version': '2.5.0.dev0+git20260227.e87a3c5',
'python': '3.12.3 (main, Jul 12 2025, 11:14:50) [GCC 11.4.0]',
'uname': uname_result(system='Linux', node='pop-os', release='6.17.9-76061709-generic', version='#202511241048176470475122.04~b24b425 SMP PREEMPT_DYNAMIC Tue D', machine='x86_64')},
{'simd_extensions': {'baseline': ['X86_V2'],
'found': ['X86_V3'],
'not_found': ['X86_V4', 'AVX512_ICL']}},
{'ignore_floating_point_errors_in_matmul': False},
{'architecture': 'Haswell',
'filepath': '/usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so',
'internal_api': 'openblas',
'num_threads': 14,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.20'}]
How does this issue affect you or how did you find it:
I am currently working on gh-29619.
The problem in the PR is that np.ones now raise DeprecationWarning even if dtype is set timedelta64 with unit.
So, I investigated the behavior of np.ones and encountered this issue.