Skip to content

Exposing custom formatters for user dtypes #194

@SwayamInSync

Description

@SwayamInSync

Since numpy_quaddtype is a subtype of PyFloatingArrType_Type, NumPy decides the formatter for printing arrays as follows

 if formatter is not None:
        fkeys = [k for k in formatter.keys() if formatter[k] is not None]
        if 'all' in fkeys:
            for key in formatdict.keys():
                formatdict[key] = indirect(formatter['all'])
        if 'int_kind' in fkeys:
            for key in ['int']:
                formatdict[key] = indirect(formatter['int_kind'])
        if 'float_kind' in fkeys:
            for key in ['float', 'longfloat']:
                formatdict[key] = indirect(formatter['float_kind'])
        if 'complex_kind' in fkeys:
            for key in ['complexfloat', 'longcomplexfloat']:
                formatdict[key] = indirect(formatter['complex_kind'])
        if 'str_kind' in fkeys:
            formatdict['numpystr'] = indirect(formatter['str_kind'])
        for key in formatdict.keys():
            if key in fkeys:
                formatdict[key] = indirect(formatter[key])

File: https://github.com/numpy/numpy/blob/main/numpy/_core/arrayprint.py

The usage of default float_kind formatter cannot handle the quad precision values (as it casts them to float64). One workaround is to override this (which I currently use for testing) as

np.set_printoptions(formatter={'float_kind': lambda x: str(x)}) # or repr(x) for scientific notations

But this affects all float types. A better workaround would be allowing dtypes exposing their custom formatters.

cc: @seberg @ngoldbaum

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions