0
    xtup = np.random.normal(92725500, scale=1, size=100),

    print(xtup)
    
    (array([92725499.6057292 , 92725500.93153155, 92725498.27913634, 92725498.59134175]))
    
    xtup = list(map('{:.0f}'.format,xtup))

When I run the above code, I get the following numpy array formatting error.

TypeError: unsupported format string passed to numpy.ndarray.__format__

2 Answers 2

1
In [274]: xtup = np.random.normal(92725500, scale=1, size=3),
     
In [275]: xtup
Out[275]: (array([92725501.65265064, 92725500.49281569, 92725498.95281461]),)
In [276]: list(map('{:.0f}'.format,xtup))
Traceback (most recent call last):
  File "<ipython-input-276-cbe978a45009>", line 1, in <module>
    list(map('{:.0f}'.format,xtup))
TypeError: unsupported format string passed to numpy.ndarray.__format__

But if I extract the array from the tuple:

In [277]: list(map('{:.0f}'.format,xtup[0]))
Out[277]: ['92725502', '92725500', '92725499']

That format string cannot be used to format an array; it can only format numbers, such as the elements of a numeric array.

I was going to point out that the comma created a tuple, but the name xtup suggests you already realize that.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure where you are getting the error, but when I run your code it works without issue.

I get this result

['92725500', '92725499', ..., '92725501']

What version of numpy are you using? That could be the issue. I am using numpy 1.19.3. If you are unsure, you can use np.version.version to find out.

2 Comments

I am on numpy version 1.19.2
Version has nothing to do with it. He's trying to format an array within a tuple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.