Skip to content

Commit

Permalink
Added a new helper method to constraint the column types of some colu…
Browse files Browse the repository at this point in the history
…mns. Also allows for replacing NaNs with some other value.
  • Loading branch information
biomadeira committed Sep 19, 2017
1 parent 92973e5 commit eac69e0
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions prointvar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,5 +679,29 @@ def get_start_end_ranges_consecutive_ints(data):
return tuple(starts), tuple(ends)


def constrain_column_types(data, dictionary, nan_value=None):
"""
Helper method that helps in constrain data types for the
various DataFrame columns.
:param data: pandas DataFrame
:param dictionary: (dict) defines common types
:param nan_value: optional new value passed to replace NaNs
:return: modified pandas DataFrame
"""

table = data
for col in table:
if col in dictionary:
try:
table[col] = table[col].astype(dictionary[col])
except (ValueError, KeyError):
# probably there are some NaNs in there
pass
if table[col].isnull().any().any() and nan_value is not None:
table[col] = table[col].fillna(nan_value, axis=1)
return table


if __name__ == '__main__':
pass

0 comments on commit eac69e0

Please sign in to comment.