简体   繁体   English

如何在单元测试中使用 Pandas 数据框

[英]How to use a pandas data frame in a unit test

I am developing a set of python scripts to pre-process a dataset then produce a series of machine learning models using scikit-learn.我正在开发一组 python 脚本来预处理数据集,然后使用 scikit-learn 生成一系列机器学习模型。 I would like to develop a set of unittests to check the data pre-processing functions, and would like to be able to use a small test pandas dataframe for which I can determine the answers for and use it in assert statements.我想开发一组单元测试来检查数据预处理功能,并希望能够使用一个小的测试熊猫数据框,我可以确定答案并在断言语句中使用它。

I cannot seem to get it to load the dataframe and to pass it to the unit tests using self.我似乎无法让它加载数据帧并将其传递给使用 self. My code looks something like this;我的代码看起来像这样;

def setUp(self):
    TEST_INPUT_DIR = 'data/'
    test_file_name =  'testdata.csv'
    try:
        data = pd.read_csv(INPUT_DIR + test_file_name,
            sep = ',',
            header = 0)
    except IOError:
        print 'cannot open file'
    self.fixture = data

def tearDown(self):
    del self.fixture

def test1(self):    
    self.assertEqual(somefunction(self.fixture), somevalue)

if __name__ == '__main__':
    unittest.main()

Thanks for the help.谢谢您的帮助。

Pandas has some utilities for testing. Pandas 有一些用于测试的实用程序。

import unittest
import pandas as pd
from pandas.util.testing import assert_frame_equal # <-- for testing dataframes

class DFTests(unittest.TestCase):

    """ class for running unittests """

    def setUp(self):
        """ Your setUp """
        TEST_INPUT_DIR = 'data/'
        test_file_name =  'testdata.csv'
        try:
            data = pd.read_csv(INPUT_DIR + test_file_name,
                sep = ',',
                header = 0)
        except IOError:
            print 'cannot open file'
        self.fixture = data

    def test_dataFrame_constructedAsExpected(self):
        """ Test that the dataframe read in equals what you expect"""
        foo = pd.DataFrame()
        assert_frame_equal(self.fixture, foo)

If you are using latest pandas, I think the following way is a bit cleaner:如果您使用的是最新的熊猫,我认为以下方式更简洁:

import pandas as pd

pd.testing.assert_frame_equal(my_df, expected_df)
pd.testing.assert_series_equal(my_series, expected_series)
pd.testing.assert_index_equal(my_index, expected_index)

Each of these functions will raise AssertionError if they are not "equal".如果它们不“相等”,这些函数中的每一个都会引发AssertionError

For more information and options: https://pandas.pydata.org/pandas-docs/stable/reference/general_utility_functions.html#testing-functions有关更多信息和选项: https : //pandas.pydata.org/pandas-docs/stable/reference/general_utility_functions.html#testing-functions

You could do something like this as well with snapshottest .你也可以用snapshottest做这样的事情。

https://stackoverflow.com/a/64070787/3384609 https://stackoverflow.com/a/64070787/3384609

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM