首页 > 解决方案 > 无法使用 python 显示 yfinance 的股息历史记录

问题描述

该程序应该通过股票代码给出公司股息的历史,但是使用元素的迭代是行不通的。下面的代码会抛出错误

import yfinance as yf
import pandas
def do_dict_history_dividends(symbol):
    '''Расчет истории дивидендов компании по кварталам
        :param symbol: Тикер компании'''
    symbol_company = yf.Ticker(symbol)
    short_name = symbol_company.info['shortName']
    global dict_of_history_dividends
    dict_of_history_dividends = {}
    value = f'История дивидендов компании "{short_name}" по кварталам:\n'
    for elem in symbol_company.dividends.keys():
        dict_of_history_dividends[str(elem)[:10]] = object_of_company.dividends[elem]
    for key in dict_of_history_dividends.keys():
        value += str(key).replace('-', '.') + ' -> ' + str(dict_of_history_dividends[key]) + '\n'
    return value if bool(dict_of_history_dividends) is True else f'Компания "{short_name}" не выплачивала дивидендов'
companies = ('MMM', 'AXP', 'AMGN', 'AAPL', 'BA', 'CAT', 'CVX', 'CSCO', 'KO', 'DOW', 'GS', 'HD', 'HON', 'IBM', 'INTC', 'JNJ', 'JPM', 'MCD', 'MRK', 'MSFT', 'NKE', 'PG', 'CRM', 'TRV', 'UNH', 'VZ', 'V', 'WBA', 'WMT', 'DIS')
for e in companies:
    print(do_dict_history_dividends(e))

错误是

Traceback (most recent call last):
  File "pandas\_libs\index.pyx", line 460, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 2131, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 2140, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 11836800000000000

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 429, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas\_libs\index.pyx", line 462, in pandas._libs.index.DatetimeEngine.get_loc
KeyError: Timestamp('1970-05-18 00:00:00')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\datetimes.py", line 702, in get_loc
    return Index.get_loc(self, key, method, tolerance)
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
    raise KeyError(key) from err
KeyError: Timestamp('1970-05-18 00:00:00')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Karapuz\Desktop\finance.py", line 183, in <module>
    print(do_dict_history_dividends(e))
  File "C:\Users\Karapuz\Desktop\finance.py", line 106, in do_dict_history_dividends
    dict_of_history_dividends[str(elem)[:10]] = object_of_company.dividends[elem]
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py", line 942, in __getitem__
    return self._get_value(key)
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py", line 1051, in _get_value
    loc = self.index.get_loc(label)
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\datetimes.py", line 704, in get_loc
    raise KeyError(orig_key) from err
KeyError: Timestamp('1970-05-18 00:00:00')
[Finished in 4.1s]

以下代码工作正常

print(yf.Ticker('MMM').dividends)

如果您尝试像在函数中一样创建字典并迭代键和值,那么就没有问题

for elem in yf.Ticker('MMM').dividends.keys():
    print(elem, yf.Ticker('MMM').dividends[elem])

1970-05-18 00:00:00 0.027375
1970-08-17 00:00:00 0.027375
1970-11-16 00:00:00 0.02725
1971-02-11 00:00:00 0.028938

另外,如果你跑一个股票

print(do_dict_history_dividends('MMM'))

同样的错误

Traceback (most recent call last):
  File "pandas\_libs\index.pyx", line 460, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 2131, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 2140, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 11836800000000000

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 429, in pandas._libs.index.DatetimeEngine.get_loc
  File "pandas\_libs\index.pyx", line 462, in pandas._libs.index.DatetimeEngine.get_loc
KeyError: Timestamp('1970-05-18 00:00:00')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\datetimes.py", line 702, in get_loc
    return Index.get_loc(self, key, method, tolerance)
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
    raise KeyError(key) from err
KeyError: Timestamp('1970-05-18 00:00:00')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Karapuz\Desktop\finance.py", line 191, in <module>
    print(do_dict_history_dividends('MMM'))
  File "C:\Users\Karapuz\Desktop\finance.py", line 106, in do_dict_history_dividends
    dict_of_history_dividends[str(elem)[:10]] = object_of_company.dividends[elem]
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py", line 942, in __getitem__
    return self._get_value(key)
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py", line 1051, in _get_value
    loc = self.index.get_loc(label)
  File "C:\Users\Karapuz\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\datetimes.py", line 704, in get_loc
    raise KeyError(orig_key) from err
KeyError: Timestamp('1970-05-18 00:00:00')
[Finished in 4.0s]

标签: pythonyfinance

解决方案


推荐阅读