Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticker.history: history's index is not of type DateTimeIndex when dividend and split tables are empty #937

Closed
ProgrammingDao opened this issue Jan 22, 2022 · 6 comments · Fixed by #941 or #943

Comments

@ProgrammingDao
Copy link

ProgrammingDao commented Jan 22, 2022

Issue:

Ticker.history (line 295):

  df.index = df.index.tz_localize("UTC").tz_convert(data["chart"]["result"][0]["meta"]["exchangeTimezoneName"])

E AttributeError: 'Index' object has no attribute 'tz_localize'

Reason:

During the concatenation of a proper price table with empty dividend and split tables, the price table's index has changed from pandas.DatetimeIndex to pandas.Index because the empty tables had this index type.

Proposed solution:

Modify parse_actions (in utils.py) to initialise the dividend and split tables with :
dividends = _pd.DataFrame(columns=["Dividends"], index=_pd.DatetimeIndex([]))
splits = _pd.DataFrame(columns=["Stock Splits"], index=_pd.DatetimeIndex([]))

Local modules' versions:

finance : 0.1.69
pandas : 1.4.0
python : 3.10

@ProgrammingDao
Copy link
Author

ProgrammingDao commented Jan 22, 2022

I have found out that the issue occurs when upgrading pandas from 1.3.5 to 1.4.0. Staying with pandas 1.3.5 doesn't cause the issue shared above.

@bma-diy
Copy link

bma-diy commented Jan 23, 2022

Thank you for identifying the issue is with numpy. pip install pandas==1.3.5 resolved the issue for me.

Here are the error messages I experienced when using yfinance in a virtual environment:

  File "/home/ubuntu/myproject/env/lib/python3.8/site-packages/yfinance/base.py", line 295, in history
    df.index = df.index.tz_localize("UTC").tz_convert(
AttributeError: 'Index' object has no attribute 'tz_localize'

This happens whenever I try to download ticker data with 'period'.

joksas added a commit to joksas/example-strategies that referenced this issue Jan 23, 2022
Several issues might have been introduced with 1.4.0. See
<ranaroussi/yfinance#937>
@psbj
Copy link

psbj commented Jan 28, 2022

Glad this was fixed, but the update isn't on pip, it's still at 0.1.69. I'm not familiar with how releases work on pip, is it automated and just takes a while or something else?

@ProgrammingDao
Copy link
Author

ProgrammingDao commented Jan 29, 2022

@psbj you need to release a new version with the fix I think. There seems to be a GitHub action to do it. @ranaroussi tried to run it to deploy the newest package but it seems that it tried to deploy with version=0.1.169 that was already there instead of version=0.1.170.

@ProgrammingDao
Copy link
Author

@psbj, @ranaroussi has just updated the version and released the version with the fix.

@keithtmccartney
Copy link

Thank-you so much, I had been bashing my head all weekend over a resolution when trying out Part Time Larry's (@hackingthemarkets) YouTube video "PyAlgoTrade Tutorial (Part 2) - Backtesting Basics" (https://www.youtube.com/watch?v=cdeftn1AZO8). New to Python, been a C# guy for years, this is a whole lot of fun!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants