[Python] Pandas ValueError when using on_bad_lines

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    Question: What I may be doing wrong and how we can fix the following error?

    import pandas as pd

    def handle_bad_line(bad_line: list[str]) -> list[str] | None:
    # Do something with the bad line, e.g., print it or modify it
    print("Bad line:", bad_line)
    # Return a modified line (if needed) or None to skip it
    return None # Skip the bad line

    df = pd.read_csv("DataFile.csv",engine="python", on_bad_lines=handle_bad_line)


    Error:


    ValueError: Argument <function handle_bad_line at 0x000001D9A6683E20> is invalid for on_bad_lines
    ​

    Remark: The function option for on_bad_lines was added on Pandas 1.4.0 and above. Following is from official document:


    Callable, function with signature (bad_line: list[str]) -> list[str] | None that will process a single bad line. bad_line is a list of strings split by the sep. If the function returns None, the bad line will be ignored. If the function returns a new list of strings with more elements than expected, a ParserWarning will be emitted while dropping extra elements. Only supported when engine='python'
    ​

    Continue reading...

Compartilhe esta Página