[Python] How to specify relevant columns

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    As far as I can tell, the following MRE conforms to the relevant documentation:

    import polars


    df = polars.read_excel(
    "/Volumes/Spare/foo.xlsx",
    engine="calamine",
    sheet_name="natsav",
    read_options={"header_row": 2},
    columns=(1,2,4,5,6,7), # columns 0 and 3 are not needed
    )

    print(df.head())


    The issue here is that the documentation states that for the columns parameter:


    Columns to read from the sheet; if not specified, all columns are read. Can be given as a sequence of column names or indices.
    ​

    Clearly, a tuple is a sequence. However, running this code results in an exception as follows:

    _fastexcel.InvalidParametersError: invalid parameters: `use_columns` callable could not be called (TypeError: 'tuple' object is not callable)


    Further research reveals that the required callable should return bool. So:

    def colspec(c):
    print(type(c))
    return True


    I then change the read_excel call to include columns=colspec.

    The program now runs without exception and reveals that the parameter passed is a class of type builtins.ColumnInfoNoDtype.

    Unfortunately, I can't find any documentation for that type.

    Is the documentation wrong? How is one supposed to used polars.read_excel to load only certain specific columns?

    Continue reading...

Compartilhe esta Página