Using pytest with pandas
In this section, we will show how to test your pandas code. We do this by testing the artifacts. We will use the third-party library, pytest
, to do this testing.
For this recipe, we will not be using Jupyter, but rather the command line.
How to do it…
- Create a project data layout. The
pytest
library supports projects laid out in a couple different styles. We will create a folder structure that looks like this:kag-demo-pytest/ ├── data │ └── kaggle-survey-2018.zip ├── kag.py └── test └── test_kag.py
The
kag.py
file has code to load the raw data and code to tweak it. It looks like this:import pandas as pd import zipfile def load_raw(zip_fname): with zipfile.ZipFile(zip_fname) as z: kag = pd.read_csv(z.open('multipleChoiceResponses.csv')) df = kag.iloc[1:] return...