是否有更惯用的方法来根据列的内容从 PyArrow 表中选择行?

2024-03-25

我有一个很大的 PyArrow 表,其中有一列名为index我想用它来对表进行分区;每个单独的值index代表表中不同的数量。

是否有一种惯用的方法可以根据列的内容从 PyArrow 表中选择行?

这是一个示例表:

import pyarrow as pa
import pyarrow.parquet as pq
import pandas as pd
import numpy as np

# Example table for data schema
irow = np.arange(2**20)
dt = 17
df0 = pd.DataFrame({'timestamp': np.array((irow//2)*dt, dtype=np.int64),
                   'index':     np.array(irow%2, dtype=np.int16),
                   'value':     np.array(irow*0, dtype=np.int32)},
                   columns=['timestamp','index','value'])
ii = df0['index'] == 0
df0.loc[ii,'value'] = irow[ii]//2
ii = df0['index'] == 1
df0.loc[ii,'value'] = (np.sin(df0.loc[ii,'timestamp']*0.01)*10000).astype(np.int32)
table0 = pa.Table.from_pandas(df0)
print(df0)

# prints the following:
         timestamp  index   value
0                0      0       0
1                0      1       0
2               17      0       1
3               17      1    1691
4               34      0       2
...            ...    ...     ...
1048571    8912845      1    9945
1048572    8912862      0  524286
1048573    8912862      1    9978
1048574    8912879      0  524287
1048575    8912879      1    9723

[1048576 rows x 3 columns]

在 Pandas 中进行此选择非常容易:

print(df0[df0['index']==1])

# prints the following
         timestamp  index  value
1                0      1      0
3               17      1   1691
5               34      1   3334
7               51      1   4881
9               68      1   6287
...            ...    ...    ...
1048567    8912811      1   9028
1048569    8912828      1   9625
1048571    8912845      1   9945
1048573    8912862      1   9978
1048575    8912879      1   9723

[524288 rows x 3 columns]

但对于 PyArrow,我必须在 PyArrow 和 numpy 或 pandas 之间进行一些调整:

value_index = table0.column('index').to_numpy()
# get values of the index column, convert to numpy format
row_indices = np.nonzero(value_index==1)[0]
# find matches and get their indices
selected_table = table0.take(pa.array(row_indices))
# use take() with those indices
v = selected_table.column('value')
print(v.to_numpy())

# which prints
[   0 1691 3334 ... 9945 9978 9723]

有更直接的方法吗?


执行布尔过滤操作不需要转换为 numpy。您可以使用equal and filter函数从pyarrow.compute用于此的模块:

import pyarrow.compute as pc

value_index = table0.column('index')
row_mask = pc.equal(value_index, pa.scalar(1, value_index.type))
selected_table = table0.filter(row_mask)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否有更惯用的方法来根据列的内容从 PyArrow 表中选择行? 的相关文章

随机推荐