python - 无法使用 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]
解决方案
推荐阅读
- azure-devops - Nuget 还原任务耗时过长
- postgresql - 错误无法初始化 Kubernetes 集群
- java - 使用 Kinesis 客户端库 (KCL 2.x) 的多个使用者到 Kinesis Stream
- java - 将 nexus 与 maven-wrapper 和 docker 一起使用
- c# - 如何在 C# 中映射双 C 结构指针?
- flutter - Flutter App 未在 Windows 的 Android 模拟器中运行
- python - 我在通过 selenium python 抓取内容时遇到错误
- javascript - 如何将动态数据放入 A4 大小的 html 页面
- mongodb - MongoDB聚合:如何从数组中的对象返回对象属性
- c++ - 为什么 std::unique 不调用 std::sort?