Pandas Options Broken Again? - python

Running this canonical example:
from pandas.io.data import Options
aapl = Options('aapl', 'yahoo')
data = aapl.get_all_data()
gives
RemoteDataError: Data not available
I know this used to be an issue in v0.14 or so, but it was supposedly fixed with v0.15. I'm on 0.18.1

It's Yahoo - they've changed their options site, so old parsers aren't working properly
Suggestion: first of all consider using pandas_datareader instead of deprecated pandas.io.data and monitor this issue so you'll know when this issue is fixed

Related

On running this python code in GOOGLE COLAB, it showing me error. Can anyone please tell what am i doing wrong and share a corrected code?

import pandas_datareader.data as web
import numpy as np
import pandas as pd
stock = ['AAPL']
data = web.DataReader(stock,data_source="yahoo",start='01/01/2010')['Adj Close']
data.sort_index(inplace=True)
returns = data.pct_change()
mean_return = returns.mean()
return_stdev = returns.std()
annualised_return = round(mean_return * 252,2)
annualised_stdev = round(return_stdev * np.sqrt(252),2)
print ('The annualised mean return of stock {} is {}, '
'and the annualised volatility is {}').format(stock[0],annualised_return,annualised_stdev)
Release notes for pandas_datareader 0.10.0 does mention that there were issues with Yahoo API's requiring headers and were fixed in this release (0.10.0).
Fixed Yahoo readers which now require headers
So, if your google-colab uses any version older than 0.10.0 will have problems using Yahoo API's.
Here are some steps on how to debug the issue in google-colab - jupyter notebook.
Step 1: Determine the installed version of module pandas_datareader.
!pip show pandas_datareader
Step 2: if the version is lower than 0.10.0 then upgrade the version.
!pip install --upgrade pandas_datareader
Step 3: Don't forget to restart the runtime to load the new libraries.
Press Runtime->Restart runtime
Step 4: Now try running the step 1 again to determine if the newest version is installed.
!pip show pandas_datareader
I hope the newest version will be installed and you can run your above code with the correction mentioned by #Aaron in his answer.
Note: ! is required before shell commands. Try going thorugh the official docs of IPython for more info.
You're calling format on the output of the print function by putting it after the close parenthesis. print doesn't return anything, so you're effectively calling None.format(...) (which doesn't exist). You should instead call format on the string directly as such:
print('The annualised mean return of stock {} is {}, and the annualised volatility is {}'.format(stock[0],annualised_return,annualised_stdev))
Here is the corrected code:
import pandas_datareader.data as web
import numpy as np
import pandas as pd
stock = ['AAPL']
data = web.DataReader(stock,data_source="yahoo",start='01/01/2010')['Adj Close']
data.sort_index(inplace=True)
returns = data.pct_change()
mean_return = returns.mean()
return_stdev = returns.std()
annualised_return = round(mean_return * 252,2)
annualised_stdev = round(return_stdev * np.sqrt(252),2)
print(f"The annualised mean return of stock {stock[0]} is {annualised_return}, and the annualised volatility is {annualised_stdev}")
So the error came because you did not use a f string. An f string puts data in-between the brackets{}
For more info visit:
https://www.geeksforgeeks.org/python-output-formatting/#:~:text=In%20Python%2C%20there%20is%20no%20printf%20%28%29%20function,string%20modulo%20%28or%20sometimes%20even%20called%20modulus%29%20operator.

Yahoo Pandas datareader date difference

I am trying to import stock data using Yahoo API as a source. I have tried it many times and I always get the same error, the start and end date are different from what I have passed. For example I pass the start and end date as '2015-1-1' & '2017-1-1' but the stock data I get start and ends at '2014-12-31' & '2016-12-30'. I dont know what I am doing wrong. I even tried using google but got a error as "data_sorce='google' not implemented."
Is there some other free data source I can use or correct the dates while using Yahoo? Jupyter notebook
You can import as yfinance and just enter a start and end when importing the data. Yahoo decommissioned their historical data API check out the Ran Aroussi the developer's of fix-yahoo-finance which is now yfinance blog where he details everything nicely https://aroussi.com/post/python-yahoo-finance
To install/upgrade yfinance using pip, run:
$ pip install yfinance
instead of this method
facebook = web.DateReader("FB", "yahoo", start, end)
can be in a format like this instead
override method from pandas_datareader by importing data as pdr
import yfinance as yf
yf.pdr_override() # <== the override :-)
# download dataframe using pandas_datareader
facebook = pdr.get_data_yahoo("FB", start="2015-1-1", end="2017-1-1")
or you can just use yfinance instead
import yfinance as yf
facebook = yf.download("FB", start="2015-1-1", end="2017-1-1")

