Yahoo Financial Stock info data loading into Pandas - python

I would like to load stock info data into pandas. Code
import yfinance as yf
abb = yf.Ticker("ABB.NS")
# get stock info
abb.info
How do i load abb.info data into pandas. Please throw some lights on this?

If you only want to convert the dict to pandas dataframe you can do it as follows:
pd.DataFrame.from_dict(abb.info, orient='index')
Generally I would recommend you to use the pandas_datareader pibrary for this task.
check the following code taken from [1].
pandas dataframe available at [2]
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)
# download dataframe
data = pdr.get_data_yahoo("SPY", start="2017-01-01", end="2017-04-30")
[1] https://github.com/ranaroussi/yfinance
[2] https://github.com/pydata/pandas-datareader

Related

Attempting to extract stock prices using python

I am currently trying to extract stock prices from a list of stock codes contained on a csv file by using pandas and yfinance.
I have 145 companies I need to do this for, is there a way of doing it? As I have tried over a period of 5 days without success.
I just need to know if its possible and what would you recommend to achieve this.
yfinance.Ticker(ticker).history(start=start_date) gets you the data that you desire.
if you have a giant csv, with a field "ticker", you can create a pandas dataframe with the below:
import pandas as pd
import yfinance
def read_create_giant_df(file_in):
df = pd.read_csv(file_in)
out = []
for item in df["ticker"]:
ticker_df = yfinance.Ticker(item).history(start="1930-01-01")
ticker_df["ticker"] = item
out.append(ticker_df)
return pd.concat(out)
below code should work , if any module missing use command to install it .
pip install yfinance
pip install yahoofinancials
Run below code to get the data for Amazon-AMZN
import pandas as pd
import yfinance as yf
from yahoofinancials import YahooFinancials
amzn_df = yf.download('AMZN',
start='2019-01-01',
end='2019-12-31',
progress=False)
amzn_df.head()

PyCharm not showing dataframe when I press ctrl+alt+f10

I'm trying to backtest a simple strategy of mine and the first step is to retrieve historical data from yfinance. However, whenver I run this, I can't see the contents of hist. Instead, it just has this outputthis output
# import all the libraries
import nsetools as ns
import pandas as pd
import numpy
from datetime import datetime
import yfinance as yf
import matplotlib.pyplot as plot
plot.style.use('classic')
a = input("Enter the ticker name you wish to apply strategy to")
ticker = yf.Ticker(a)
hist = ticker.history(period="1mo", interval="5m")
hist
I really just want to see the historical prices against the time but can't get the dataframe to appear. I would appreciate any input on this.

problem with 1m interval in get_data_yahoo(pandas_datareader)

I wrote
from pandas_datareader import data as pdr
import yfinance as yf
data = pdr.get_data_yahoo("AAPL", start='2020-04-12', end='2020-04-13', interval="m")
And I got a error
KeyError 'Date'
I heard people saying Yahoo finance API is down but I still can get the following code working correctly
data = pdr.get_data_yahoo("AAPL", start='2020-04-12', end='2020-04-13')
pandas_datareader interval="m" means 1 month you need yfinance.
pip download yfinance
import yfinance
a = yfinance.download(ticker="your symbol",period="5d",interval="1m")

Slicing a spreadsheet in Python terminal

So I'm making a program that asks a server a data and it send back. The problem is when I ask for some data it answer:
And I need just the value from the column "Close" . I can't figure out how to do it.
Code:
import yfinance as yf
import time
stock = yf.Ticker("MSFT")
data = stock.history(period="1")
oferta= stock.info["bid"]
print(data)
yfinance uses the pandas library to return data. You are receiving a pandas' DataFrame object in data, so to select only the "Close" column you can do this:
print(data["Close"].iloc[0])

pandas yahoo finance real time data

Is it possible to get real time stock data with pandas from yahoo finance?
For historical data i would do the following:
import pandas.io.data as web
aapl = web.get_data_yahoo('AAPL')
Is there a way to get the latest aapl price?
EDIT:
Yahoo has ended their free finance API so this answer is no longer relevant. Below is my answer for pre-2019 purposes.
Archival:
There's plenty of libraries available for this. Pandas doesn't explicitly do this though.
Most simply, I would suggest you just use a web library to download yahoo data. I like using requests, but you could also use urllib. You can coerce the response into a data frame after you get it.
import requests
requests.get("http://download.finance.yahoo.com/d/quotes.csv?s=AAPL&e=.csv&f=nsl1op").text
The nsl1op format var is from the docs:
I will use here the tags of name(n), symbol(s), the latest value(l1), open(o) and the close value of the last trading day(p)
Your response data should look something like
u'"Apple Inc.","AAPL",114.63,113.50,115.07\r\n'
You can just split up the string using the csv library and throw it into a data frame from there
To answer your question about using Pandas specifically, you can pull stock data from yahoo using pandas like so:
from pandas.io.data import DataReader
from datetime import datetime
aapl = DataReader('AAPL', 'yahoo', datetime(2015,7,1), datetime(2015,7,1))
print(aapl['Adj Close'][0])
This code results in:
126.599998
The other keys you can use are Open, Close, High, Low, and Volume.
Keep in mind that the returned dataset is an array. You need to enumerate said array to get the data, either by specifying your index, or with a for loop.
You can use below code to get all info from yahoo finace api:
import pandas as pd
from pandas_datareader import data as wb
aapl=wb.DataReader('AAPL',start='2015-1-1',data_source='yahoo')
print(aapl)

Categories

Resources