Skip to content

Commit

Permalink
9999 management (#25)
Browse files Browse the repository at this point in the history
* Added more tests to ensure parse none is capturing -9999

* see that -9999 int causes issue

* Fix -9999 issue with ints

* integer test case

---------

Co-authored-by: micah johnson <micah.johnson150@gmail.com>
  • Loading branch information
micah-prime and micahjohnson150 authored Aug 15, 2024
1 parent 612bdc2 commit 3b67a3c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion snowex_db/string_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def parse_none(value):
if isinstance(value, str):
if value.lower() in ['nan', 'none', '-9999', '-9999.0'] or not value:
result = None
elif isinstance(value, float):
elif isinstance(value, float) or isinstance(value, int):
if np.isnan(value) or value == -9999:
result = None

Expand Down
8 changes: 4 additions & 4 deletions tests/data/density.csv
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Easting,743281
# Northing,4324005
# top (cm),bottom (cm),density A (kg/m3),density B (kg/m3),density C (kg/m3)
35.0,25.0,190.0,245.0,NaN
25.0,15.0,228.0,241.0,NaN
15.0,5.0,217.0,253.0,NaN
12.0,2.0,236.0,NaN,NaN
35.0,25.0,190.0,245.0,-9999
25.0,15.0,228.0,241.0,-9999
15.0,5.0,217.0,253.0,-9999
12.0,2.0,236.0,NaN,-9999
24 changes: 16 additions & 8 deletions tests/test_string_management.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pytest
from snowex_db.string_management import *

Expand Down Expand Up @@ -62,18 +63,25 @@ def test_strip_encapsulated(s, encaps, expected):
assert r == expected


def test_parse_none():
@pytest.mark.parametrize('str_value, expected', [
# Expected nones
('NaN', None),
('NONE', None),
(-9999, None), # integer case
('-9999', None),
('-9999.0', None),
(-9999.0, None),
(np.nan, None),
# Shouldn't modify anything
(10.5, 10.5),
("Comment", "Comment"),
])
def test_parse_none(str_value, expected):
"""
Test we can convert nones and nans to None and still pass through everything
else.
"""
# Assert these are converted to None
for v in ['NaN', '', 'NONE', np.nan]:
assert parse_none(v) is None

# Assert these are unaffected by function
for v in [10.5, 'Comment']:
assert parse_none(v) == v
assert parse_none(str_value) == expected


@pytest.mark.parametrize('args, kwargs, expected', [
Expand Down

0 comments on commit 3b67a3c

Please sign in to comment.