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

throw exception when calling history() on a vietnamese stock which contains dividend record #2270

Open
alai04 opened this issue Feb 17, 2025 · 2 comments

Comments

@alai04
Copy link

alai04 commented Feb 17, 2025

Describe bug

When calling history() for Vietnamese stocks, if it contains dividend records, the dividend records will have a currency field, causing an exception. For example:

tk = yf.Ticker('PNJ.VN')
hist = tk.history(start='2024-11-01')

will cause:

  File "/Users/alai04/projects/python/yfinance/history.py", line 8, in <module>
    hist = tk.history(start='2024-11-01')
  File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/yfinance/utils.py", line 104, in wrapper
    result = func(*args, **kwargs)
  File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/yfinance/base.py", line 80, in history
    return self._lazy_load_price_history().history(*args, **kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/yfinance/utils.py", line 104, in wrapper
    result = func(*args, **kwargs)
  File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/yfinance/scrapers/history.py", line 318, in history
    dividends, splits, capital_gains = utils.parse_actions(data["chart"]["result"][0])
                                       ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/yfinance/utils.py", line 536, in parse_actions
    dividends.columns = ["Dividends"]
    ^^^^^^^^^^^^^^^^^
  File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/pandas/core/generic.py", line 6313, in __setattr__
    return object.__setattr__(self, name, value)
           ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "properties.pyx", line 69, in pandas._libs.properties.AxisProperty.__set__
  File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/pandas/core/generic.py", line 814, in _set_axis
    self._mgr.set_axis(axis, labels)
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/pandas/core/internals/managers.py", line 238, in set_axis
    self._validate_set_axis(axis, new_labels)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/pandas/core/internals/base.py", line 98, in _validate_set_axis
    raise ValueError(
    ...<2 lines>...
    )
ValueError: Length mismatch: Expected axis has 2 elements, new values have 1 elements

Simple code that reproduces your problem

tk = yf.Ticker('PNJ.VN')
hist = tk.history(start='2024-11-01')

Debug log

DEBUG Entering history()
DEBUG Entering history()
DEBUG PNJ.VN: Yahoo GET parameters: {'period1': '2024-11-01 00:00:00+07:00', 'period2': '2025-02-17 14:10:03+07:00', 'interval': '1d', 'includePrePost': False, 'events': 'div,splits,capitalGains'}
DEBUG Entering get()
DEBUG Entering _make_request()
DEBUG url=https://query2.finance.yahoo.com/v8/finance/chart/PNJ.VN
DEBUG params={'period1': 1730394000, 'period2': 1739776203, 'interval': '1d', 'includePrePost': False, 'events': 'div,splits,capitalGains'}
DEBUG Entering _get_cookie_and_crumb()
DEBUG cookie_mode = 'basic'
DEBUG Entering _get_cookie_and_crumb_basic()
DEBUG loaded persistent cookie
DEBUG reusing cookie
DEBUG crumb = 'vsEB7v4e5NI'
DEBUG Exiting _get_cookie_and_crumb_basic()
DEBUG Exiting _get_cookie_and_crumb()
DEBUG response code=200
DEBUG Exiting _make_request()
DEBUG Exiting get()
DEBUG PNJ.VN: yfinance received OHLC data: 2024-11-01 02:00:00 -> 2025-02-17 06:54:51
DEBUG PNJ.VN: OHLC after cleaning: 2024-11-01 09:00:00+07:00 -> 2025-02-17 13:54:51+07:00
Traceback (most recent call last):
File "/Users/alai04/projects/python/yfinance/history.py", line 6, in
hist = tk.history(start='2024-11-01')
File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/yfinance/utils.py", line 104, in wrapper
result = func(*args, **kwargs)
File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/yfinance/base.py", line 80, in history
return self._lazy_load_price_history().history(*args, **kwargs)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/yfinance/utils.py", line 104, in wrapper
result = func(*args, **kwargs)
File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/yfinance/scrapers/history.py", line 318, in history
dividends, splits, capital_gains = utils.parse_actions(data["chart"]["result"][0])
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/yfinance/utils.py", line 535, in parse_actions
dividends.columns = ["Dividends"]
^^^^^^^^^^^^^^^^^
File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/pandas/core/generic.py", line 6313, in setattr
return object.setattr(self, name, value)
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
File "properties.pyx", line 69, in pandas._libs.properties.AxisProperty.set
File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/pandas/core/generic.py", line 814, in _set_axis
self._mgr.set_axis(axis, labels)
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/pandas/core/internals/managers.py", line 238, in set_axis
self._validate_set_axis(axis, new_labels)
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
File "/Users/alai04/.virtualenvs/stock/lib/python3.13/site-packages/pandas/core/internals/base.py", line 98, in _validate_set_axis
raise ValueError(
...<2 lines>...
)
ValueError: Length mismatch: Expected axis has 2 elements, new values have 1 elements

Bad data proof

No response

yfinance version

0.2.53

Python version

3.13.2

Operating system

macOS 15.3

@ValueRaider
Copy link
Collaborator

Might be worth comparing div currency against price currency in metadata, sometimes Yahoo mixes currencies.

@VadimLopatin
Copy link

Hello Team,

I am getting the same error when trying to get UAE stock price history.

test = yf.Ticker("ADCB.AB")
test.history(auto_adjust=False, period='max')

yfinance version 0.2.54
python version 3.11.11


ValueError Traceback (most recent call last)
in <cell line: 0>()
----> 1 test.history(auto_adjust=False, period='max')

8 frames
/usr/local/lib/python3.11/dist-packages/yfinance/utils.py in wrapper(*args, **kwargs)
102
103 with IndentationContext():
--> 104 result = func(*args, **kwargs)
105
106 logger.debug(f'Exiting {func.name}()')

/usr/local/lib/python3.11/dist-packages/yfinance/base.py in history(self, *args, **kwargs)
78 @utils.log_indent_decorator
79 def history(self, *args, **kwargs) -> pd.DataFrame:
---> 80 return self._lazy_load_price_history().history(*args, **kwargs)
81
82 # ------------------------

/usr/local/lib/python3.11/dist-packages/yfinance/utils.py in wrapper(*args, **kwargs)
102
103 with IndentationContext():
--> 104 result = func(*args, **kwargs)
105
106 logger.debug(f'Exiting {func.name}()')

/usr/local/lib/python3.11/dist-packages/yfinance/scrapers/history.py in history(self, period, interval, start, end, prepost, actions, auto_adjust, back_adjust, repair, keepna, proxy, rounding, timeout, raise_errors)
316
317 # actions
--> 318 dividends, splits, capital_gains = utils.parse_actions(data["chart"]["result"][0])
319 if not expect_capital_gains:
320 capital_gains = None

/usr/local/lib/python3.11/dist-packages/yfinance/utils.py in parse_actions(data)
533 dividends.index = _pd.to_datetime(dividends.index, unit="s")
534 dividends.sort_index(inplace=True)
--> 535 dividends.columns = ["Dividends"]
536
537 if "capitalGains" in data["events"]:

/usr/local/lib/python3.11/dist-packages/pandas/core/generic.py in setattr(self, name, value)
6311 try:
6312 object.getattribute(self, name)
-> 6313 return object.setattr(self, name, value)
6314 except AttributeError:
6315 pass

properties.pyx in pandas._libs.properties.AxisProperty.set()

/usr/local/lib/python3.11/dist-packages/pandas/core/generic.py in _set_axis(self, axis, labels)
812 """
813 labels = ensure_index(labels)
--> 814 self._mgr.set_axis(axis, labels)
815 self._clear_item_cache()
816

/usr/local/lib/python3.11/dist-packages/pandas/core/internals/managers.py in set_axis(self, axis, new_labels)
236 def set_axis(self, axis: AxisInt, new_labels: Index) -> None:
237 # Caller is responsible for ensuring we have an Index object.
--> 238 self._validate_set_axis(axis, new_labels)
239 self.axes[axis] = new_labels
240

/usr/local/lib/python3.11/dist-packages/pandas/core/internals/base.py in _validate_set_axis(self, axis, new_labels)
96
97 elif new_len != old_len:
---> 98 raise ValueError(
99 f"Length mismatch: Expected axis has {old_len} elements, new "
100 f"values have {new_len} elements"

ValueError: Length mismatch: Expected axis has 2 elements, new values have 1 elements

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