检查两个 pyspark Rows 是否相等 [英] Check if two pyspark Rows are equal

查看:36
本文介绍了检查两个 pyspark Rows 是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Spark 作业编写单元测试,其中一些输出被命名为元组:pyspark.sql.Row

I am writing unit tests for a Spark job, and some of the outputs are named tuples: pyspark.sql.Row

我如何断言他们的平等?

How can I assert their equality?

actual = get_data(df)
expected = Row(total=4, unique_ids=2)
self.assertEqual(actual, expected)

当我这样做时,这些值会以我无法确定的顺序重新排列.

When I do this, the values are rearranged in an order I can not determine.

推荐答案

您的代码应该按编写的方式工作,因为根据 文档:

Your code should work as written because according to the docs:

字段将按名称排序.

然而,另一种方法是使用 asDict() 方法的 pySpark.sql.Row 并将它们作为字典进行比较:

Nevertheless, another way is to use the asDict() method of the pySpark.sql.Row and compare them as dictionaries:

actual = get_data(df)
expected = Row(total=4, unique_ids=2)
self.assertEqual(actual.asDict(), expected.asDict())

这篇关于检查两个 pyspark Rows 是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