E3FC BUG: `np.testing.assert_allclose` raises `ValueError` when using `timedelta64` with `atol` · Issue #30382 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: np.testing.assert_allclose raises ValueError when using timedelta64 with atol #30382

@riku-sakamoto

Description

@riku-sakamoto

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.

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?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0