Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop numpy floats being displayed as "np.float64"?

I have a large library with many doctests. All doctests pass on my computer. When I push changes to GitHub, GitHub Actions runs the same tests in Python 3.8, 3.9, 3.10 and 3.11. All tests run correctly on on Python 3.8; however, on Python 3.9, 3.10 and 3.11, I get many errors of the following type:

Expected:
    [13.0, 12.0, 7.0]
Got:
    [np.float64(13.0), np.float64(12.0), np.float64(7.0)]

I.e., the results are correct, but for some reason, they are displayed inside "np.float64".

In my code, I do not use np.float64 at all, so I do not know why this happens. Also, as the tests pass on my computer, I do not know how to debug the error, and it is hard to produce a minimal working example. Is there a way I can make the doctests pass again, without changing each individual test?

like image 602
Erel Segal-Halevi Avatar asked Dec 02 '25 09:12

Erel Segal-Halevi


1 Answers

numpy allows you to control this without downgrading:

np.set_printoptions(legacy='1.25')

See numpy.set_printoptions for details.

This is also stated here: Representation of NumPy scalars changed - NumPy 2.0.0 Release Notes

like image 65
Leolo Avatar answered Dec 04 '25 22:12

Leolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!