【问题标题】:How to limit the decimals I retrieve when pulling data from a database? Python Pandas从数据库中提取数据时如何限制我检索的小数?蟒蛇熊猫
【发布时间】:2021-11-11 21:11:05
【问题描述】:

我正在尝试使用 python 中的 yahoo_fin 库提取股票数据。

我想检索 S&P500 中符合特定条件的所有股票。不幸的是,当调用 yahoo_fin 时,我得到一个 64 位数字,这使得程序运行非常缓慢。有什么方法可以限制我提取的数据量,只取小数点后 2 位?

from yahoo_fin import stock_info as si

spy = si.tickers_sp500()

for i in spy:
    if si.get_live_price(i) < 50.00:
        print(i)

这会打印出我想查看的代码,但速度非常慢,我相信这是因为它请求的 64 位数字很大。

【问题讨论】:

  • 我认为这与小数位无关。 yahoo_fin 模块获取数据的速度似乎很慢。使用get_next_earnings_date(i) 以相同的速度返回。我建议使用线程来加快速度。
  • 看起来这个包是从 yahoo 财务图表中提取的,所以它为每个代码加载了一个对接负载的数据。使用 alpha-vantage query1.finance.yahoo.com/v8/finance/chart/TSLA 可能会更快
  • 啊,好的,谢谢!我只是假设,因为如果我不要求价格,而只是要求代码或非数字数据,它会立即发生
  • 好的,谢谢,我会调查 alpha-vantage,这可能正是我想要的

标签: python pandas dataframe stock yahoo-finance


【解决方案1】:

正如 cmets 中所讨论的,yahoo_fin 模块获取数据的速度很慢。我想进一步指出,无论您决定使用yahoo-fin 还是alpha-vantage,线程都可能是一个必要因素。

这是一个可以实现线程的粗略示例。运行此方法大约需要 10-15 秒,而另一种方法则需要几分钟。

from yahoo_fin import stock_info as si
import threading

global price_list
price_list = []

# this will get called many times
def print_low_ticks(chunk):
    global price_list
    for i in chunk:
        live_price = si.get_live_price(i)
        if live_price < 50.00:
            price_list.append(i)

spy = si.tickers_sp500()
threads = []

# create threads that only process 10 values
for i in range(len(spy)):
    if ((i % 10 == 0) or (i == 0)):
        x = threading.Thread(target=print_low_ticks, 
                             args=([spy[i:i+10]]),
                             daemon=False)
        threads.append(x)
        x.start()

# wait for all threads to finish
for thread in threads:
    thread.join()

# print alphabetically sorted list
print(sorted(price_list))

【讨论】:

  • 太棒了,谢谢!我将研究线程,因为这是我在这种情况下第一次听说它。
猜你喜欢
  • 2017-01-26
  • 2022-01-11
  • 2022-01-22
  • 1970-01-01
  • 2020-07-18
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
相关资源
最近更新 更多