Skip to content

Commit 7449878

Browse files
George RobertsonGeorge Robertson
authored andcommitted
Fixed null_checker tests to find value errors and key errors
1 parent 5e485c3 commit 7449878

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

codonPython/check_null.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import numpy
22
import pandas as pd
33

4-
def check_null(dataframe: pd.DataFrame, columns_to_be_checked: tuple) -> bool:
4+
def check_null(dataframe: pd.DataFrame, columns_to_be_checked: list) -> bool:
55
"""
66
Checks a pandas dataframe for null values
77
@@ -11,7 +11,7 @@ def check_null(dataframe: pd.DataFrame, columns_to_be_checked: tuple) -> bool:
1111
----------
1212
data : pandas.DataFrame
1313
Dataframe to read
14-
columns_to_be_checked: tuple
14+
columns_to_be_checked: list
1515
Given dataframe columns to be checked for null values
1616
1717
Returns
@@ -21,21 +21,22 @@ def check_null(dataframe: pd.DataFrame, columns_to_be_checked: tuple) -> bool:
2121
2222
Examples
2323
--------
24-
>>> check_null(dataframe = pd.DataFrame({'col1': [1,2], 'col2': [3,4]}), columns_to_be_checked = ('col1'))
24+
>>> check_null(dataframe = pd.DataFrame({'col1': [1,2], 'col2': [3,4]}),columns_to_be_checked = ['col1', 'col2'])
2525
0
26-
>>> check_null(dataframe = pd.DataFrame({'col1': [1,numpy.nan], 'col2': [3,4]}), columns_to_be_checked = ('col1'))
26+
>>> check_null(dataframe = pd.DataFrame({'col1': [1,numpy.nan], 'col2': [3,4]}),columns_to_be_checked = ['col1'])
2727
1
2828
"""
2929

30-
for eachVal in columns_to_be_checked:
31-
if type(eachVal) != str:
32-
raise ValueError("Please input string values for column names.")
33-
if columns_to_be_checked not in dataframe.columns:
34-
raise KeyError("Please check the column names correspond to values in the DataFrame.")
30+
if not isinstance(columns_to_be_checked, list):
31+
raise ValueError("Please make sure that all your columns passed are strings")
32+
33+
for eachCol in columns_to_be_checked:
34+
if eachCol not in dataframe.columns:
35+
raise KeyError("Please check the column names correspond to values in the DataFrame.")
3536

3637
null_count = 0
3738
for eachColumn in columns_to_be_checked:
3839
prev_null_count = null_count
3940
null_count = prev_null_count + (len(dataframe) - dataframe[eachColumn].count())
40-
41+
4142
return null_count

codonPython/tests/check_consistent_measures_test.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from codonPython.check_consistent_measures import check_consistent_measures
22
import pandas as pd
3+
import numpy as np
34
import pytest
45

56
@pytest.mark.parametrize("data, geography_col, measure_col, measures_set, expected", [
@@ -30,34 +31,28 @@
3031
def test_each_org_levels_BAU(data, geography_col, measure_col, measures_set, expected):
3132
assert expected == check_consistent_measures(data, geography_col, measure_col, measures_set)
3233

33-
@pytest.mark.parametrize("data, geography_col, measure_col, measures_set", [
34+
35+
@pytest.mark.parametrize("data, geography_col, measure_col, measures_set", [
3436
(
3537
pd.DataFrame({
36-
"Geog" : ["National" ,"National", 1, "Region", "Local", "Local"],
37-
"measure" : ["m1", "m2", "m1", "m2", "m1", "m2"],
38+
"Geog" : ["National" ,"National", "Region", "Region", "Local", "Local"],
39+
"measure" : ["m1", "m2", "m1", np.nan, "m1", "m2"],
3840
"Value_Unsuppressed" : [4, 2, 2, 1, 2, 1],
3941
}),
4042
"Geog",
4143
"measure",
4244
set({"m1", "m2"}),
43-
)
44-
])
45-
46-
def test_each_org_levels_valueErrors(data, geography_col, measure_col, measures_set):
47-
with pytest.raises(ValueError):
48-
check_consistent_measures(data, geography_col, measure_col, measures_set)
49-
50-
@pytest.mark.parametrize("data, geography_col, measure_col, measures_set", [
45+
),
5146
(
5247
pd.DataFrame({
53-
"Geog" : ["National" ,"National", "Region", "Region", "Local", "Local"],
54-
"measure" : ["m1", "m2", "m1", True, "m1", "m2"],
48+
"Geog" : ["National" ,"National", 1, "Region", "Local", "Local"],
49+
"measure" : ["m1", "m2", "m1", "m2", "m1", "m2"],
5550
"Value_Unsuppressed" : [4, 2, 2, 1, 2, 1],
5651
}),
5752
"Geog",
5853
"measure",
5954
set({"m1", "m2"}),
60-
)
55+
)
6156
])
6257

6358

0 commit comments

Comments
 (0)