MCPcopy Index your code
hub / github.com/robertmartin8/MachineLearningStocks / data_string_to_float

Function data_string_to_float

utils.py:1–24  ·  view source on GitHub ↗

The result of our regex search is a number stored as a string, but we need a float. - Some of these strings say things like '25M' instead of 25000000. - Some have 'N/A' in them. - Some are negative (have '-' in front of the numbers). - As an artifact of our regex

(number_string)

Source from the content-addressed store, hash-verified

1def data_string_to_float(number_string):
2 """
3 The result of our regex search is a number stored as a string, but we need a float.
4 - Some of these strings say things like '25M' instead of 25000000.
5 - Some have 'N/A' in them.
6 - Some are negative (have '-' in front of the numbers).
7 - As an artifact of our regex, some values which were meant to be zero are instead '>0'.
8 We must process all of these cases accordingly.
9 :param number_string: the string output of our regex, which needs to be converted to a float.
10 :return: a float representation of the string, taking into account minus sign, unit, etc.
11 """
12 # Deal with zeroes and the sign
13 if ("N/A" in number_string) or ("NaN" in number_string):
14 return "N/A"
15 elif number_string == ">0":
16 return 0
17 elif "B" in number_string:
18 return float(number_string.replace("B", "")) * 1000000000
19 elif "M" in number_string:
20 return float(number_string.replace("M", "")) * 1000000
21 elif "K" in number_string:
22 return float(number_string.replace("K", "")) * 1000
23 else:
24 return float(number_string)
25
26
27def duplicate_error_check(df):

Callers 2

parse_keystatsFunction · 0.90
forwardFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected