代码之家  ›  专栏  ›  技术社区  ›  Ben G

尝试将数据帧写入feather时出错?

  •  0
  • Ben G  · 技术社区  · 7 年前

    我同时使用R和Python,我想将其中一个数据帧编写为feather,以便在R中更轻松地使用它。但是,当我尝试将其编写为feather时,出现以下错误:

    ArrowInvalid: trying to convert NumPy type float64 but got float32
    

    In[1]
    df.dtypes
    
    Out[1]
    id         Object
    cluster    int64
    vector_x   float64
    vector_y   float64
    

    feather.write_dataframe(df, "path/df.feather") 或 df.to_feather("path/df.feather") .

    https://issues.apache.org/jira/browse/ARROW-1345 https://github.com/apache/arrow/issues/1430

    编辑1:

    df[['vector_x', 'vector_y', 'cluster']] = df[['vector_x', 'vector_y', 'cluster']].astype(float)
    
    df[['doc_id', 'text']] = df[['doc_id', 'text']].astype(str)
    
    df[['doc_vector', 'doc_vectors_2d']] = df[['doc_vector', 'doc_vectors_2d']].astype(list)
    
    df.dtypes
    
    Out[1]:
    doc_id           object
    text             object
    doc_vector       object
    cluster          float64
    doc_vectors_2d   object
    vector_x         float64
    vector_y         float64
    dtype: object
    

    编辑2:

    经过多次搜索,问题似乎是我的cluster列是由int64整数组成的列表类型。所以我想真正的问题是,羽毛格式支持列表吗?

    编辑3:

    不过,feather不支持列表之类的嵌套数据类型,至少目前还不支持。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ajay Liu    6 年前
    • 幸运的是,我在这里找到了错误的原因。

    解决方案:

    pip install pyarrow==latest # my version is 1.0.0 and it work
    

    那么,还是用吧警局给你的羽毛(“文件名”)并读取。

        2
  •  4
  •   Uwe L. Korn    7 年前

    id Object 列。这些是Python对象,不能用语言无关的格式表示。这根羽毛(实际上是下面的阿帕奇箭头/ pyarrow )正在尝试猜测 id 列。猜测是在它在列中看到的第一个对象上完成的。这些是 float64 努比标量。稍后,你有 float32 标量。Arrow没有将它们强制为某种类型,而是对类型更严格,并且失败了。

    您应该能够通过确保所有列都具有非对象数据类型来解决此问题 df['id'] = df['id'].astype(float) .

        3
  •  2
  •   Ben G    6 年前