atreadw1492 / yahoo_fin

Scrape stock price history from new (Spring 2017) Yahoo Finance layout
MIT License
285 stars 125 forks source link

Undocument raw parameter in get_options_chain trigger exception #88

Open mdelvaux opened 2 years ago

mdelvaux commented 2 years ago

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

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(",",""))