我試圖把每一行的總和我的熊貓數據幀:在熊貓數據幀總結行返回NAN
new_df['cash_change'] = new_df.sum(axis=0)
但是我的成績回頭率NaN
我想可能有當我將職位轉換爲乘號的小數點:
pos_to_dec = np.array([Decimal(d) for d in security.signals['positions'].values])
我需要做的就是將我的列乘以一起。我將它轉換回不過:
cash_change[security.symbol] = cash_change[security.symbol].astype(float)
下面是完整的方法。其目的是進行一些列的乘法每個安全然後總和總在最後:
def get_cash_change(self):
"""
Calculate daily cash to be transacted every day. Cash change depends on
the position (either buy or sell) multiplied by the adjusted closing price
of the equity multiplied by the trade amount.
:return:
"""
cash_change = pd.DataFrame(index=self.positions.index)
try:
for security in self.market_on_close_securities:
# First convert all the positions from floating-point to decimals
pos_to_dec = np.array([Decimal(d) for d in security.signals['positions'].values])
cash_change['positions'] = pos_to_dec
cash_change['bars'] = security.bars['adj_close_price'].values
# Perform calculation for cash change
cash_change[security.symbol] = cash_change['positions'] * cash_change['bars'] * self.trade_amount
cash_change[security.symbol] = cash_change[security.symbol].astype(float)
# Clean up for next security
cash_change.drop('positions', axis=1, inplace=True)
cash_change.drop('bars', axis=1, inplace=True)
except InvalidOperation as e :
print("Invalid input : " + str(e))
# Sum each equities change in cash
new_df = cash_change.dropna()
new_df['cash_change'] = new_df.sum(axis=0)
return cash_change
我new_df
數據幀最終看起來是這樣的:
MTD ESS SIG SNA cash_change
price_date
2000-01-04 0.0 0.00 0.00 0.00 NaN
2000-01-05 0.0 0.00 0.00 0.00 NaN
2000-01-06 0.0 0.00 0.00 0.00 NaN
2000-01-07 0.0 0.00 0.00 0.00 NaN
2000-01-10 0.0 0.00 0.00 0.00 NaN
2000-01-11 0.0 0.00 0.00 0.00 NaN
2000-01-12 0.0 0.00 0.00 0.00 NaN
2000-01-13 0.0 0.00 0.00 0.00 NaN
2000-01-14 0.0 0.00 0.00 0.00 NaN
2000-01-18 0.0 0.00 0.00 0.00 NaN
2000-01-19 0.0 0.00 0.00 0.00 NaN
2000-01-20 0.0 0.00 0.00 0.00 NaN
2000-01-21 0.0 0.00 0.00 0.00 NaN
2000-01-24 0.0 1747.83 1446.71 0.00 NaN
2000-01-25 3419.0 0.00 0.00 0.00 NaN
2000-01-26 0.0 0.00 0.00 1660.38 NaN
2000-01-27 0.0 0.00 -1293.27 0.00 NaN
2000-01-28 0.0 0.00 0.00 0.00 NaN
任何建議上我做錯了什麼?或者可能另一種方法來總結每一行的列?
我建議添加最小可重現的示例 - http://stackoverflow.com/help/mcve。這裏的人通常願意幫忙,但我懷疑很多SO用戶會用你當前的問題玩這個猜謎遊戲 –