-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
TypeError: unsupported format string passed to numpy.ndarray.__format__ #12491
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
Comments
I applied a small patch to cpython to try and get it to print what that format string is -- it appears to be garbage (?) diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index ed2d40064a..eb1d85e295 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -4453,8 +4453,8 @@ object_format(PyObject *self, PyObject *args)
should reject format specifications */
if (PyUnicode_GET_LENGTH(format_spec) > 0) {
PyErr_Format(PyExc_TypeError,
- "unsupported format string passed to %.200s.__format__",
- self->ob_type->tp_name);
+ "unsupported format string passed to %.200s.__format__: '%s'",
+ self->ob_type->tp_name, PyUnicode_AsUTF8String(format_spec));
return NULL;
}
self_as_str = PyObject_Str(self);
hexdumped:
|
oh 🤦♂️, my printing is wrong -- that's the |
looks related to #5543 |
I stumbled on this issue when printing a pandas data frame with a poorly formed index. Hope it can be solved as this could be a common mistake and the traceback message does not provide much clue as to what went wrong (other than it was in NumPy not Pandas). Steps to reproduce:
|
@billtubbs I think you should probably move that to pandas, I am not sure |
Ah. Okay thanks. Then I will do that. |
Actually, I just noticed that this error is now being caught in the latest version of Pandas (1.0.0) when the index is created. Sorry for unnecessarily raising it here. |
@charris I believe the original issue is still a thing ? |
I just hit this through pyupgrade also. The fact that |
@charris Yes, I also think this this issue should be reopened; the behaviour seems really inconsistent: print("hello %.2f" % np.array([1.38362])) # works
print("hello %.2f" % np.array([[1.38362]])) # works
print(f"hello {np.array([1.38362]):.2f}") # TypeError It would be really nice to have the last think working as well! |
I think this issue is closed because it is a duplicate of #5543 There is no reason to expect that >>> from decimal import Decimal
>>> d = Decimal("1.234")
>>> f"{d:E}"
'1.234E+0'
>>> "%E" % d
'1.234000E+00' |
Reproducing code example:
Error message:
NumPy/Python version information:
OS: macOS High Sierra and Linux (Travis CI)
This happened when upgrading a codebase using the pyupgrade tool.
%g
and{:g}
work the same for builtin types. Is this a NumPy bug?The text was updated successfully, but these errors were encountered: