8000 np.set_printoptions doesn't affect floats · Issue #11048 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

np.set_printoptions doesn't affect floats #11048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
louisabraham opened this issue May 5, 2018 · 8 comments
Open

np.set_printoptions doesn't affect floats #11048

louisabraham opened this issue May 5, 2018 · 8 comments

Comments

@louisabraham
Copy link
>>> import numpy as np
>>> '%s' % np.array(1/3)
'0.3333333333333333'
>>> '%s' % np.array([1/3])
'[0.33333333]'
>>> np.set_printoptions(formatter={"float_kind": lambda x: "%.1f" % x})
>>> '%s' % np.array(1/3)
'0.3333333333333333'
>>> '%s' % np.array([1/3])
'[0.3]'
@eric-wieser
Copy link
Member

A simpler reproduction is:

>>> np.set_printoptions(formatter={"float_kind": lambda x: "%.1f" % x})
>>> str(np.float64(1/3))
str(np.float64(1/3))

@ahaldane, do you think this is a feature (full precision is always shown for scalars) or an omission?

@ahaldane
Copy link
Member
ahaldane commented May 7, 2018

We definitely discussed this before, and I recall that it is intentional. Perhaps here?: #9201 (comment)

(there may have been other places)

Anyway, I think for back-compat reasons we concluded that scalars must continue using a hardcoded repr, ie they are not affected by set_printoptions. In the thread above there was some discussion of whether that should apply to 0d arrays to, the answer was "no, they are affected".

@eric-wieser
Copy link
Member

I also looked back at that issue, and I think what I meant to say was that str(0d_arr) should be the same as str(0d_arr[()]) - which doesn't preclude messing with str(np.generic)

@stefanv
Copy link
Contributor
stefanv commented Oct 23, 2018

This is very confusing. I just ran into the issue again that the following two prints give different results:

In [1]: import numpy as np

In [2]: np.set_printoptions(precision=3)

In [3]: print(np.array(0.12345678))
0.12345678

In [4]: print(np.array([0.12345678]))
[0.123]

Could we revisit this decision?

@charris
Copy link
Member
charris commented Oct 23, 2018

I think this is because float inherits from Python float and uses that for printing.

@eric-wieser
Copy link
Member

@charris: I get the same behavior for np.float32, so I don't think so

@eric-wieser
Copy link
Member

I think the decision goes something like this:

  1. np.set_printoptions applies to arrays, not scalars
  2. str(0d_arr) should be the same as str(0d_arr[()]), in the same way that float(0d_arr) matches float(0d_arr[()])

I'm not sure I can rationalize the first step.

@ahaldane
Copy link
Member

For the record, this behavior goes back as far as numpy 1.0.1, and probably before.

#9201 shows our attempts to update the str/repr of 0d arrays and why we had to keep this behavior. As I recall it was due to people expecting that str applied to a 0d array of S or U dtype would always give back the string itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants
0