
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
표에 NaN 부분이 있어서 결과가 NaN으로 나오더라구요.
그래서 행 지우는 식을 찾아서 지우고 했더니 결과가 나왔는데요.
함수로 만들어서 숫자를 바꾸니까 199,200행이 없다고 오류가 나요.
함수로 만들었을 때 NaN이 있는 행을 알아서 지울 수 있게 하려면 어떻게 하면 되나요?

작성한 코드 및 에러 메세지
def get_return(code,n):
df = fdr.DataReader(code,'2018')
df = df[['Close']]
df['ma'] = df.rolling(n).mean().shift(1)#rolling(3)은 3일간, shift(1)은 한 칸 아래로 내림
df['action'] = np.where(df['Close'] > df['ma'], 'buy', 'sell')
df['action_temp'] = df['action'].shift(1)#action 열과 action_temp 열의 내용이 다른 부분을 찾기 위한 사전작업
df.iloc[-1,-1] = 'sell'#마지막엔 팔아야 이익을 실현할 수 있기 때문이지
cond = (df['action'] == 'buy') & (df['action_temp'] =='sell')
cond1= (df['action'] == 'sell') & (df['action_temp'] =='buy')
df['real_buy'] = np.where(cond,'buy', np.where(cond1,'sell',''))
df = df[['Close','ma','real_buy']]#왜 'Date'를 쓰면 오류가 나는 걸까?
df_buy = df[cond].reset_index()
df_buy.columns = ['날짜', '종가(buy)', '3일 이평값', '액션']
df_sell = df[cond1].reset_index()#.reset_index()가 없을 때는 한 줄은 buy, ,한 줄은 sell로 번갈아 나왔는데, 이걸 붙이니까 반쪽에만 있던 빈 줄이 없어진다.
df_sell.columns = ['날짜', '종가(sell)', '3일 이평값', '액션']
print(df_buy) #이렇게 하니까 멋없게 나오는구만.
df_sell#이렇게 하니까 예쁘게 나오는구만.
df_result = pd.concat([df_buy,df_sell],axis=1)#axis=1 때문에 두 표가 옆으로 붙음
print(df_result)
df_result_2 = df_result.drop([199,200], axis=0)#빈공간이 나오는 199,200행을 지워줌.
df_result_2
df_result_2['수익률'] = df_result_2['종가(sell)']/df_result_2['종가(buy)']#이게 1보다 작은 값이 나오면 손해본거다.
return df_result_2[['수익률']].cumprod().iloc[-1,-1]-1 #cumprod()는 누적곱
