url NotFoundError with quandl - python

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')

Related

Function to export multiple yfinance stocks to csv

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

Python error "Unable to read URL" even though "Requirement already satisfied" in command prompt

I've been trying to work on some beginner/entry level python projects for a resume (working with WIN10) and I've been running into the same constant error. I've been trying to pull info from yahoo finance and I get this error, no matter how I rework my code:
import pandas_datareader as web
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime as dt
start = dt.datetime(2018,1,1)
end = dt.datetime.now()
tickers = ["FB", "GS", "NVDA", "MSFT", "TSLA", "AAPL", "CCL", "BA"]
colnames = []
for ticker in tickers:
data = web.DataReader(ticker, "yahoo", start, end)
if len(colnames) == 0:
combined = data[['Adj Close']].copy()
else:
combined = combined.join(data['Adj Close'])
colnames.append(ticker)
combined.columns = colnames
print(combined)
RemoteDataError: Unable to read URL: https://finance.yahoo.com/quote/FB/history?period1=1514779200&period2=1652673599&interval=1d&frequency=1d&filter=history
I looked around and saw that yahoo finance changed it's structure for python, so I then tried changing the format or my code, still got the error. I tried installing suggested libraries to fix it, and while it says that their installed, I can't use the libraries because it says that module isn't found.
I've looked at other Stack posts and none of the solutions are helping.

Accessing nested dictionary from a JSON, with variable headers

I am trying to use json_normalize to parse data from the yahoo financials package. Seem to be running into an issue when trying to separate the columns out from the last object, a variable date. Each date I believe is a dictionary, which contains various balance sheet line items.
My code is:
import json
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
import yfinance as yf
from yahoofinancials import YahooFinancials
tickerinput = "AAPL"
ticker = yf.Ticker(tickerinput)
tickerfin = YahooFinancials(tickerinput)
balancesheet = tickerfin.get_financial_stmts('annual', 'balance')
''' Flattening w json_normalize'''
balsheet = pd.json_normalize(balancesheet, record_path=['balanceSheetHistory', tickerinput])
I have also tried using this below code but receive a key error, despite it being in the original JSON output.
balsheet = pd.json_normalize(balancesheet, record_path=['balanceSheetHistory', tickerinput], meta=['2021-09-25', ['totalLiab','totalStockholderEquity','totalAssets','commonStock','otherCurrentAssets','retainedEarnings','otherLiab','treasuryStock','otherAssets','cash','totalCurrentLiabilities','shortLongTermDebt','otherStockholderEquity','propertyPlantEquipment','totalCurrentAssets','longTermInvestments','netTangibleAssets','shortTermInvestments','netReceivables','longTermDebt','inventory','accountsPayable']], errors='ignore')
The main issue is that I am returned the below data frame:
Returned dataframe from balsheet
Sample Output of the JSON file:
JSON Output (balancesheet variable)

Where am I going wrong retrieving stock data from Quandl?

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

Parse requests.get() output into a pandas dataframe

I am following a tutorial an am stuck at parsing the output of requests.get()
My goal is to connect to the API below to pull historical crypto-currency prices and put them into a pandas dataframe for further analysis.
[API: https://www.cryptocompare.com/api/#-api-data-histoday-]
Here's what I have.
import requests
response = requests.get("https://min-api.cryptocompare.com/data/histodayfsym=ETC&tsym=USD&limit=10&aggregate=3&e=CCCAGG")
print(response.text)
Now I want to output into a dataframe...
pd.DataFrame.from_dict(response)
But I get...
PandasError: DataFrame constructor not properly called!
You can use the json package to convert to dict:
import requests
from json import loads
import pandas as pd
response = requests.get("https://min-api.cryptocompare.com/data/histodayfsym=ETC&tsym=USD&limit=10&aggregate=3&e=CCCAGG")
dic = loads(response.text)
print(type(dic))
pd.DataFrame.from_dict(dic)
However as jonrsharpe noted, a much more simple way would be:
import requests
import pandas as pd
response = requests.get("https://min-api.cryptocompare.com/data/histodayfsym=ETC&tsym=USD&limit=10&aggregate=3&e=CCCAGG")
print(type(response.json()))
pd.DataFrame.from_dict(response.json())

Categories

Resources