Last active
September 23, 2021 00:18
-
-
Save andrewdoss-bit/ca937a983244fa5f646e0467e2192a7b to your computer and use it in GitHub Desktop.
Unit testing a FIPS code cleaning function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import pytest | |
def fips_cleaner(code): | |
"""Standardizes county FIPS codes as 5-digit strings.""" | |
return code.astype(str).str.extract('(^[^/.]*).*', expand=False).str.zfill(5) | |
@pytest.mark.parametrize( | |
"input, expected", | |
[ | |
('01001', '01001'), # 5-character string in, 5-character string out | |
('1001', '01001'), # 4-character string in, 5-character string out | |
(1001, '01001'), # int in, 5-character string out | |
(1001.0, '01001'), # float in, 5-character string out | |
('11001', '11001'), # Similar to before, but with two digit state code | |
(11001, '11001'), # Similar to before, but with two digit state code | |
(11001.0, '11001'), # Similar to before, but with two digit state code | |
] | |
) | |
def test_fips_cleaner(input, expected): | |
input_series = pd.Series([input]) # Setup | |
cleaned_value = fips_cleaner(input_series).iloc[0] # Compute | |
assert cleaned_value == expected # Assert |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment