【问题标题】:Parsing using Polars使用 Polar 解析
【发布时间】:2022-01-24 18:47:51
【问题描述】:

我正在尝试使用 read_csv 命令将数据加载到 Polars DataFrame 中,但我不断收到此错误

RuntimeError: Any(ComputeError("Could not parse 0.5 as dtype Int64 at column 13.\n                                            The total offset in the file is 11684833 bytes.\n\n                                            Consider running the parser `with_ignore_parser_errors=true`\n                                            or consider adding 0.5 to the `null_values` list."))

虽然我使用转换器参数如下:

converters = {
    'Date': lambda x: datetime.strptime(x, "%b %d, %Y"),
    'Number': lambda x: float(x)
    }

错误仍然存​​在。 我也尝试使用错误中显示的参数:

with_ignore_parser_errors=TRUE

错误仍然存​​在。我能做些什么? 我的问题不在于解析日期,而在于解析数字。 这就是我现在所拥有的:

    converters = {
    'Date': lambda x: datetime.strptime(x, "%b %d, %Y"),
    'Number': lambda x: float(x)
    }
    df_file = pl.read_csv(file_to_read, has_headers=True, converters=converters,with_ignore_parser_errors=TRUE)

【问题讨论】:

  • 我也尝试使用函数作为转换器 def col_fixer(x): try: return float(x) except ValueError: return np.str df_file = pl.read_csv(file_to_read, has_headers=True,转换器=dict(B=col_fixer))

标签: python parsing numbers python-3.6 python-polars


【解决方案1】:

Polars 没有converters 参数。所以这行不通。

似乎浮点列正试图被解析为整数。您可以手动将dtype 设置为pl.Int64,方法是将列名传递为kwargspl.read_csv(.., dtype = {"foo": pl.Int64}

或者您可以增加infer_schema_length 以便 Polars 自动检测浮点数(前 100 行可能只包含整数)。

默认为100,尝试增加它直到架构推断正确检测到浮点列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-11
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    相关资源
    最近更新 更多