Skip to content

BUG: None should be a valid colspec? #7079

Closed
@AllenDowney

Description

@AllenDowney

In https://github.com/pydata/pandas/blob/master/pandas/io/parsers.py, the init method for FixedWidthReaders checks whether the colspecs are valid. Since None is a value slice index, should it be allowed as a colspec?

This would help me because I don't know the width of the last column until I start reading the file.

class FixedWidthReader(object):
"""
A reader of fixed-width lines.
"""
def __init__(self, f, colspecs, delimiter, comment):
    self.f = f
    self.buffer = None
    self.delimiter = '\r\n' + delimiter if delimiter else '\n\r\t '
    self.comment = comment
    if colspecs == 'infer':
        self.colspecs = self.detect_colspecs()
    else:
        self.colspecs = colspecs

    if not isinstance(self.colspecs, (tuple, list)):
        raise TypeError("column specifications must be a list or tuple, "
                        "input was a %r" % type(colspecs).__name__)

    for colspec in self.colspecs:
        if not (isinstance(colspec, (tuple, list)) and
                len(colspec) == 2 and
                isinstance(colspec[0], (int, np.integer)) and
                isinstance(colspec[1], (int, np.integer))):
            raise TypeError('Each column specification must be '
                            '2 element tuple or list of integers')

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions