atreadw1492/yahoo_fin

Undocument raw parameter in get_options_chain trigger exception

Opened this issue · 0 comments

I know the parameter is undocumented, but cleaning up as what raw=False would need seems very useful, but it fails in some cases. I think there are two problems in the current code

  • the code fails if the column was numeric, i.e. there were none of the special cases present
  • the code fails when a value contains a comma

I can solve the two issues above with the following code where I also added support to transform column "Implied Volatility" that has a % format similar to "% Change"

    for cp, chain in chains.items():
        # some columns can have "-" to indicate no data
        # the code in get_options_chain normally has a parameter "raw" that would make the conversion but
        # it generates exceptions on str called on non string column, can be used as blueprint though
        #         calls["% Change"] = calls["% Change"].str.strip("%").map(force_float)
        #         calls["% Change"] = calls["% Change"].map(lambda num: num / 100 if isinstance(num, float) else 0)
        #         calls["Volume"] = calls["Volume"].str.replace("-", "0").map(force_float)
        #         calls["Open Interest"] = calls["Open Interest"].str.replace("-", "0").map(force_float)
        for col in ["% Change", "Implied Volatility"]:
            if chain[col].dtype == object:
                chain[col] = pd.to_numeric(chain[col].str.strip("%").str.replace("-","0").str.replace(",","")) / 100
        for col in ["Volume", "Open Interest"]:
            if chain[col].dtype == object:
                chain[col] = pd.to_numeric(chain[col].str.replace("-", "0").str.replace(",",""))