Beam Python SDK: pd.merge left join error (valueError: Attempted to encode null for non-nullable field)

Beam Python SDK: pd.merge left join error (valueError: Attempted to encode null for non-nullable field)

我正在使用 beam python SDK 使用 apache_beam.dataframe.io.read_csv 库读取两个 CSV 文件。

subscriber_data = (p | 'Read Subscriber File' >> read_csv('subscriber_data.csv'))

address_data = (p | 'Read address File' >> read_csv('address_data.csv'))

然后尝试使用以下代码pd.merge 左连接合并这些文件

subscriber_address_df = subscriber_data.merge(address_data.set_index('address_id').state,
                                                         right_index=True,
                                                         left_on='address_id',
                                                         how='left')

一旦我尝试使用以下代码打印 subscriber_address_df

to_pcollection(subscriber_address_df , include_indexes=False) | beam.Map(print)

我收到这个错误:

ValueError: Attempted to encode null for non-nullable field "state". [while running 'Unbatch 'merge_DataFrame_2137267343520'/ParDo(_UnbatchNoIndex)']

如何解决这个错误?

我的理解是,发生此错误的原因是数据框架构中非可空列状态的左联接生成的空值。

我尝试使用 to_pcollection 将数据帧切换到 Pcollection 并使用 .with_output_types 为该 pcollection 分配具有可为空列的模式,然后切换回数据帧 to_dataframe,但它没有工作

这看起来像是一个错误。我提交了 BEAM-12587。与此同时,您可以通过执行 to_pcollection(..., yield_elements='pandas') 来解决此问题,这将导致 Pandas Dataframe 对象的 PCollection,然后您可以使用自己的逻辑将其拆分为行。