pydata / pandas-datareader

Extract data from a wide range of Internet sources into a pandas DataFrame.
https://pydata.github.io/pandas-datareader/stable/index.html
Other
2.94k stars 681 forks source link

Response format from Yahoo seems to have changed I keep getting this error. #952

Open yeison opened 1 year ago

yeison commented 1 year ago

File "/Users/yeison/miniforge3/envs/tf-metal-0.6.0/lib/python3.10/site-packages/pandas_datareader/yahoo/daily.py", line 153, in _read_one_data data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"] TypeError: string indices must be integers

raphi6 commented 1 year ago

Sometimes I get the following error, if anyone could help out would be greatly appreciated :)

Sometimes I get the following error : If anyone could help out that would be great

""" Traceback (most recent call last): File "C:\Users\rapha\PycharmProjects\PROJECT\risk\var.py", line 406, in Backtest().range_of_days() File "C:\Users\rapha\PycharmProjects\PROJECT\risk\var.py", line 392, in range_of_days var = VaR(stock_list, temp_start, temp_end, weights, alpha).historical_var() np.sqrt(t) File "C:\Users\rapha\PycharmProjects\PROJECT\risk\var.py", line 38, in init yahoo_data = pandasdr.get_data_yahoo(s, end=end, start=start)['Close'] File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\data.py", line 80, in get_data_yahoo return YahooDailyReader(args, **kwargs).read() File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\base.py", line 258, in read df = self._dl_mult_symbols(self.symbols) File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\base.py", line 268, in _dl_mult_symbols stocks[sym] = self._read_one_data(self.url, self._get_params(sym)) File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\yahoo\daily.py", line 238, in _read_one_data data = new_j["HistoricalPriceStore"] UnboundLocalError: local variable 'new_j' referenced before assignment

Process finished with exit code 1 """

Yeah, I started to get the same error all of sudden Today. I added a 1 second pause (using the sleep() function) between sequential PDR queries in my script which worked most of the day, but then ran into the same issue again later in the day and for which a 2 second pause seems to have now fixed it once more, but who knows for how long. Playing with the PDR's retry_count and pause parameters did not seem to help much either, BTW.

More of a workaround than a solution really, but hope that helps nonetheless for now.

That is extremely helpful, thank you so much. As long as it works for the next couple months for my dissertation, all good !

spot92 commented 1 year ago

Sometimes I get the following error, if anyone could help out would be greatly appreciated :)

Sometimes I get the following error : If anyone could help out that would be great

""" Traceback (most recent call last): File "C:\Users\rapha\PycharmProjects\PROJECT\risk\var.py", line 406, in Backtest().range_of_days() File "C:\Users\rapha\PycharmProjects\PROJECT\risk\var.py", line 392, in range_of_days var = VaR(stock_list, temp_start, temp_end, weights, alpha).historical_var() np.sqrt(t) File "C:\Users\rapha\PycharmProjects\PROJECT\risk\var.py", line 38, in init yahoo_data = pandasdr.get_data_yahoo(s, end=end, start=start)['Close'] File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\data.py", line 80, in get_data_yahoo return YahooDailyReader(args, **kwargs).read() File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\base.py", line 258, in read df = self._dl_mult_symbols(self.symbols) File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\base.py", line 268, in _dl_mult_symbols stocks[sym] = self._read_one_data(self.url, self._get_params(sym)) File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\yahoo\daily.py", line 238, in _read_one_data data = new_j["HistoricalPriceStore"] UnboundLocalError: local variable 'new_j' referenced before assignment

Process finished with exit code 1 """

You're getting this using your pull request?

raphi6 commented 1 year ago

