问题
I have a large PyArrow table with one column called index
that I would like to use to partition the table; each separate value of index
represents a different quantity in the table.
Is there an idiomatic way to select rows from a PyArrow table based on contents of a column?
Here's an example table:
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]
It's very easy to do this selection in 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]
But for PyArrow I have to do some shuffling around between PyArrow and either numpy or 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]
Is there a more straightforward way?
回答1:
A conversion to numpy is not needed to do a boolean filter operation. You can use the equal
and filter
functions from the pyarrow.compute
module for this:
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)
来源:https://stackoverflow.com/questions/64578761/is-there-a-more-idiomatic-way-to-select-rows-from-a-pyarrow-table-based-on-conte