Skip to content

Commit

Permalink
Merge pull request #435 from ethanwhite/convert-float-nulls
Browse files Browse the repository at this point in the history
Convert numeric nulls when also converting string nulls
  • Loading branch information
henrykironde committed Mar 1, 2016
2 parents 3663e0f + fd6a0ba commit febe789
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/cleanup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
def floatable(value):
"""Check if a value can be converted to a float"""
try:
float(value)
return True
except ValueError:
return False

def correct_invalid_value(value, args):
"""This cleanup function replaces null indicators with None."""
try:
if value in [item for item in args["nulls"]]:
return None
if float(value) in [float(item) for item in args["nulls"]]:
if float(value) in [float(item) for item in args["nulls"] if floatable(item)]:
return None
return value
except:
Expand Down
6 changes: 6 additions & 0 deletions test/test_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from retriever.lib.table import Table
from retriever.lib.templates import BasicTextTemplate
from retriever.lib.tools import getmd5
from retriever.lib.cleanup import correct_invalid_value
from retriever import DATA_WRITE_PATH
from nose.tools import with_setup

Expand Down Expand Up @@ -50,7 +51,12 @@ def test_auto_get_delimiter_semicolon():
test_engine.auto_get_delimiter("a;b;c;,d")
assert test_engine.table.delimiter == ";"

def test_correct_invalid_value_string():
assert correct_invalid_value('NA', {'nulls': ['NA', '-999']}) == None

def test_correct_invalid_value_number():
assert correct_invalid_value(-999, {'nulls': ['NA', '-999']}) == None

def test_create_db_statement():
"""Test creating the create database SQL statement"""
assert test_engine.create_db_statement() == 'CREATE DATABASE test_abc'
Expand Down

0 comments on commit febe789

Please sign in to comment.