Sometimes I get the following error, if anyone could help out would be greatly appreciated :) Sometimes I get the following error : If anyone could help out that would be great """ Traceback (most recent call last): File "C:\Users\rapha\PycharmProjects\PROJECT\risk\var.py", line 406, in Backtest().range_of_days() File "C:\Users\rapha\PycharmProjects\PROJECT\risk\var.py", line 392, in range_of_days var = VaR(stock_list, temp_start, temp_end, weights, alpha).historical_var() np.sqrt(t) File "C:\Users\rapha\PycharmProjects\PROJECT\risk\var.py", line 38, in init yahoo_data = pandasdr.get_data_yahoo(s, end=end, start=start)['Close'] File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\data.py", line 80, in get_data_yahoo return YahooDailyReader(args, **kwargs).read() File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\base.py", line 258, in read df = self._dl_mult_symbols(self.symbols) File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\base.py", line 268, in _dl_mult_symbols stocks[sym] = self._read_one_data(self.url, self._get_params(sym)) File "C:\Users\rapha\PycharmProjects\PROJECT\venv\lib\site-packages\pandas_datareader\yahoo\daily.py", line 238, in _read_one_data data = new_j["HistoricalPriceStore"] UnboundLocalError: local variable 'new_j' referenced before assignment Process finished with exit code 1 """

You're getting this using your pull request?

Yeah, but appears it just happened yesterday, I will try again today and see if i get the same error. my code was also calling the API super fast so maybe that is why too, I believe @KryptoEmman was as well.

nmaiorana commented 1 year ago

The problem is that there is a bug in the code where "new_j" was never set due to a condition:

`

        j = json.loads(re.search(ptrn, resp.text, re.DOTALL).group(1))

        if "_cs" in j and "_cr" in j:
            new_j = decrypt_cryptojs_aes(j)  # returns j["context"]["dispatcher"]["stores"]
            # from old code

        data = new_j['HistoricalPriceStore']

`

If the condition is not met, "new_j" never gets set. Should "j" be used instead?

I can try to fix but I don't have the dev setup and probably won't have a chance to get to it until this weekend.

raphi6 commented 1 year ago

The problem is that there is a bug in the code where "new_j" was never set due to a condition:

`

        j = json.loads(re.search(ptrn, resp.text, re.DOTALL).group(1))

        if "_cs" in j and "_cr" in j:
            new_j = decrypt_cryptojs_aes(j)  # returns j["context"]["dispatcher"]["stores"]
            # from old code

        data = new_j['HistoricalPriceStore']

`

If the condition is not met, "new_j" never gets set. Should "j" be used instead?

I can try to fix but I don't have the dev setup and probably won't have a chance to get to it until this weekend.

Yeah, ive tried adding an else: to decrypt j again or just to keep the original j

But then there is an error that pandas-datareader cant read " AAPL " for example. It just returns an empty row. A way around this is to download data and just use it like that, pretty annoying though?

I think its a much more fundamental error with Yahoo! or datareader ? Also every j should contain " if "_cs" in j and "_cr" in j: " if it is encrypted by Yahoo. So that leads me to believe that there is some other problem and also error: Cant read AAPL, returning empty row.

It is also random, sometimes can get data for a specific date and then next time it fails for the same date.

raphi6 commented 1 year ago

The problem is that there is a bug in the code where "new_j" was never set due to a condition:

`

        j = json.loads(re.search(ptrn, resp.text, re.DOTALL).group(1))

        if "_cs" in j and "_cr" in j:
            new_j = decrypt_cryptojs_aes(j)  # returns j["context"]["dispatcher"]["stores"]
            # from old code

        data = new_j['HistoricalPriceStore']

`

If the condition is not met, "new_j" never gets set. Should "j" be used instead?

I can try to fix but I don't have the dev setup and probably won't have a chance to get to it until this weekend.

But I do agree, there should be some sort of "else:" to handle the exceptions. What I have tried so far with original or encrypted hasnt worked ... empty row.

KryptoEmman commented 1 year ago

@raphi6 - I have the same issue again this morning and now I'm getting it every time I do a query with the PDR. :(

harrybraviner commented 1 year ago

I was seeing this issue

UnboundLocalError: local variable 'new_j' referenced before assignment

