【问题标题】:Why index name always appears in the parquet file created with pandas?为什么索引名称总是出现在用 pandas 创建的 parquet 文件中?
【发布时间】:2019-01-23 03:32:57
【问题描述】:

我正在尝试使用 pandas 数据框创建镶木地板,即使我删除了文件的索引,当我重新读取镶木地板文件时它仍然出现。谁能帮我这个?我希望将 index.name 设置为 None

>>> df = pd.DataFrame({'key': 1}, index=[0])
>>> df
  key
0    1
>>> df.to_parquet('test.parquet')
>>> df = pd.read_parquet('test.parquet')
>>> df
     key
index     
0        1
>>> del df.index.name
>>> df
     key
0    1
>>> df.to_parquet('test.parquet')
>>> df = pd.read_parquet('test.parquet')
>>> df
     key
index     
0        1

【问题讨论】:

  • 这是使用 fastparquet 还是 pyarrow 作为 Parquet 后端?
  • 使用 fastparquet

标签: python-3.x pandas dataframe parquet fastparquet


【解决方案1】:

使用 pyarrow 按预期工作:

>>> df = pd.DataFrame({'key': 1}, index=[0])
>>> df.to_parquet('test.parquet', engine='fastparquet')
>>> df = pd.read_parquet('test.parquet')
>>> del df.index.name
>>> df
   key
0    1
>>> df.to_parquet('test.parquet', engine='fastparquet')
>>> df = pd.read_parquet('test.parquet')
>>> df
       key
index     
0        1 ---> INDEX NAME APPEARS EVEN AFTER DELETING USING fastparquet
>>> del df.index.name
>>> df.to_parquet('test.parquet', engine='pyarrow')
>>> df = pd.read_parquet('test.parquet')
>>> df
   key
0    1 --> INDEX NAME IS NONE WHEN CONVERSION IS DONE WITH pyarrow

【讨论】:

    【解决方案2】:

    嘿,这适用于 pyarrow 与以下

    df = pd.DataFrame({'key': 1}, index=[0])
    df.to_parquet('test.parquet', engine='pyarrow', index=False)
    df = pd.read_parquet('test.parquet', engine='pyarrow')
    df.head()
    

    正如to_parquet documentation 中提到的@alexopoulos7 所述,您可以使用“index”参数作为参数。它似乎有效,也许是因为我明确说明了engine='pyarrow'

    【讨论】:

      【解决方案3】:

      我一直在使用 pyarrowfastparquet 这两个库,试图在不保留索引的情况下编写 parquet 文件,因为我需要从 redshift 作为外部表读取这些数据。

      对我来说,它适用于库 fastparquet

      df.to_parquet(destination_file, engine='fastparquet', compression='gzip', write_index=False)
      

      如果您尝试关注to_parquet official documentation,您会看到它提到了参数“index”,但如果使用的引擎中不存在此参数,则会引发错误。目前,我发现只有 fastparquet 有这样的选项,并且命名为 "write_index"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 2021-09-19
        • 1970-01-01
        • 2018-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多