-
-
Notifications
You must be signed in to change notification settings - Fork 12k
Description
Describe the issue:
When using np.testing.assert_allclose with timedelta64 arrays and specifying atol as a timedelta64, a ValueError is raised due to an unsupported format code in the error message construction.
According to the source code below, formatter for np.timedelta64 seems not to be defined.
numpy/numpy/_core/src/multiarray/scalartypes.c.src
Lines 649 to 652 in 8f9a6b2
| else if (PyArray_IsScalar(self, Integer) | |
| && !PyArray_IsScalar(self, Timedelta)) { | |
| obj = Py_TYPE(self)->tp_as_number->nb_int(self); | |
| } |
If it is not intentional, I am happy to open a PR to fix this. Maybe, just removing the format specifier in the code below is enough?
numpy/numpy/testing/_private/utils.py
Line 1769 in 8f9a6b2
| header = f'Not equal to tolerance rtol={rtol:g}, atol={atol:g}' |
This is partially related to #29619
Reproduce the code example:
import numpy as np
a = np.array([1, 2], dtype="m8[s]")
b = np.array([3, 4], dtype="m8[s]")
np.testing.assert_allclose(a, b, atol=np.timedelta64(1, "s"))Error message:
Traceback (most recent call last):
File "/home/riku/repos/personals/numpy/workspace/b.py", line 8, in <module>
np.testing.assert_allclose(a, b, atol=np.timedelta64(1, "s"))
File "/home/riku/repos/personals/numpy/build-install/usr/lib/python3.12/site-packages/numpy/testing/_private/utils.py", line 1769, in assert_allclose
header = f'Not equal to tolerance rtol={rtol:g}, atol={atol:g}'
ValueError: Unknown format code 'g' for object of type 'str'Python and NumPy Versions:
2.5.0.dev0+git20251205.8f9a6b2
3.12.3 (main, Jul 12 2025, 11:14:50) [GCC 11.4.0]
Runtime Environment:
No response
Context for the issue:
No response