on a small number of symbols (sorry, didn't record a list) yesterday (2023-01-12), and on everything I try today (2023-01-13). In case anyone needs symbols that don't work, I've tried '^GSPC', 'AAPL', 'A', 'INTC' today. These fail consistently, even if there has been a large pause since the previous query.

From memory (and I didn't record these, sorry) the symbols I had failures on yesterday were the same symbols each time. It's like Yahoo rolled out a change, but only to a few symbols yesterday and now it's hit all of the symbols.

raphi6 commented 1 year ago

Yeah it looks like it has completely stopped working again, Can anyone that has used yf.pdr_override() as a fix let me know if they get the same results for data, because to me it looks like the results are different to what pandas-datareader gets

harrybraviner commented 1 year ago

I tried adding that call at the start of my code, and I still got the same error.

On Fri, Jan 13, 2023, 11:59 raphi6 @.***> wrote:

Yeah it looks like it has completely stopped working again, Can anyone that has used yf.pdr_override() as a fix let me know if they get the same results for data, because to me it looks like the results are different to what pandas-datareader gets

— Reply to this email directly, view it on GitHub https://github.com/pydata/pandas-datareader/issues/952#issuecomment-1382137111, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMWWIRG6MFBIR2GNILJR2DWSGCYJANCNFSM6AAAAAATBU43XU . You are receiving this because you are subscribed to this thread.Message ID: @.***>

raphi6 commented 1 year ago

I tried adding that call at the start of my code, and I still got the same error. On Fri, Jan 13, 2023, 11:59 raphi6 @.> wrote: Yeah it looks like it has completely stopped working again, Can anyone that has used yf.pdr_override() as a fix let me know if they get the same results for data, because to me it looks like the results are different to what pandas-datareader gets — Reply to this email directly, view it on GitHub <#952 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMWWIRG6MFBIR2GNILJR2DWSGCYJANCNFSM6AAAAAATBU43XU . You are receiving this because you are subscribed to this thread.Message ID: @.>

have you imported yfinance as yf? should work

raphi6 commented 1 year ago

If anyone understand how the rest of pandas-datareader actually reads the data from Yahoo!'s website, that is most likely where the error is.

nichaelwichterle1 commented 1 year ago

Yes, using pandas_datareader and just today started getting the error as described above. Hopefully we can find a solution soon, thanks everyone for collaborating on this latest headache!

nmaiorana commented 1 year ago

I wish Yahoo would re-activate their API. That would make all this much easier.

I did notice that there is a separate link on the stock pages to download history. I wonder if the datareader needs to change to that URL.

On this screen, which is what I believe the datareader uses, there is a link to download:

URL Used by datareader

It's been a while since I've followed the code down to the web call, but I believe they scrape from main window.

KryptoEmman commented 1 year ago

From what I can tell from the output of the the "json.loads(re.search(ptrn, resp.text, re.DOTALL).group(1))" call in yahoo/daily.py module (see output attached), neither "_cs" or "_cr" are found, which from my puny and also possibly misguided understanding of this, is used to determine whether the data returned is encrypted or not, and if that is the case then triggers a call to the decrypt_cryptojs_aes(data) function. From the looks of it, some of the output appears to be encrypted. Hope some of these observations help anybody taking a deeper look than my limited understanding of the decrypt_cryptojs_aes(data) function would permit.

out.txt

KryptoEmman commented 1 year ago

I wish Yahoo would re-activate their API. That would make all this much easier.

I did notice that there is a separate link on the stock pages to download history. I wonder if the datareader needs to change to that URL.

On this screen, which is what I believe the datareader uses, there is a link to download:

URL Used by datareader

It's been a while since I've followed the code down to the web call, but I believe they scrape from main window.

Yes, using an available API instead would be sweet - much better than what effectively amounts to simply "screen scraping" which puts anyone doing this at the mercy of Yahoo substantially modifying their pages - how dare they! ;)

nmaiorana commented 1 year ago

I just asked ChatGPT and it suggested using yfinance. Has anyone had experience with it? The data format looks the same.

price_histories = yf.download(tickers=stock_universe,
                                  period='5y',
                                  auto_adjust=True)

So far no issues except I had to get the latest version to match my pandas version.

From Chat GPT:

import yfinance as yf
import datetime

# Define the ticker symbol and date range
ticker = "AAPL"
start = datetime.datetime.now() - datetime.timedelta(days=365)
end = datetime.datetime.now()

# Download the historical stock prices
data = yf.download(ticker, start=start, end=end)

# Print the data
print(data)
bishdj22 commented 1 year ago

@nmaiorana - Can you please share the steps to match the yfinance version to Pandas? Thanks

nmaiorana commented 1 year ago

I'm still running tests. I have one failing (Average Dollar Volume). But here is what I did:

Original Code:

            start = datetime(year=2019, month=1, day=1)
            end = datetime(year=2020, month=1, day=1)

            yahoo_reader = pdr.yahoo.daily.YahooDailyReader(symbols=['AAPL', 'GOOG'], start=start, end=end,
                                                            adjust_price=True,
                                                            interval='d', get_actions=False, adjust_dividends=True)
            test_data_df = yahoo_reader.read()
            yahoo_reader.close()

New Code:


            start = datetime(year=2019, month=1, day=1)
            end = datetime(year=2020, month=1, day=1)
            test_data_df = yf.download(tickers=['AAPL', 'GOOG'], start=start, end=end, auto_adjust=True)
            test_data_df.rename_axis(columns=['Attributes', 'Symbols'], inplace=True)
            test_data_df.to_csv(test_data_file, index=True)

28 Other tests passed. I'm still trying to see why. Last time this happened it was because the datareader changed the way it made the adjusted prices.

nmaiorana commented 1 year ago

The test failure was due to the difference in precision when the adjusted prices were computed. I'm going to change my test cases to round to 2 decimal places prior to using the dataset. Might break some other tests due to small decimal values, I'll just have to adjust the expected values.

nichaelwichterle1 commented 1 year ago

I'm certainly not a a comp sci or dev ops type, by my trouble shooting has so far not resulted in any progress. This seems to be a major change with regards to encryption. Would greatly appreciate if anyone finds a go-between to extract the data and store it in a pandas dataframe. Much appreciated. Fingers crossed for the Yahoo API....

raphi6 commented 1 year ago

I'm certainly not a a comp sci or dev ops type, by my trouble shooting has so far not resulted in any progress. This seems to be a major change with regards to encryption. Would greatly appreciate if anyone finds a go-between to extract the data and store it in a pandas dataframe. Much appreciated. Fingers crossed for the Yahoo API....

I believe the encryption is fine, my friend uses a similar package that reads the data differently from Yahoo! but uses the same decryption and it still works, I think pandas-datareader is just falling apart now.

raphi6 commented 1 year ago

I'm still running tests. I have one failing (Average Dollar Volume). But here is what I did:

Original Code:

            start = datetime(year=2019, month=1, day=1)
            end = datetime(year=2020, month=1, day=1)

            yahoo_reader = pdr.yahoo.daily.YahooDailyReader(symbols=['AAPL', 'GOOG'], start=start, end=end,
                                                            adjust_price=True,
                                                            interval='d', get_actions=False, adjust_dividends=True)
            test_data_df = yahoo_reader.read()
            yahoo_reader.close()

New Code:


            start = datetime(year=2019, month=1, day=1)
            end = datetime(year=2020, month=1, day=1)
            test_data_df = yf.download(tickers=['AAPL', 'GOOG'], start=start, end=end, auto_adjust=True)
            test_data_df.rename_axis(columns=['Attributes', 'Symbols'], inplace=True)
            test_data_df.to_csv(test_data_file, index=True)

28 Other tests passed. I'm still trying to see why. Last time this happened it was because the datareader changed the way it made the adjusted prices.

Have you tried using yf.pdr_override() ? does this cause any difference between the old pandas-datareader ?

nmaiorana commented 1 year ago

I tried, I could not get it to work. I changed my price histories function to use yfinance. They made some updates in December and it seems to be working fine for me. My test cases are all passing and frankly, it's much faster than the datareader.

nichaelwichterle1 commented 1 year ago

Thanks very much everyone! I've migrated to yfinance and am looking to unplug/replug the data now from yfinance into my originally designed scripts. The only problem I seem to be having now relates to Dates: the format i want is simply year-month-date but I'm also getting a timestamp. I tried the formats above to no avail. Using widgets, some of the errors I've been encountering have been:
1 Failed download:

any input would be much appreciated! many thanks

nichaelwichterle1 commented 1 year ago

stock1 = yf.download("ticker1", start="startDate", end="endDate")

nichaelwichterle1 commented 1 year ago

Or specifically, the date I'm getting in my dataframe is: 2022-02-17 00:00:00-05:00 Would anyone know how to get rid of the time component ? All I need is the date 2022-02-17 and not the 00:00:00-05:00 component

I think that's why I'm getting getting a "TypeError: float() argument must be a string or a number, not 'Timestamp' Thanks again everyone! much appreciated

nmaiorana commented 1 year ago

After you get the data, you need to change the index, it's not interpreted as a date from yfinance.

I had to convert mine to Pandas DatetimeIndex.

    price_histories = yf.download(tickers=symbols, auto_adjust=True, **argv)
    price_histories.index = pd.DatetimeIndex(price_histories.index)

This does not get rid of the time component, but will allow you to filter by date.

CKDarling commented 1 year ago

As work around you may try to use solution from https://pypi.org/project/yfinance/ title pandas_datareader override

Im going on record and saying all who use Yahoo! Finance data need to put effort into maintaining Yfinance. This issue has been apparent for over a month and very little has been done by the team who manages this repo. Yfinance admin fixed it within a few days of the Yahoo! API encryption.

nichaelwichterle1 commented 1 year ago

After you get the data, you need to change the index, it's not interpreted as a date from yfinance.

I had to convert mine to Pandas DatetimeIndex.

    price_histories = yf.download(tickers=symbols, auto_adjust=True, **argv)
    price_histories.index = pd.DatetimeIndex(price_histories.index)

This does not get rid of the time component, but will allow you to filter by date.

Thanks very much for your help, pls excuse my lack of programing proficiency - I feel like I'm on the verge of getting this all done but just not quite there yet. I'm using:

ticker1 = widgets.Text( value='TSLA', description='Ticker 1', placeholder='TSLA', disabled=False ) startDate = widgets.DatePicker( value=datetime.date(2022,2,17), description='Start Date', disabled=False ) endDate = widgets.DatePicker( value=datetime.date.today(), description='End Date', disabled=False ) Stock1 = yf.download(ticker1.value, start=startDate.value, end=endDate.value)

...this all works so far however I'm still getting Timestamp data and have not been able to get a string or number as the rest of my code would need. Where would I insert the above two lines you highlighted above ? I've been trying to convert the date index I'm sorry, still struggling with this one...

NRHartFRC commented 1 year ago

Interpreted conflict summary & progress breakdown:

Appears Yahoo Finance (YF) has migrated their codebase to a different architecture--perhaps react+redux architecture--which is rendering API objects/variables/data in a non-conventional manner. Raphi6 has decrypted the YF [AES] feature and has modified the pandas-datareader library accordingly. Now, we are experiencing data acquisition bugs (e.g., string indicies must be integers, new_j is referenced before assignment, etc...).

Proposed crux:

React-Redux architecture operates as a real-time, 'global state bus' to hold all variables/objects/states/values/facts/truths, and updates them without requiring end-user intervention between updates [1]. Long term scalability is the motivation here for YF. "Why is local context required at all?," is the rhetoric that contradicts our understanding of this migration bug. Per Figure 1, while actions/events from the UI are dispatched to a reducer (located in the store), the actions undergo a 'state' interaction--depicted by a state 'loopback.' Once reduced, this interaction event is seamlessly updated in the store (globally). I believe this interaction phase (between reducer and store) is casting the datatype to a string element array (despite pdr wanting an integer to start with), plus this is happening all out of context (pdr wants local context, YF uses global state context). The spread operator [...], located in the reducer component (.js file on YF backend) for YF HistoricalPriceStore encryption, creates a 'shallow clone' of the original object, with global-context, and string datatype of all dispatched events/actions. Once the global store 'new_j' state is updated (that is an action that is dispatched to the store on YF), the 'local-context-state' of new_j has not yet been set as a local reference yet--only as a global reference in the redux store. The state of the redux store is updated using the spread operator, in a global context, as a string, before the python compiler interprets the locally referenced decryption assignment in your daily.py component (python/pdr wants integers). So fixing one problem, say the datatype issue, creates another migration issue by virtue of Redux dataflow (see Figure 1).

Proposed solution:

Curly braces {} could be beneficial to help python see a 'key-value pair' from JSX parser on YF backend (see event: [action.state, ...state.events] below). In other words, a dictionary could be created, rather than malformed string that is fetched from redux store to create new_j (i.e., malformed for pdr). Attribute key is the decrypted new_j state, value is the decrypted HistoricalPriceStore value. So, change your 'event handling' decryption process in daily.py. Specifically that of the data = new_j['HistoricalPriceStore']. Pandas-datareader appears to want a numerical datatype for new_j rather than a string, YF wants string to talk to the redux store. Try updating your event attribute handling of the new_j data list as a dictionary. Specifically, try experimenting with curly braces in combination with the grave symbol, or backquote (located above tab key) to embed integers into strings, then parse as needed. The event on YF backend will dispatch the 'action.event' (current array state, interpreted as local to your event handler function read_one_data). In the store, '...state.events' (appended state update request/confirmation, interpreted as global event handler action that fetches prices from HistoricalPriceStore list) needs to wait for the python reference to be acknowledged before updating. Fetch HistoricalPriceStore from a dictionary that has the proper typecasting and context.Theoretically, new_j bug could be a matter of object/variable --> (redux-state) declaration/assignment vs. re-declaration/re-assignment that is out of context. Solution is to pull the string element values from the HistoricalPriceStore event, typecast the values to integers in a local context for python/pdr to use, while avoiding emitting an event/action to the redux store via YF dispatcher (this happens in parallel, which is what gives UnboundLocalError). Again, this must happen before the redux state is updated in the global-context-store, otherwise new_j will be out of context. Mediocre solution would involve making 'new_j' a global variable, so as to mimic a redux store state that may be accessible at a higher level, and more importantly, in the proper global context (will likely not work). Spread operator (enemy here) ought to appear as an attribute key-value pair in a case statement in the reducers.js component (of YF backend) and may appear as such:

Sample YF backend key-value attribute handling for new_j data action event to update HistoricalPriceStore (all YF backend):

events: [action.event, ...state.events] //current key-value pair in redux (data assignment), update store events

Sample reference case statement REDUCER for react-redux (typ. JSX syntax rather than javascript):

case 'DECYRPTION_SUCCESS': return { ...state, decryption: { decryptionType: 'AES', isPending: false, isSuccessful: true }, decryptionInProgress: false, events: [action.event, ...state.events] //HistoricalPriceStore decryption, ...append decrypted value to store }

Disclaimer: I am not an expert on this subject. Hope this helps your dissertation success. All for OP and OP for all!

let Spread operator = [...] appends elements to existing array, while preserving original array (seems to be a string here) Dispatch = construct action/payload event for global state acquisition (declaration, or redeclaration) Event handler = arbitrary function for data acquisition, function called for UI actions Event = message/attribute in reducers.js component file and top-level function (i.e., deposit handler event [1]) Emit = dispatch/broadcast an event Bus = binary registry stack of attributes/events Component = javascript (JSX) file in /src/assets directory State = binary representation of facts, truth, objects, variables, results, booleans, etc... Action = input at UI level, string of attributes bundled into an 'event' that is dispatched to reducer, then to store Reducer = data handler (de-abstraction step, metaphorical automated binary interface that talks to the store) Interactions = cross-functional/cross-app data acquisition from previous 'state' to new 'state' Store = truth/fact/global state bus/message bank/database/

Figure 1. Redux dataflow https://redux.js.org/tutorials/fundamentals/part-2-concepts-data-flow [1]

nichaelwichterle1 commented 1 year ago

Or can anyone provide some online forum where I can seek some help and ask some python related questions ? I really think I'm close but would love to get this to work again. I'd even pay for a bit of help! Much appreciated

santosh1383 commented 1 year ago

Here I share a few tricks to solve this problem:

import pandas from pandas_datareader import data as pdr import yfinance as yfin yfin.pdr_override()

then

df = pdr.get_data_yahoo("TSLA", start="yyyy-mm-dd", end="yyyy-mm-dd") print(df)

Hopefully it can be useful, thanks :)

nichaelwichterle1 commented 1 year ago

Thanks very much!! ... I'm using the yfin.pdr_override() but still one little issue however:

Stock1 = pdr.get_data_yahoo("ticker1", start="yyyy-mm-dd", end="yyyy-mm-dd") Stock1

now the error I'm getting is: 1 Failed download:

btw when I type in: Stock1.index.dtype --> datetime64[ns, America/Toronto]

nichaelwichterle1 commented 1 year ago

Thanks again everyone for your help, unfortunately I will be away all of next week but hopefully when I'm back and can give another crack at this (given all your input) I'll finally get things to work again (fingers crossed). Once again, thx very much in advance. Back in a week!

davimmilhome commented 1 year ago

This issue still up, but im very impressed with your tryes to solve it

yuzhipeter commented 1 year ago

yahoo finance has new problem again.

df = web.DataReader("SPY", data_source='yahoo', start='2023-01-01', end='2023-01-13', session = sesh)

Error msg

Traceback (most recent call last):

File "C:\Users\peter\AppData\Local\Temp/ipykernel_27516/630144412.py", line 1, in df = web.DataReader("SPY", data_source='yahoo', start='2023-01-01', end='2023-01-13', session = sesh)

File "C:\Users\peter\anaconda3\lib\site-packages\pandas\util_decorators.py", line 207, in wrapper return func(*args, **kwargs)

File "C:\Users\peter\anaconda3\lib\site-packages\pandas_datareader\data.py", line 370, in DataReader return YahooDailyReader(

File "C:\Users\peter\anaconda3\lib\site-packages\pandas_datareader\base.py", line 253, in read df = self._read_one_data(self.url, params=self._get_params(self.symbols))

File "C:\Users\peter\anaconda3\lib\site-packages\pandas_datareader\yahoo\daily.py", line 227, in _read_one_data new_j = decrypt_cryptojs_aes(

File "C:\Users\peter\anaconda3\lib\site-packages\pandas_datareader\yahoo\daily.py", line 81, in decrypt_cryptojs_aes plaintext = unpad(plaintext, 16, style="pkcs7")

File "C:\Users\peter\anaconda3\lib\site-packages\Crypto\Util\Padding.py", line 92, in unpad raise ValueError("Padding is incorrect.")

ValueError: Padding is incorrect.

Thanks!

pyproper commented 1 year ago

I just asked ChatGPT and it suggested using yfinance. Has anyone had experience with it? The data format looks the same.

price_histories = yf.download(tickers=stock_universe,
                                  period='5y',
                                  auto_adjust=True)

So far no issues except I had to get the latest version to match my pandas version.

From Chat GPT:

import yfinance as yf
import datetime

# Define the ticker symbol and date range
ticker = "AAPL"
start = datetime.datetime.now() - datetime.timedelta(days=365)
end = datetime.datetime.now()

# Download the historical stock prices
data = yf.download(ticker, start=start, end=end)

# Print the data
print(data)

Thank you @nmaiorana Just wanted to confirm this works as of 1/26/23 13:28 pm I only pulled prices so I cannot confirm that other data attributes work or not.

jfomann29 commented 1 year ago

@nichaelwichterle1 - try this after creation of data frame:

df['Date_New'] = data.index df['Date_New'] = pd.to_datetime(df['Date_New']).tz_convert('America/New_York').dt.strftime('%Y-%m-%d') df = df.set_index('Date_New')

nichaelwichterle1 commented 1 year ago

Thank you so much, after much trying I'm finally getting an output and all works for Stock1 and for that I'm overjoyed!

My last question however is that I need to do the same process for Stock2, however the output for Stock2 seems to be a float. How can I convert Stock2 to a dt format like Stock1 so that I can run the same process and then finally graph both 1 and 2 ? Once again, I'm very appreciative of all the help!

Python Screenshot 2023-01-30new

nichaelwichterle1 commented 1 year ago

Hi everyone, Im making alot of progress and really getting close now – can someone pls help me with my code, the output I want is almost all there but I’d love to know what code I should insert so that the dates on the x-axis are automatically spaced out depending on the date range I input.

The pic below is what I’m getting so far, as you can see, the spacing of the dates is problematic. That is the last issue I’m having. Many thanks for your help and input, it is all much appreciated!! Final output for dscrd

nichaelwichterle1 commented 1 year ago

Right, I am using yfinance to pull the data. I just am not able to change the date spacing on the x axis however. Anyone ?

jfomann29 commented 1 year ago

@nichaelwichterle1 what data type is the index of your underlying dataframe? If it is not DatetimeIndex, try that before plotting the graph.

import datetime as dt df.index = pd.to_datetime(df.index)

nichaelwichterle1 commented 1 year ago

Thanks very much for your input. I was initially having problems because downloading the dataframe initially yielded year-month-day-hour-seconds which was problematic. I had to convert it via: Stock1['Date_New'] = pd.to_datetime(Stock1['Date_New']).tz_convert('America/New_York').dt.strftime('%Y-%m-%d')

this worked well enough but I have no idea how to change the date spacing on the x axis. Thx again, it is very much appreciated!

uad1098 commented 1 year ago

when I code yfinance as follows by santosh1383 commented on Jan 20

import pandas from pandas_datareader import data as pdr import yfinance as yfin yfin.pdr_override()

df = pdr.get_data_yahoo("TSLA", start="yyyy-mm-dd", end="yyyy-mm-dd") print(df)

I get exception following exception error. Can anyone tell me why and what am I missing? Exception in thread Thread-1: Traceback (most recent call last): File "C:\Users\RAM LLC\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner self.run() File "C:\Users\RAM LLC\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run self._target(*self._args, *self._kwargs) File "C:\Users\RAM LLC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\multitasking__init__.py", line 102, in _run_via_pool return callee(args, kwargs) File "C:\Users\RAM LLC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\yfinance\multi.py", line 169, in _download_one_threaded data = _download_one(ticker, start, end, auto_adjust, back_adjust, File "C:\Users\RAM LLC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\yfinance\multi.py", line 181, in _download_one return Ticker(ticker).history(period=period, interval=interval, File "C:\Users\RAM LLC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\yfinance\base.py", line 157, in history data = data.json() File "C:\Users\RAM LLC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\models.py", line 898, in json return complexjson.loads(self.text, kwargs) File "C:\PythonPrograms\Python\Python38-32\LIb\json__init__.py", line 357, in loads return _default_decoder.decode(s) File "C:\PythonPrograms\Python\Python38-32\LIb\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\PythonPrograms\Python\Python38-32\LIb\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

uad1098 commented 1 year ago

Solved my problem: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) Thanks to GabrieleTap - uninstalled and installed yfinance and pandas-datareader

GabrieleTap commented on Jul 19, 2021 This worked for me:

pip uninstall yfinance pip uninstall pandas-datareader pip install yfinance --upgrade --no-cache-dir pip install pandas-datareader

heetsamber commented 1 year ago

from pandas_datareader import data as web from datetime import datetime import yfinance as yf

yf.pdr_override()

symbol = "000660.KS"

start = datetime(2016,3,1) end = datetime(2016, 3, 31) sk = web.get_data_yahoo(tickers=symbol, start=start,end=end) # yf.download() print(sk)

sanjayram15 commented 10 months ago

File "/Users/yeison/miniforge3/envs/tf-metal-0.6.0/lib/python3.10/site-packages/pandas_datareader/yahoo/daily.py", line 153, in _read_one_data data = j["context"]["dispatcher"]["stores"]["HistoricalPriceStore"] TypeError: string indices must be integers

if you find an solution kindly update an solution @ sanjayram1515