Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9999 management #25

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading