This question already has answers here:
"TypeError: string indices must be integers" when getting data of a stock from Yahoo Finance using Pandas Datareader
(5 answers)
Closed 2 months ago.
One week ago, I ran the following code and did not get an error.
import datetime as dt
import pandas_datareader.yahoo.daily as yd
df1 = yd.YahooDailyReader("SPY", interval='d', start=dt.date(2022,7,1),end=dt.date.today()).read()
However, when I tried the same code today, I got the following error message:
Does anyone know how to solve this problem?
It seems yahoo finance has changed it API or the service is down.
You can use Tiingo API instead (you need to make an account to get an API token)
import pandas_datareader as web
r = web.get_data_tiingo("SPY", api_key=ENV('NEWS_TOKEN'))
see: Remote Data Access#Tiingo
I have the same problem, yfinance is still working
import yfinance as yf
from yahoofinancials import YahooFinancials
my_data = yf.download('TSLA', start='2021-12-17', end='2022-12-17', progress=False)
Related
I am trying to access my table in SQL database. However, I am getting an unusual error. Can someone please help me I am very new at this.
import sqlite3
import pandas as pd
com = sqlite3.connect('Reporting.db')
Note: Panda dataframe is already defined above that's why I am not including this over here.
df.to_sql('tblReporting', com, index=False, if_exists='replace')
print('tblReporting loaded \n')```
%load_ext sql
%sql sqlite:///Reporting.db
%%sql
SELECT *
FROM tblReporting
This is the error I am getting
SELECT *
^
SyntaxError: invalid syntax
Note #2: I am using Anaconda Navigator for writing scripts
Solved it!! that's my Syntax
import sqlite3
import pandas as pd
com = sqlite3.connect('Reporting.db')
df.to_sql('tblReporting', com, index=False, if_exists='replace')
print('tblReporting loaded \n')
org_query = '''SELECT * FROM tblReporting'''
df = pd.read_sql_query(org_query, com)
df.head()
Note: added ''' before and after my org_query helped me resolved this
I am trying to learn ML models for predicting stock prices and initially, I tried using DataReader
import pandas_datareader as web
df = web.DataReader('AAPL', data_source='yahoo', start='2016-01-01', end='2021-08-01')
But I get a RemoteDataError and kept hitting a dead end trying to figure it out so I tried using tiingo
https://tiingo-python.readthedocs.io/en/latest/readme.html
I read through the documentation and tried passing a dictionary with 'api_key' as a key
into my tiingo client, ie.
from tiingo import TiingoClient
client = TiingoClient()
config = {}
config['session'] = True
config['api_key'] = 'my_api_key'
client = TiingoClient(config)
The documentation says I can now use TiingoClient to make API calls, however,
RuntimeError: Tiingo API Key not provided. Please provide via environment variable or config argument.
It is quite challenging learning the ML models and its syntax but what compounds the difficulty for me is what some data-scientests consider to be trivial as they don't typically deal with gathering or scraping data. Maybe my question is trivial but I've spent about an hour trying to figure out how to import data properly for stock prices and the only method that worked for me so far is
df = web.get_data_yahoo('stock symbol')
but I would like to grasp the other ways of importing stock prices via Tiingo and DataReader so if anyone can provide explanations/tips/suggestions I'd greatly appreciate it.
EDIT: for my tiingo account I did not buy any subscription plan for using their data as I was under the impression I can access data for free with my api-key
This is what I use, but its identical to what you are using it seems.
config = {}
config['session'] = True
config['api_key'] = "key here"
client = TiingoClient(config)
Remove this line: TiingoClient()
I am having trouble using the google bigquery package in pandas. I have installed the google-api-python-client as well as the pandas-gbq packages. But for some reason when I go to query a table I get a DistributionNotFound: The 'google-api-python-client' distribution was not found and is required by the application error. Here is a snippet of my code:
import pandas as pd
from pandas.io import gbq
count_block = gbq.read_gbq('SELECT count(int64_field_0) as count_blocks FROM Data.bh', projectid)
Using a virtual environment in this scenario can allow you to rule out problems with your library installations
I would like to use public data from bigquery on datalab, and then into a pandas dataframe. How would I go about doing that. I have tried 3 different versions:
from google.cloud import bigquery
client = bigquery.Client()
QUERY = (
'SELECT pickup_datetime, dropoff_datetime FROM `bigquery-public-
data.new_york.tlc_yellow_trips_20*`') --also tried without the ` and wildcard
query = client.run_sync_query('%s LIMIT 100' % QUERY)
query.timeout_ms = 10000
query.run()
Error: BadRequest
import pandas as pd
df=pd.io.gbq.read_gbq("""
SELECT pickup_datetime, dropoff_datetime
FROM bigquery-public-data.new_york.tlc_yellow_trips_20*
LIMIT 10
""", project_id='bigquery-public-data')
Error: I am asked to give access to pandas, but when I agree, I get This site can’t be reached localhost refused to connect.
%%bq query
SELECT pickup_datetime, dropoff_datetime
FROM bigquery-public-data.new_york.tlc_yellow_trips_20*
LIMIT 10
Error: Just keeps Running
Any help on what I am doing wrong would be appreciated.
The codes above should work after some minor changes and after you granted google access to your local machine with your email using gcloud, install and initialize.
Get the project ID by typing bq after you initialized gcloud with gcloud init.
In my first code above use client = bigquery.Client(project_id='your project id')
Since you granted access, the second code should work as well, just update your project ID. If you dont use the limit function, then this may take a long time to load since pandas will transform the data to a dataframe.
And the third code will work as well.
WARNING: NEWBIE
i had put off upgrading from pandas 0.18 to 0.19 until this morning. this code used to just give a deprication warning:
import pandas.io.data as web
x = web.DataReader('GE','yahoo',(2016, 10, 1), (2016, 11, 1))
now it throws an error and tells me: "The pandas.io.data module is moved to a separate package " builtins.ImportError: The pandas.io.data module is moved to a separate package (pandas-datareader). After installing the pandas-datareader package (https://github.com/pandas-dev/pandas-datareader), you can change the import from pandas.io import data, wb to from pandas_datareader import data, wb."
so, i rewrite my "from...import..." line to:
from pandas_datareader import data, wb
as expected, when i run the code, it throws an error:
builtins.NameError: name 'web' is not defined
when i try this code:
x = wb.pandas-datareader('GE','yahoo',(2016, 10, 1), (2016, 11, 1))
this error is thrown:
builtins.AttributeError: module 'pandas_datareader.wb' has no attribute 'pandas'
when i try this code:
x = wb.Datareader('GE','yahoo',(2016, 10, 1), (2016, 11, 1))
this error is thrown:
builtins.AttributeError: module 'pandas_datareader.wb' has no attribute 'DataReader'
can anyone please tell me how to call the datareader now?
thanks in advance
The old method of using the data reader in pandas should not be used as the first error you encountered. So never use import pandas.io.data as web
The correct way to access the modules of the new package pandas_datareader is what you wrote.
from pandas_datareader import data, wb
data and wb are modules (Python files) with many different functions that you can call to bring in different types of external data into your program. To see all the functions of each module use the dir command.
You can see all the publicly available objects with:
[attribute for attribute in dir(data) if attribute[0] != '_']
Which outputs
['DataReader',
'EurostatReader',
'FamaFrenchReader',
'FredReader',
'GoogleDailyReader',
'OECDReader',
'Options',
'YahooActionReader',
'YahooDailyReader',
'YahooOptions',
'YahooQuotesReader',
'get_components_yahoo',
'get_data_famafrench',
'get_data_fred',
'get_data_google',
'get_data_yahoo',
'get_data_yahoo_actions',
'get_quote_google',
'get_quote_yahoo',
'warnings']
So, these are all the items that you can use after the . in the data module.
If you run the same dir command with the wb module you will see that DataReader does not exist in that module. It exists in the above list in the data module.
Finally, make sure you have spelled your function correctly DataReader has upper case R. Use tab completion to avoid these mistakes or you will get the no attribute error. If the function you want does not get outputted with the dir command then you are using the wrong module.
DataReader also accepts strings as dates so the following will get you what you want.
data.DataReader('GE','yahoo', '2016-10-1', '2016-11-1')