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

Unknown timezone error #1359

Closed
msroth opened this issue Jan 26, 2023 · 12 comments
Closed

Unknown timezone error #1359

msroth opened this issue Jan 26, 2023 · 12 comments

Comments

@msroth
Copy link

msroth commented Jan 26, 2023

I'm getting a strange timezone error that I can't track down. No where am I knowingly accessing the timezone data. It seems to be something going on with Pandas and the data fetch. Any ideas? Using yfinance v0.2.6, Python v3.9, Pandas v1.4.2.

code:
import yfinance as yf
yf.pdr_override()
ticker = yf.Ticker('AAPL')
print('last price = ' + ticker.basic_info['last_price'])

error:
File "/home/pi/.local/lib/python3.9/site-packages/yfinance/base.py", line 103, in getitem
File "/home/pi/.local/lib/python3.9/site-packages/yfinance/base.py", line 221, in last_price
File "/home/pi/.local/lib/python3.9/site-packages/yfinance/base.py", line 116, in _get_1y_prices
File "/home/pi/.local/lib/python3.9/site-packages/yfinance/base.py", line 615, in history
File "/home/pi/.local/lib/python3.9/site-packages/yfinance/utils.py", line 442, in set_df_tz
File "/home/pi/.local/lib/python3.9/site-packages/pandas/core/indexes/datetimes.py", line 268, in tz_convert
File "/home/pi/.local/lib/python3.9/site-packages/pandas/core/arrays/datetimes.py", line 870, in tz_convert
File "pandas/_libs/tslibs/timezones.pyx", line 111, in pandas._libs.tslibs.timezones.maybe_get_tz
File "pandas/_libs/tslibs/timezones.pyx", line 134, in pandas._libs.tslibs.timezones.maybe_get_tz
File "/home/pi/.local/lib/python3.9/site-packages/pytz/init.py", line 188, in timezone
pytz.exceptions.UnknownTimeZoneError: 'America/New_York'

@tab-co
Copy link

tab-co commented Jan 27, 2023

try this answer #1325 (comment)

@ValueRaider
Copy link
Collaborator

ValueRaider commented Jan 27, 2023

What version is pytz? And maybe try upgrading pandas too.

@tab-co
Copy link

tab-co commented Jan 27, 2023 via email

@tab-co
Copy link

tab-co commented Jan 27, 2023

I am using pytz 2022.7.1 with pandas 1.5.3 and pandas-datareader 0.10.0 (PDR below). If it matters, also numpy 1.24.1
For the excel side I am using XlsxWriter 3.0.7
Yfinance still fails on a timezone violation until I did this (to go time zone agnostic) at the recommendation of "irezumi":

# retrieve historical index data within defined date range from Yahoo
historicalIndex = pdr.get_data_yahoo(symbol, start=start_date, end=fix_date)
# remove time zone localisation in DataFrame index. *NB* Indexing a timezone-aware DatetimeIndex with a
# timezone-naive datetime is deprecated and will raise KeyError in a future version
historicalIndex = historicalIndex.tz_localize(None)

My python code now runs

@ValueRaider
Copy link
Collaborator

I am confused because that recommendation doesn't change anything inside yfinance, so cannot possibly fix your original error.

If you remove the tz_localize(None) and run with latest yfinance, what happens exactly?

@tab-co
Copy link

tab-co commented Jan 27, 2023

If i remove the tz_localize(None); here's what happens:

***** converting data to worksheet >>> NASDAQ <<< DATA
Traceback (most recent call last):
File ".....\Newsletter\Yahoo_finance_index_graphing(production version 2022-02-08).py", line 714, in
historicalIndex.to_excel(writer, sheet_name=label)
File ".....l\Newsletter\venv\Lib\site-packages\pandas\util_decorators.py", line 211, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File ".....l\Newsletter\venv\Lib\site-packages\pandas\util_decorators.py", line 211, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File ".....Newsletter\venv\Lib\site-packages\pandas\core\generic.py", line 2374, in to_excel
formatter.write(
File ".....l\Newsletter\venv\Lib\site-packages\pandas\io\formats\excel.py", line 950, in write
writer._write_cells(
File ".....\Sentinel\Newsletter\venv\Lib\site-packages\pandas\io\excel_xlsxwriter.py", line 266, in _write_cells
for cell in cells:
File ".....\Newsletter\venv\Lib\site-packages\pandas\io\formats\excel.py", line 888, in get_formatted_cells
cell.val = self._format_value(cell.val)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".....\Newsletter\venv\Lib\site-packages\pandas\io\formats\excel.py", line 602, in _format_value
raise ValueError(
ValueError: Excel does not support datetimes with timezones. Please ensure that datetimes are timezone unaware before writing to Excel.

If i go back to yfinance 0.2.3 it also fixes the problem

@tab-co
Copy link

tab-co commented Jan 27, 2023

sorry about the file references - I redacted the names to remove identifying or sensitive file locations, but was not circumspect in scrubbing them all to the same stub of ".....\Newsletter\

@ValueRaider
Copy link
Collaborator

No worries. This is an Excel problem not yfinance

@tab-co
Copy link

tab-co commented Jan 27, 2023 via email

@tab-co
Copy link

tab-co commented Jan 27, 2023 via email

@ValueRaider
Copy link
Collaborator

ValueRaider commented Jan 27, 2023

Because the code blowing up isn't yfinance nor any of its dependencies, and nothing inherently wrong with data. Returning timezone-aware datetimes is reasonable, and introduction in 0.1.75 fixed a bunch of user experience problems. It's the right thing to do, and easy to remove the timezone.

Suppose Excel could not parse floating-point data, or NaNs - would that be the fault of yfinance?

0.2.4 restored intended default behaviour of download() returning timezone-aware, see #1283.

@tab-co
Copy link

tab-co commented Jan 27, 2023 via email

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

No branches or pull requests

3 participants