Reputation: 129
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?
Upvotes: 3
Views: 7447
Reputation: 5588
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
Upvotes: 4
Reputation: 75
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)
Upvotes: 0
Reputation: 955
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.
Upvotes: 2