The following example demonstrates the issue.
>>> import numpy as np
>>> X = np.random.randn(10,3)
>>> np.save("x.npy", X)
>>> Y = np.load("x.npy", "r")
>>> Y.min()
memmap(-2.3064808987512744)
>>> print(Y.min())
-2.3064808987512744
>>> print("{}".format(Y.min()))
-2.3064808987512744
>>> print("{:6.3}".format(Y.min()))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: non-empty format string passed to object.__format__
Without the mode = 'r' in the load function, everything works as expected. Is this a bug? Or, am missing something?
Is there anyway to 'extract' the float value from the memmap to use it directly?
EDIT:
The 'item' method can be used to 'Copy an element of an array to a standard Python scalar and return it'. So, the following code works:
>>> print("{:6.3}".format(Y.min().item(0)))
-2.31
Is there a rhyme or reason when you need to extract a value to use it?
'{}'.formatmechanism depends on the object (in this a numpy array or datatype) implementing a__format__method.%fformatting method? Even when it 'works', the formating isn't quite right, e.g. '-2. ' instead of ' -2.03'. The new formatting method might still be poorly implemented fornumpy.