Numeric data loaded from file using memmap exception when used in formatted output

The following example demonstrates the problem.

>>> 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 mode = 'r' in the boot function, everything works as expected. This is mistake? Or, is there something missing?

Is there a way to "extract" the float value from the memmap so I can use it directly?

EDIT:

The "item" method can be used to "Copy and return an array item to a standard Python scalar." So the following code works:

>>> print("{:6.3}".format(Y.min().item(0)))
  -2.31

      

Is there a rhyme or a reason you need to extract a meaning in order to use it?

+3


source to share


1 answer


https://github.com/numpy/numpy/issues/5543

ndarray should offer a format that can adjust the precision

According to this issue, since the last February,

n = np.array([1.23, 4.56])
print('{0:.6} AU'.format(n))

      



creates the same error

TypeError: non-empty format string passed to object.__format__

      

I am assuming the objects numpy

memmap

have the same problem and the same solution is possible.

Obviously the py3 style format

still doesn't work, at least for adding to packages like numpy

.

+2


source







All Articles