问题描述
如何对python数据帧进行单元测试?
How do i unit test python dataframes?
我有一些功能,这些功能具有作为数据帧的输入和输出.我几乎所有的功能都可以做到这一点.现在,如果我想对此进行单元测试,什么是最好的方法?为每个函数创建一个新的数据框(填充值)似乎有点费力?
I have functions that have an input and output as dataframes. Almost every function I have does this. Now if i want to unit test this what is the best method of doing it? It seems a bit of an effort to create a new dataframe (with values populated) for every function?
您有什么资料可以推荐我吗?您应该为这些功能编写单元测试吗?
Are there any materials you can refer me to? Should you write unit tests for these functions?
推荐答案
虽然Pandas的测试功能主要用于内部测试,但NumPy包含一组非常有用的测试功能,在此处记录如下: NumPy测试支持.
While Pandas's test functions are primarily used for internal testing, NumPy includes a very useful set of testing functions that are documented here: NumPy Test Support.
这些函数比较NumPy数组,但是您可以使用values
属性获得位于Pandas Data Frame基础上的数组.您可以定义一个简单的数据框,然后将函数返回的结果与期望的结果进行比较.
These functions compare NumPy arrays, but you can get array that underlie a Pandas Data Frame using the values
property. You can define a simple Data Frame and compare what your function returns to what you expect.
您可以使用的一种技术是为多种功能定义一组测试数据.这样,您可以使用 Pytest灯具一次定义该数据框,然后使用进行多次测试.
One technique you can use is to define one set of test data for a number of functions. That way, you can use Pytest Fixtures to define that Data Frame once, and use it in multiple tests.
在资源方面,我在测试中找到了这篇文章NumPy和Pandas 非常有用.我还于今年在PyCon加拿大做了有关数据分析测试的简短介绍:自动化数据分析测试.
In terms of resources, I found this article on Testing with NumPy and Pandas to be very useful. I also did a short presentation about data analysis testing at PyCon Canada this year: Automate Your Data Analysis Testing.
这篇关于您如何对Python DataFrames进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!