regex - Python: Error in re.compile() function for double digits -
this regex needs find "1x"
or "x1"
must able find double digits such "10x"
, "x11"
.
leverage_match = re.compile(r"\d+x|x\d+", flags=re.ignorecase)
according regex101.com above regex should sufficient capture entire numbers for:
import pandas pd import re df = pd.dataframe(["bull estox 11x s", "bull estox x12 s"], columns=["name"]) name "bull estox 11x s" "bull estox x12 s"
however, code below returns 1 digit, e.g. "11x"
becomes "1x"
.
leverage_match = re.compile(r"\d+x|x\d+", flags=re.ignorecase) #<- same seen above def f(value): f2 = lambda x: leverage_match.findall(x)[0] if len(leverage_match.findall(x)) > 0 else "" leverage = f2(value) if leverage != "": return "{}".format(leverage) if leverage[0].replace("x","x") == "x": leverage = leverage[1]+leverage[0].replace('x','x') df["description"] = df["name"].map(lambda x:f(x))
----------
update: complete code, assure did not leave essential out:
import pandas pd import re df = pd.dataframe(["bull estox 11x s", "bull estox x12 s"], columns=["name"]) description_map = {"estox":"euro stoxx 50"} underlying_match = re.compile(r"\s(\s+)\s") leverage_match = re.compile(r"\d+x|x\d+", flags=re.ignorecase) def f(value): f1 = lambda x: description_map[underlying_match.findall(x)[0]] if underlying_match.findall(x)[0] in description_map else "" f2 = lambda x: leverage_match.findall(x)[0] if len(leverage_match.findall(x)) > 0 else "" f3 = lambda x: "-" if "bear" in x else "-" if "short" in x else "" underlying = f1(value) leverage = f2(value) sign = f3(value) statement = "tracks " + underlying if underlying == "": if sign == "-" , leverage == "": return statement + "{}".format("inversely.") if sign == "-" , leverage != "": return statement + "{} {}{} leverage.".format("inversely", sign, leverage) if sign == "" , leverage != "": return statement + "with {}{} leverage.".format(sign, leverage) else: return "tracks" if leverage[0].replace("x","x") == "x": leverage = leverage[1]+leverage[0].replace('x','x') if leverage != "" , sign == "-": statement += " {} {}{} leverage.".format("inversely", sign, leverage) elif leverage != "" , sign == "": statement += " {} leverage.".format(leverage) else: if sign == "-": statement += " {} ".format("inversely") return statement df["description"] = df["name"].map(lambda x:f(x)) print df
i think gave wrong example, following df
df = pd.dataframe(["bull axp 11x s", "bull axp x11 s"], columns=["name"])
the output follows
name description 0 bull axp 11x s tracks american express 11x leverage. 1 bull axp x11 s tracks american express 1x leverage.
and x11
becomes 1x
because there error in logic of our code in following section:
if leverage[0].replace("x","x") == "x": leverage = leverage[1]+leverage[0].replace('x','x')
it must instead should have been follows:(update)
if leverage[0].replace("x","x") == "x": leverage = ''.join(leverage[1:])+leverage[0].replace('x','x')
if fix , output expected , follows:
name description 0 bull axp 11x s tracks american express 11x leverage. 1 bull axp x11 s tracks american express 11x leverage.
complete code
import pandas pd import re df = pd.dataframe(["bull estox 11x s", "bull estox x12 s"], columns=["name"]) description_map = {"estox":"euro stoxx 50"} underlying_match = re.compile(r"\s(\s+)\s") leverage_match = re.compile(r"\d+x|x\d+", flags=re.ignorecase) def f(value): f1 = lambda x: description_map[underlying_match.findall(x)[0]] if underlying_match.findall(x)[0] in description_map else "" f2 = lambda x: leverage_match.findall(x)[0] if len(leverage_match.findall(x)) > 0 else "" f3 = lambda x: "-" if "bear" in x else "-" if "short" in x else "" underlying = f1(value) leverage = f2(value) sign = f3(value) statement = "tracks " + underlying if underlying == "": if sign == "-" , leverage == "": return statement + "{}".format("inversely.") if sign == "-" , leverage != "": return statement + "{} {}{} leverage.".format("inversely", sign, leverage) if sign == "" , leverage != "": return statement + "with {}{} leverage.".format(sign, leverage) else: return "tracks" if leverage[0].replace("x","x") == "x": leverage = ''.join(leverage[1:])+leverage[0].replace('x','x') if leverage != "" , sign == "-": statement += " {} {}{} leverage.".format("inversely", sign, leverage) elif leverage != "" , sign == "": statement += " {} leverage.".format(leverage) else: if sign == "-": statement += " {} ".format("inversely") return statement df["description"] = df["name"].map(lambda x:f(x)) print df
Comments
Post a Comment