pandas 现在我正在计算TA,如下所示,有没有什么方法可以使它更快或更有效地处理大量的数据

cbeh67ev  于 2023-09-29  发布在  其他
关注(0)|答案(1)|浏览(88)
import pandas as pd
import talib as ta
import datetime 

start=datetime.datetime.now()
a = ["ACC.csv", "ADANIPORTS.csv", "AMBUJACEM.csv", 'ASHOKLEY.csv', 'ASIANPAINT.csv']

dataframes = {}  

for file_name in a:
    df = pd.read_csv(file_name)
    df['SMA'] = ta.SMA(df['close'], timeperiod=14)
    macd, signal, macd_histogram = ta.MACD(df['close'], fastperiod=12, slowperiod=26)
    df['MACD'] = macd
    df['MACD_Signal'] = signal
    df['RSI'] = ta.RSI(df['close'], timeperiod=14)
    file_key = file_name.replace(".csv", "")  
    dataframes[file_key] = df  

for key, df in dataframes.items():
    print(f"File: {key}")
    print(df)
    print("\n")
print(datetime.datetime.now()-start)

我现在就像上面那样计算TA。有没有什么方法可以使它更快或更有效地处理大量的数据。

s1ag04yj

s1ag04yj1#

你可以使用下面的方法来应用多线程。

# Use a ThreadPoolExecutor to parallelize the processing of files
with concurrent.futures.ThreadPoolExecutor() as executor:
    # Submit each file for processing
    futures = [executor.submit(process_file, file_name) for file_name in a]
    # Wait for all futures to complete
    concurrent.futures.wait(futures)

相关问题