Python-AttributeError:模块“ pandas”没有属性“ ewm”

时间:2018-08-04 23:15:33

标签: python-3.x

我正在尝试运行以下代码,但出现错误。

line 43, in <module> ups_avg = pd.ewm(ups, span=RSI_N)[-1] 
AttributeError: module 'pandas' has no attribute 'ewm

我正在使用最新的熊猫模块。 请问如何修理第43行?谢谢

from binance.client import Client    
import numpy as np    
import pandas as pd    
import smtplib    
import time    
import yaml    

CONFIG = yaml.load(open('./CONFIG.yml'))    
API_KEY = CONFIG['binance_api']['key']    
API_SECRET = CONFIG['binance_api']['secret']    
user = CONFIG['gmail']['user']    
passwd = CONFIG['gmail']['password']    

client = Client(API_KEY, API_SECRET)

# against ETH    
SYMBOLS = ('ADA', 'ADX', 'BAT', 'BCC', 'DASH', 'EOS', 'IOTA',

        'LTC', 'NEO', 'OMG', 'STORJ', 'XLM', 'NANO', 'XRP', 'XVG', 'ZEC')    
RSI_N = 14    
RSI_THRESHOLD = 8    
RUN_INTERVAL_MINS = 30

def send_email(rsi_values):    
    if len(rsi_values) > 0:        
        message = '\n'.join('{0:>8} {1:.2f}'.format(symbol, rsi) for (symbol, rsi) in rsi_values)    
        email_text = 'From: {0}\nTo: {1}\nSubject: Stock Recommendations\n\n{2}'.format(user, user, message)

        try:    
            server = smtplib.SMTP_SSL('smtp.gmail.com', 465)    
            server.ehlo()    
            server.login(user, passwd)    
            server.sendmail(user, user, email_text)    
            server.close()    
        except:    
            pass

while True:    
    rsi_values = []    
    for SYMBOL in SYMBOLS:    
        klines = client.get_historical_klines(SYMBOL + 'ETH', Client.KLINE_INTERVAL_30MINUTE, '{} hours ago UTC'.format((RSI_N + 3) // 2))    
        closings = np.asarray(klines, dtype=np.float)[-RSI_N - 1:, 4]        
        diffs = np.diff(closings)    
        ups = diffs.clip(min=0)    
        downs = diffs.clip(max=0)    
        ups_avg = pd.ewma(ups, span=RSI_N)[-1]    
        downs_avg = -pd.ewma(downs, span=RSI_N)[-1]    
        rs = ups_avg / downs_avg    
        rsi = 100 - 100 / (1 + rs)    
        rsi_values.append((SYMBOL, rsi))

    print('\n'.join('{0:>8} {1:.2f}'.format(symbol, rsi) for (symbol, rsi) in rsi_values))    
    rsi_values = list(filter(lambda x: x[1] < RSI_THRESHOLD, rsi_values))        
    send_email(rsi_values)        
    time.sleep(60 * RUN_INTERVAL_MINS)

7 个答案:

答案 0 :(得分:0)

尝试这个:

  ups_avg = pd.DataFrame.ewma(ups, span=RSI_N)[-1]

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.ewm.html

答案 1 :(得分:0)

将ups转换为dataframe ups_avg = ups.ema(span = RSI_N).mean()

答案 2 :(得分:0)

使用对象dataframe.ewm()。mean()

ups.ewm(span=RSI_N)
Eg.
df = DataFrame({'B': [0, 1, 2, np.nan, 4]})
df.ewm(com=0.5).mean()

答案 3 :(得分:0)

在尝试计算熊猫数据帧上的指数加权移动平均值时,我遇到了类似的问题。我假设您正在尝试将“ ups_avg”作为“ ups”的指数加权平均值进行同样的操作。

这为我解决了这个问题,并用一个虚拟数据帧进行了说明:

cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)

fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640,480))

while(True):
    ret, frame = cap.read()
    out.write(frame)
    cv2.imshow('frame', frame)
    c = cv2.waitKey(1)
    if c & 0xFF == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()

答案 4 :(得分:0)

尝试以下操作:

ups_avg = ups.ewm(span=RSI_N).mean() 
downs_avg = - downs.ewm(span=RSI_N).mean() 

答案 5 :(得分:0)

显然,自0.18.0以来,语法已经发生了一些变化,您应该尝试以下操作:

`ups_avg = ups.ewm(span = RSI_N).mean()

downs_avg =-downs.ewm(span = RSI_N).mean()`

答案 6 :(得分:0)

尝试使用

pd.DataFrame.ewm(ups, span=RSI_N).mean()

它很好用,因为从熊猫身上剥夺了ewma,我得到了这个对我来说很好的解决方案