[pandas] How to get PE and MarketCap?

2,046 views
Skip to first unread message

Lukasz Szybalski

unread,
Oct 14, 2013, 1:12:18 AM10/14/13
to pyd...@googlegroups.com
Hello,
I'm trying to figure out how to get price earnings and market cap for "MSFT" stock.

I see the function get_quote_yahoo in the source, and I see mentioning of symbol and

    request = ''.join(compat.itervalues(_yahoo_codes)) # code request string
    header = list(_yahoo_codes.keys())


https://github.com/pydata/pandas/blob/7261d436aab5e32e20cb5e91891304b124cc32ed/pandas/io/data.py


How do I go from :
msft = DataReader("MSFT",  "yahoo", datetime(2009,1,1))

to getting the PE and market cap?

Thank you
Lucas

Skipper Seabold

unread,
Oct 14, 2013, 10:00:15 AM10/14/13
to pyd...@googlegroups.com
I'm not sure, but it doesn't look this is is explicitly available the way this is written. Correct me if I'm wrong. Presumably, you can just adjust the URL by hand to your needs though and load it into a DataFrame. 

I don't know about getting historical data, but to get an idea, since the _yahoo_codes dictionary is mutable, you can do something like this. I don't know why market cap doesn't work.

from pandas.io.data import get_quote_yahoo, _yahoo_codes
get_quote_yahoo("MSFT")
_yahoo_codes.update({'MarketCap' : 'j1'})
get_quote_yahoo("MSFT")
_yahoo_codes.update({'52WeekLow' : 'j'})
get_quote_yahoo("MSFT")


 
Thank you
Lucas

--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

szyb...@gmail.com

unread,
Oct 14, 2013, 10:36:20 PM10/14/13
to pyd...@googlegroups.com


On Monday, October 14, 2013 9:00:15 AM UTC-5, Skipper Seabold wrote:
On Mon, Oct 14, 2013 at 6:12 AM, Lukasz Szybalski <szyb...@gmail.com> wrote:
Hello,
I'm trying to figure out how to get price earnings and market cap for "MSFT" stock.

I see the function get_quote_yahoo in the source, and I see mentioning of symbol and

    request = ''.join(compat.itervalues(_yahoo_codes)) # code request string
    header = list(_yahoo_codes.keys())


https://github.com/pydata/pandas/blob/7261d436aab5e32e20cb5e91891304b124cc32ed/pandas/io/data.py


How do I go from :
msft = DataReader("MSFT",  "yahoo", datetime(2009,1,1))

to getting the PE and market cap?


I'm not sure, but it doesn't look this is is explicitly available the way this is written. Correct me if I'm wrong. Presumably, you can just adjust the URL by hand to your needs though and load it into a DataFrame. 

I don't know about getting historical data, but to get an idea, since the _yahoo_codes dictionary is mutable, you can do something like this. I don't know why market cap doesn't work.

from pandas.io.data import get_quote_yahoo, _yahoo_codes
get_quote_yahoo("MSFT")
_yahoo_codes.update({'MarketCap' : 'j1'})
get_quote_yahoo("MSFT")

Thank you.
That worked, but it looks like the Market Cap is returned as:
287.0B

and I guess for some reason it doesn't get parsed, probably because of the B?

get_quote_yahoo("MSFT")
      52WeekLow  DividentYeld  MarketCap 
MSFT      26.26           2.7        NaN


Any idea what that might be caused by, how to fix it/go around it?

Thanks
Lucas



 

Skipper Seabold

unread,
Oct 15, 2013, 4:43:58 AM10/15/13
to pyd...@googlegroups.com
float("203.B")

Yeah, I'd just roll the source into your own function and parse it yourself. Ideally, you can fix it and make a pull request.

https://github.com/pydata/pandas/issues/5229

from pandas.io.data import _yahoo_codes
from pandas.io.common import urlopen
import pandas.compat as compat
from collections import defaultdict

sym_list = 'MSFT'
_yahoo_codes.update({'MarketCap' : 'j1'})
request = ''.join(compat.itervalues(_yahoo_codes))  # code request string
header = list(_yahoo_codes.keys())

data = defaultdict(list)

                                                               request)

with urlopen(url_str) as url:
    lines = url.readlines()

# parse this correctly.

Skipper
Reply all
Reply to author
Forward
0 new messages