scikit-surprise: python cannot find module even though pip lists it as installed

I am trying to use the scikit-surprise module to build a recommender system however I am having an error in getting it to compile.
I am receiving the ImportError: Cannot import name "Reader" error
My class is as follows
import pandas as pd
from surprise import Reader, Dataset
userReviewsFilePath ="UserReviewsFirst5000WithHeadings.csv"
ratings = pd.read_csv(userReviewsFilePath) # reading data in pandas df
ratings_dict = {'recipeID': list(ratings.recipeID),
'rating': list(ratings.rating),
'userID': list(ratings.userID)}
df = pd.DataFrame(ratings_dict)
reader = Reader(rating_scale=(1, 5))
data = Dataset.load_from_df(df[['recipeID', 'rating', 'userID']], reader)
pip show says that version 1.0.6 is installed
I think your problem come from the installation... I installed "surprise" and past your code and it worked:
import pandas as pd
from surprise import Reader, Dataset
print(Reader) # or just print(surprise) if you import surprise
out:
<class 'surprise.reader.Reader'>
Start by re-install surprise and tell us.
If you have more than one version of python, do:
which pip
to see if you installed surprise on the used version of python
I think it's in surprise.reader: https://surprise.readthedocs.io/en/stable/reader.html
Your code should read:
from surprise.reader import Reader
from surprise.dataset import Dataset
Edit: I checked the instructions again which seem to contradict this, and give your original code as the correct example. https://surprise.readthedocs.io/en/stable/getting_started.html#getting-started
So maybe they add their own shortcuts? Either way, it seems like this isn't the correct solution, sorry. (Unless it works, in which case their instructions might be out of date.)
Edit 2: They do alias it, so "from surprise import Reader" should indeed have worked: https://github.com/NicolasHug/Surprise/blob/master/surprise/init.py#L19
I think you need to do
from surprise.reader import Reader

How to pull Call/Put Option prices with pandas-datareader in Python?

I have the following code. I tried to pull the data from yahoo and google, both don't work. It's throwing the below message
from pandas_datareader.data import Options
fb_options = Options('TSLA', 'yahoo')
options_df = fb_options.get_options_data(expiry=fb_options.expiry_dates[0])
print(options_df.tail())
Error Message: Yahoo Options has been immediately deprecated due to large breaks in the API without the
introduction of a stable replacement. Pull Requests to re-enable these data
connectors are welcome.
Is there any other way to retrieve the options prices?
The following is working for me right now
import yfinance as yf # https://github.com/ranaroussi/yfinance
aapl= yf.Ticker("AAPL")
# aapl.options # list of dates
DF_calls, DF_puts = aapl.option_chain(aapl.options[0]) # returns 2 DataFrames
Alternative method
import pandas_datareader as pdr
aapl = Options('aapl')
calls = aapl.get_call_data()
Partial output:
Last Bid Ask Chg Vol Open_Int IV Underlying_Price Last_Trade_Date
200.0 2020-07-02 call AAPL200702C00200000 164.50 151.60 156.20 0.000000 13.0 13.0 1.962891 353.63 2020-06-23 13:30:03
Link to my project code
Yahoo ended support for their options API, and as such, the Yahoo options reader and get_options_data were deprecated in pandas_datareader 0.7.0 (marked for removal). Unfortunately, there are no other readers in pandas_datareader which provide options prices.
There are (to my knowledge) no free-to-use APIs for options data, other than TD Ameritrade (see this endpoint), though you must be a TD Ameritrade account holder to obtain access to their developer API (link).

Problems with Pandas DataReader and Yahoo

I was trying to get stock information as follows:
from pandas.io.data import DataReader
import datetime
data = DataReader("F", "yahoo", datetime.datetime(1990, 1, 1),datetime.datetime(2002, 1, 1))
which fails with
IOError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.finance.yahoo.com/table.csv?s=C001.F&a=0&b=1&c=2014&d=11&e=1&f=2017&g=d&ignore=.csv'
Up to now, I could not find a fix for this issue or a suitable work-around. Do you guys have any suggestions?
It seems 'yahoo'is no longer supported. Try "morningstar" or "google".
The simple yahoo financial link,that worked for years, is no longer supported.
I've heard of a work around that involves browser spoofing (wget from command line) requires browser aliasing to obtain time sensitive cookies that are then required for each request -- but I've never tried it myself since "morningstar" currently still works (but I miss yahoo's adjusted close).
#(Pascal 3.6)
import pandas as pd
import pandas_datareader.data as web
...
df = web.DataReader('MSFT','morningstar')
for idx, row in df.iterrows():
print(idx[1],row[0],row[1],row[2],row[3],row[4])

Categories