[英]Pandas Dataframe to Apache Beam PCollection conversion problem
[英]How to convert beam dataframe back to PCollection?
下面给出的代码片段运行良好,并给出了预期的结果。
table_pcollection = (p | 'Read table' >> io.ReadFromBigQuery(table=f'{TABLE_PREFIX}.test_table'))
# Requirement is to keep only some columns from tables
df_table = to_dataframe(table_pcollection, proxy=pandas.DataFrame(columns=['col1', 'col3', 'col8', 'is_active']), label=f'CNV: To dataframe')
test_pcoll = (to_pcollection(df_table, label='to pcollcetion final pcollection', yield_elements='pandas', include_indexes=True))
test_pcoll | 'Output of test_pcoll' >> io.WriteToText('output/test_pcoll.txt')
现在,如果我们在数据帧上应用一些操作,例如 [where/groupby/drop]。 它不工作
table_pcollection = (p | 'Read table' >> io.ReadFromBigQuery(table=f'{TABLE_PREFIX}.test_table'))
df_table = to_dataframe(table_pcollection, proxy=pandas.DataFrame(columns=['col1', 'col3', 'col8', 'is_active']), label=f'CNV: To dataframe')
df_table.where((df_table['is_active']) & (df_table['col3'] == 'new'), inplace=True)
df_table.dropna(inplace=True)
test_pcoll = (to_pcollection(df_table, label='to pcollcetion final pcollection', yield_elements='pandas', include_indexes=True))
test_pcoll | 'Output of test_pcoll' >> io.WriteToText('output/test_pcoll.txt')
它抛出这个错误
AttributeError: 'str' object has no attribute 'eq' [while running 'to pcollcetion final pcollection/[ComputedExpression[get_column_Series_4878315232], ComputedExpression[get_column_Series_4878353168], ComputedExpression[eq_Series_4878353600], ComputedExpression[__and___Series_4879336496]]:4879347568/FlatMap(evaluate)/FlatMap(evaluate)']
*注意:如果我在将 PCollection 转换为数据框之前使用另一种转换,即 beam.Select([set schema]) 来获取数据框对象,那么我可以将所有操作应用于数据框对象。 而且我能够找回 PCollection。
我认为这是我们需要在文档中阐明的内容。 proxy
参数是针对特定用例的——如果你有一个PCollection[pd.DataFrame]
(即元素已经是 DataFrames),你想将其转换为 Beam DataFrame——那么你可以指定代理来描述数据框。
在您的情况下,您有一个逐元素的 PCollection,因为ReadFromBigQuery
输出一个PCollection[dict]
(元素是字典)。 如您所见,适当的解决方案是插入一个描述数据架构的转换,例如beam.Select
,而不是指定代理。 DataFrame API 将根据输入的模式为您生成proxy
。 在apache_beam.examples.dataframe.flight_delays
中有一个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.