Skip to content

Commit

Permalink
Merge branch 'sdh-filter-na-fix' into 'main'
Browse files Browse the repository at this point in the history
Handle NAs in filtering approved data function

Closes #91

See merge request water/computational-tools/surface-water-work/hyswap!75
  • Loading branch information
Scott Hamshaw committed May 15, 2024
2 parents 4bf74bb + c17146a commit 94bd738
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 5 additions & 5 deletions hyswap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ def filter_approved_data(data, filter_column=None):
.. doctest::
>>> df = pd.DataFrame({
... 'data': [1, 2, 3, 4],
... 'approved': ['A', 'A', 'P', 'P']})
... 'data': [1, 2, 3, 4, 5],
... 'approved': ['A', 'A, e', 'A', 'P', 'P']})
>>> df.shape
(4, 2)
(5, 2)
Then filter the data to only return approved data.
.. doctest::
>>> df = utils.filter_approved_data(df, filter_column='approved')
>>> df.shape
(2, 2)
(3, 2)
"""
if filter_column is None:
raise ValueError("filter_column must be specified.")
return data[data[filter_column].str.contains("A")]
return data[data[filter_column].str.contains("A", na=False)]


def rolling_average(df, data_column_name, data_type,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
class TestDataFilters:
def test_filter_approved_data(self):
"""Test the filter_approved_data function."""
data = {"a": ["A", "A, e", "A, R", "A, [4]", "P", "P"],
"b": [1, 2, 3, 4, 5, 6]}
data = {"a": ["A", "A, e", "A, R", np.nan, "A, [4]", "P", "P"],
"b": [1, 2, 3, np.nan, 5, 6, 7]}
data_df = pd.DataFrame(data)
df = utils.filter_approved_data(data_df, filter_column="a")
assert df["b"].tolist() == [1, 2, 3, 4]
assert df["b"].tolist() == [1, 2, 3, 5]

def test_filter_approved_data_error(self):
"""Test the filter_approved_data function."""
Expand Down

0 comments on commit 94bd738

Please sign in to comment.