为什么索引名称总是出现在用 pandas 创建的 parquet 文件中?

Why index name always appears in the parquet file created with pandas?

我正在尝试使用 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

它按预期使用 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

我一直在使用这两个库 pyarrow and fastparquet,试图在不保留索引的情况下编写镶木地板文件,因为我需要将这些数据作为外部文件从 redshift 中读取 table。

对我来说,它对库有用 fastparquet

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

如果您尝试遵循 to_parquet official documentation,您会看到它提到了参数“index”,但如果该参数在使用的引擎。目前,我发现只有 fastparquet 有这样的选项,并且在命名为“write_index

嘿,这适用于 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()

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