简体   繁体   中英

Python Pandas Exception Occurred “example” when Executed Pandas operation in function method,simple function worked

I am facing a strange issue and try to explain this here.

Look at this code,Here "df" is pandas dataframe with date as index and open,high,low,close,volume as columns.

calc(df) is the function i am calling to calculate the logic and calling the ematv() inside the get_update() function.

1.In the below code when i run

df = ematv(df.fillna(0),13)

no errors seen,This is calculating EMA based on 'close'

2.The same function called from get_update() ,df=ematv(df['example'].fillna(0),lengthema,name='example') when i use to calculate ema where i send 'example' column as data.Here i get the error. exception occurred 'example'

What is wrong that i am doing ,This may be a good lesson for me to learn. Few of the warning i have got given below.

def ematv(df,period,name='close'):#closely matching except the decimal
    newcol ='ema'+str(period)+name
    df[newcol] = df[name].ewm(span=period,min_periods=0,adjust=False,ignore_na=False).mean()
    return df


def get_update(df, length=None,threshold = None,lengthema=None):
    if length is None:
        length = 14
        lengthema=13
        column_name = 't1'
    else:
        length = int(length)
        lengthema=int(lengthema)
        column_name = 'ti_{}'.format(length)

    df['example']=TA.test(df)//example column with value added
    df=ematv(df['example'].fillna(0),lengthema,name='example')
    return df


def calc(df):
    df = ematv(df.fillna(0),13)
    df  = get_update(df.fillna(0), length=20,threshold =0,lengthema=13)
    return df

calc(df)

some Warning i get:

pydevd_resolver.py:166: FutureWarning: Series.base is deprecated and will be removed in a future version attr = getattr(var, n) pydevd_resolver.py:166: FutureWarning: Series.data is deprecated and will be removed in a future version attr = getattr(var, n) pydevd_resolver.py:166: FutureWarning: Series.flags is deprecated and will be removed in a future version attr = getattr(var, n) pydevd_resolver.py:166: FutureWarning: Series.itemsize is deprecated and will be removed in a future version attr = getattr(var, n) pydevd_resolver.py:166: FutureWarning: Series.strides is deprecated and will be removed in a future version attr = getattr(var, n) pydevd_resolver.py:71: FutureWarning: Series.flags is deprecated and will be removed in a future version return getattr(var, attribute) pydevd_resolver.py:71: FutureWarning: Series.strides is deprecated and will be removed in a future version return getattr(var, attribute)

edit 1: Tried changing the code with this edit.

def ematv(dff:DataFrame,period,name='close')-> Series:#closely matching except the decimal
    newcol ='ema'+str(period)+name
    dff[newcol] = dff[name].ewm(span=period,min_periods=0,adjust=False,ignore_na=False).mean()
    return dff

here now the ema is calculated but i am getting this error exception occured Wrong number of items passed 34, placement implies 1

how this is solving the issue i am not very sure ,This solved the problem. adding this df['examples']='' solves the problem before calling the

df['examples']=ematv

def ematv(dff,period,name='close'):#closely matching except the decimal
    newcol ='ema'+str(period)+name
    dff[newcol] = dff[name].ewm(span=period,min_periods=0,adjust=False,ignore_na=False).mean()
    return dff[newcol]


def get_update(df, length=None,threshold = None,lengthema=None):
    if length is None:
        length = 14
        lengthema=13
        column_name = 't1'
    else:
        length = int(length)
        lengthema=int(lengthema)
        column_name = 'ti_{}'.format(length)

    df['example']=TA.test(df)//example column with value added
    df['examples']=''
    df['examples']=ematv(df['example'].fillna(0),lengthema,name='example')
    return df


def calc(df):
    df = ematv(df.fillna(0),13)
    df  = get_update(df.fillna(0), length=20,threshold =0,lengthema=13)
    return df

calc(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM