Skip to content

No support for __format__ in scalars/arrays (Trac #1675) #2271

@numpy-gitbot

Description

@numpy-gitbot

Original ticket http://projects.scipy.org/numpy/ticket/1675 on 2010-11-15 by @pv, assigned to unknown.

Numpy scalars and arrays don't work properly in the format() string formatting context:

>>> '{0:d}'.format(np.int16(1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'd' for object of type 'str'

The reason seems to be that the __format__ method is inherited from object, and it converts the item to string via str first.

The correct fix seems to be to implement a properly formatting-code aware __format__ ourselves. For most types, this can probably be done directly by calling PyInt's or PyFloat's routine. However, for e.g. long doubles we might want to implement the formatting ourselves.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions