ranaroussi / yfinance

Download market data from Yahoo! Finance's API
https://aroussi.com/post/python-yahoo-finance
Apache License 2.0
14.64k stars 2.43k forks source link

TypeError raised when passing timezone (tz) argument into history() #466

Closed SelcouthTech closed 1 year ago

SelcouthTech commented 4 years ago

Hi I'm attempting to test the timezone parameter in the history function. If I understand Ticker().history(tz) correctly, the resulting dataframe index is determined as such:

  1. Using raw time from https response, convert to UTC time -- df.index.tz_convert('UTC') on line 231
  2. Use the result of (1) here and localize to the https response info for "exchangeTimezoneName" -- df.index.tz_convert('UTC').tz_convert(data["chart"]["result"][0]["meta"]["exchangeTimezoneName"]) on line 231

So are the dates in the resulting dataframe corresponding to the local exchange dates and not my machine's timezone (e.g., for Softbank (9984.T) price history date's correspond with days when Tokyo Stock Exchange is open)? If that's true and I specify a timezone argument, I receive the following TypeError:


>>>import yfinance as yf
>>>sb = yf.Ticker('9984.T').history(tz='America/Los_Angeles')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/yfinance/base.py", line 225, in history
    df = _pd.concat([quotes, dividends, splits], axis=1, sort=True)
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pandas/core/reshape/concat.py", line 274, in concat
    op = _Concatenator(
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pandas/core/reshape/concat.py", line 454, in __init__
    self.new_axes = self._get_new_axes()
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pandas/core/reshape/concat.py", line 519, in _get_new_axes
    return [
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pandas/core/reshape/concat.py", line 520, in <listcomp>
    self._get_concat_axis() if i == self.bm_axis else self._get_comb_axis(i)
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pandas/core/reshape/concat.py", line 526, in _get_comb_axis
    return get_objs_combined_axis(
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pandas/core/indexes/api.py", line 92, in get_objs_combined_axis
    return _get_combined_index(obs_idxes, intersect=intersect, sort=sort, copy=copy)
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pandas/core/indexes/api.py", line 145, in _get_combined_index
    index = union_indexes(indexes, sort=sort)
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pandas/core/indexes/api.py", line 214, in union_indexes
    return result.union_many(indexes[1:])
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pandas/core/indexes/datetimes.py", line 377, in union_many
    this, other = this._maybe_utc_convert(other)
  File "/Users/user/.pyenv/versions/3.8.5/lib/python3.8/site-packages/pandas/core/indexes/datetimelike.py", line 903, in _maybe_utc_convert
    raise TypeError("Cannot join tz-naive with tz-aware DatetimeIndex")
TypeError: Cannot join tz-naive with tz-aware DatetimeIndex
BillGatesCat commented 3 years ago

Having the same issue :(

GF-Huang commented 3 years ago

image image