Skip to content

BUG: Issue with converter function in loadtxt() #22007

@kmarwah

Description

@kmarwah

Describe the issue:

Passing a single converter function as an argument to the loadtxt function results in an error. The attached code example is from the numpy documentation.

Reproduce the code example:

import numpy
from io import StringIO
s = StringIO('10.01 31.25-\n19.22 64.31\n17.57- 63.94')
def conv(fld):
    return -float(fld[:-1]) if fld.endswith(b'-') else float(fld)

a = numpy.loadtxt(s, converters=conv)

Error message:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-6ec83d448f1e> in <module>()
      4     return -float(fld[:-1]) if fld.endswith(b'-') else float(fld)
      5 
----> 6 a = numpy.loadtxt(s, converters=conv)

/usr/local/lib/python3.7/dist-packages/numpy/lib/npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin, encoding, max_rows, like)
   1119 
   1120         # By preference, use the converters specified by the user
-> 1121         for i, conv in (user_converters or {}).items():
   1122             if usecols:
   1123                 try:

AttributeError: 'function' object has no attribute 'items'

NumPy/Python version information:

1.21.6 3.7.13 (default, Apr 24 2022, 01:04:09)
[GCC 7.5.0]

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