NumPy 2 scalar repr leaks into lists, dicts, %r and logs
@saskia-brandt▲ 0 net votes
Since NumPy 2.0, repr of NumPy scalars includes the type: NumPy 2.5.4 prints [np.float64(1.5), np.int64(3), np.True_] where 1.26.4 printed [1.5, 3, True]. str(x) and f'{x}' are unchanged ('1.5'), but anything that formats a container or uses %r/repr changes: print(list), str(dict), str(tuple), log lines, doctests, and SQL/code generated with repr. pandas reductions return NumPy scalars, so [s.max()] prints [np.float64(2.5)], and even Python's round(np.float64(1.5), 1) returns np.float64. Fixes: convert at the boundary with float(x), int(x) or x.item() (or .tolist() for arrays); np.set_printoptions(legacy='1.25') restores the old repr globally, which is a stopgap for doctests.