yahooquery icon indicating copy to clipboard operation
yahooquery copied to clipboard

Converting data in a dictionary format into one in Pandas DataFrame

Open informatism opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. When obtaining the data via yahooquery, there seem to be Python dictionary and Pandas DataFrame formats. I would like to be able either to convert between those two formats OR to obtain data in whichever format.

aapl = Ticker('aapl')
aapl.institution_ownership

aapl = Ticker('aapl')
aapl.get_modules('institutionOwnership')

The advantage is that I can just call all_modules once and get every module (e.g., institutionOwnership and others) already. Saving requests and not reaching the rate limit.

Another advantage is that depending one when/why/how, I use one method or another. It'd be easier to compare two different data from the same ticker, etc.

Describe the solution you'd like Is there a way to convert data in a dictionary format into one in Pandas DataFrame (vice verse). For example, I can take aapl.get_modules('institutionOwnership') and turn it into the same DF format as aapl.institution_ownership

aapl = Ticker('aapl')
dict_to_dataframe(aapl.get_modules('institutionOwnership'))

or

aapl = Ticker('aapl')
data = aapl.all_modules
dict_to_dataframe(data['institutionOwnership'])

In fact, some of these seems to be already available via functions in ticker class, e.g., to_dataframe.

Or perhaps there's already a way to do this :)

Describe alternatives you've considered Alternatively, I just call aapl.institution_ownership and other modules one-by-one. It's a bad since calling all_modules is just one call, so it would save everyone many requests.

informatism avatar Mar 25 '23 13:03 informatism

Hi, Maybe I am totally missing the point, but I'll just focus on

Is there a way to convert data in a dictionary format into one in Pandas DataFrame (vice verse).

Yes, the pd.DataFrame() function. If you run this:

stocks = ['aapl','msft','nvda','googl']
yq_stocks = Ticker(stocks)
stock_data = yq_stocks.all_modules
stockdf = pd.DataFrame(stock_data)
stockdf

The result looks like this.

And then you can pick any one of the modules to dig down. To pick financialData, you can do:

stock_fd = list(stockdf.T.financialData.values)
pd.DataFrame(stock_fd, index=stocks).T

Which should give you something like this.

Is this what you're after? Apologies, if I missed it.

Cheers, Matt

ms82494 avatar Mar 31 '23 03:03 ms82494