import numpy as np
import pandas as pd
import pandas_datareader.data as web
from pandas_datareader import data, wb
goog = web.DataReader('GOOG', data_source='google', start='3/14/2017',end='9/14/2017')
print goog.head()
Why does it shows invalid syntax for goog?
Thank you
Ok I found out because in Python 3 they replace it with a function so the correct type is:
print (goog.head())
Related
I'm trying to define a function to allow me to extract information on stocks over the past 12 months and export it to a CSV file. I'm not sure where it's going wrong as it prints 'bad'. Any thoughts?
Thanks.
import pandas as py
import numpy as np
import yfinance as yf
import datetime as dt
from pandas_datareader import data as pdr
from yahoofinancials import YahooFinancials
yf.pdr_override()
now_time=dt.datetime.now()
start_time = dt.datetime(now_time.year - 1, now_time.month , now_time.day)
bad_names=[]
def download_stock(stock):
try:
print(stock)
stock_df = pdr.get_yahoo_data(stock, start_time, now_time)
stock_df['Name'] = stock
output_name = stock + '_data.csv'
stock_df.to_csv("./stocks/"+output_name)
except:
bad_names.append(stock)
print('bad: %s' % (stock))
download_stock('AAPL')
A try - except block will handle any exception and simply execute what follows after except.
You could try running the code without the try-except block and see what the error is.
Alternatively, you could use
except Exception as e:
print(e)
So you can know what is going wrong exactly. Looking at it now, I would guess that you are missing one dot in the filepath "../stocks/"+output_name
Can you please help me with this code. Error is posted below the code.
import pandas as pd
import pandas_datareader.data as web
import numpy as np
FB = web.YahooOptions('FB')
for exp in FB.expiry_dates:
print(exp.isoformat())
error:
RemoteDataError: Unable to read URL: https://query1.finance.yahoo.com/v7/finance/options/FB
Response Text:
b'Forbidden'
from module release page:
v0.6.0 (January 24, 2018)
Highlights include:
Immediate deprecation of Yahoo!, Google Options and Quotes and EDGAR. The end points behind these APIs have radically changed and the
existing readers require complete rewrites. In the case of most Yahoo!
data the endpoints have been removed. PDR would like to restore these
features, and pull requests are welcome.
so I won't expect that works
to do what you want directly using pandas:
import pandas as pd
from datetime import datetime
df = pd.read_json('https://query1.finance.yahoo.com/v7/finance/options/FB')
for date in df['optionChain'][1][0]['expirationDates']:
print(datetime.fromtimestamp(date).isoformat())
output:
>>>
2022-01-13T20:00:00
2022-01-20T20:00:00
2022-01-27T20:00:00
2022-02-03T20:00:00
2022-02-10T20:00:00
2022-02-17T20:00:00
2022-02-24T20:00:00
2022-03-03T20:00:00
2022-03-17T21:00:00
2022-04-13T21:00:00
2022-05-19T21:00:00
2022-06-16T21:00:00
2022-07-14T21:00:00
2022-09-15T21:00:00
2023-01-19T20:00:00
2023-03-16T21:00:00
2023-06-15T21:00:00
2024-01-18T20:00:00
2026-02-20T20:00:00
Process finished with exit code 0
Code:
import pandas as pd
import quandl
quandl.ApiConfig.api_key = 'wsnt2aKcnkNMJjdqqwTz'
pd = quandl.get('BATS / BATS_GOOGL')
print(df.head())
Error:
NotFoundError: (Status 400) (Quandl Error QECx01) We could not recognize the URL you requested: /api/v3/datasets/BATS / BATS_GOOGL/data. Please check your URL and try again.
You should not use spaces in the path; also, you should not use pd as a variable name (remember, you have imported pandas as pd), plus that, as is now, you are asking for the head() of a dataframe df that is nowhere defined.
Try with
df = quandl.get('BATS/BATS_GOOGL')
ValueError: The Quandl API key must be provided either through the api_key variable or through the environmental variable QUANDL_API_KEY.
I am trying to retrieve some simple stock data from Quandl. I have put in the actual API key instead of the x in the below example code below but I am still getting errors. Am I missing out on something?
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
style.use('ggplot')
symbol = 'AAPL'
api_key = 'x'
start = dt.datetime(2015, 1, 1)
end = dt.datetime.now()
df = web.DataReader(symbol, 'quandl', start, end, api_key)
print(df.head())
From the quandl docs:
AUTHENTICATION The Quandl Python module is free but you must have a
Quandl API key in order to download data. To get your own API key, you
will need to create a free Quandl account and set your API key.
After importing the Quandl module, you can set your API key with the
following command: quandl.ApiConfig.api_key = "YOURAPIKEY"
So you will need to pip install and import quandl. Then you can set the api_key attribute as above.
If you only want to get the data from Quandl, maybe you can try another approach.
import pandas as pd
import Quandl
api_key = 'yoursuperamazingquandlAPIkey'
df = Quandl.get('heregoesthequandlcode', authtoken = api_key)
print(df.head())
I am trying to query from a database and I've tried to lookup the right way to format an SoQL string but I am failing. I try the following:
from __future__ import division, print_function
from sodapy import Socrata
import pandas as pd
import numpy as np
client = Socrata("data.cityofchicago.org", None)
df = client.get("kkgn-a2j4", query="WHERE traffic > -1")
and receive an error that it Could not parse SoQL query "WHERE traffic > -1" at line 1 character 1. If I do the following, however, it works:
from __future__ import division, print_function
from sodapy import Socrata
import pandas as pd
import numpy as np
client = Socrata("data.cityofchicago.org", None)
df = client.get("kkgn-a2j4", where="traffic > -1")
But I want to know how to get the query argument to work so I can use more complex queries. Specifically, I want to try to query when traffic > -1 and BETWEEN '2013-01-19T23:50:32.000' AND '2014-12-14T23:50:32.000'.
You can use the sodapy where parameter ($where in SoQl) to combine multiple filters, just use AND to combine them:
traffic > -1 AND last_update BETWEEN '2013-01-19T23:50:32.000' AND '2014-12-14T23:50:32.000'