Skip to content

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

Closed

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.

Activity

  1. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    @mwiebe wrote on 2011-03-25

    Fixed in master, except for the long double formatting. Leaving it out of 1.6 as it's neither a perf nor crash bug.

  2. numpy-gitbot commented on Oct 19, 2012

    @numpy-gitbot
    Author

    Milestone changed to NumPy 1.7 by @charris on 2012-04-26

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions